线上第七期_接口测试应用 dubbo 与 swagger_20190112

Dubbo的介绍

搭建dubbo环境

搭建注册中心

wget http://archive.apache.org/dist/zookeeper/zookeeper-3.3.3/zookeeper-3.3.3.tar.gz
tar zxvf zookeeper-3.3.3.tar.gz
cd zookeeper-3.3.3
cp conf/zoo_sample.cfg conf/zoo.cfg

zoo.conf的配置,修改一个本地的目录

# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
dataDir=/root/dubbo/zookeeper-3.3.3/data
# the port at which the clients will connect
clientPort=12181

熟悉docker的也可以用docker完全搭建起来

客户端测试

echo dump | nc jenkins.testing-studio.com 12181

maven安装

需要3.1版本以上

管理控制台搭建

修改zookeeper的默认连接端口地址12181

更改默认端口

mvn --projects dubbo-admin-backend spring-boot:run -Dserver.port=18080

切换到老版本的管理控制台

git checkout master
mvn --projects dubbo-admin spring-boot:run -Dserver.port=18080

测试环境地址: http://jenkins.testing-studio.com:18080

provider搭建

需要修改的地方

  • 去掉自带的嵌入式的zookeeper启动代码
  • 更改zookeeper的默认地址为12181
git clone https://github.com/apache/incubator-dubbo-samples.git
cd *samples/*basic
#vi src/main/resources/spring/dubbo-demo-provider.xml
mvn clean package
mvn -Djava.net.preferIPv4Stack=true \\
-Dexec.mainClass=org.apache.dubbo.samples.basic.BasicProvider  \\
exec:java

consumer的使用

在独立的另外一个机器上部署

需要修改的地方

vi vi src/main/resources/spring/dubbo-demo-consumer.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <!-- consumer's application name, used for tracing dependency relationship (not a matching criterion),
    don't set it same as provider -->
    <dubbo:application name="seveniruby-consumer"/>

    <!-- use multicast registry center to discover service -->
    <dubbo:registry group="aaa" address="zookeeper://jenkins.testing-studio.com:12181"/>

    <!-- generate proxy for the remote service, then demoService can be used in the same way as the
    local regular interface -->
    <dubbo:reference id="demoService" check="false" interface="org.apache.dubbo.samples.basic.api.DemoService"/>

</beans>
git clone https://github.com/apache/incubator-dubbo-samples.git
cd *samples/*basic
#vi src/main/resources/spring/dubbo-demo-consumer.xml
mvn clean package
mvn -Djava.net.preferIPv4Stack=true \\
-Dexec.mainClass=org.apache.dubbo.samples.basic.BasicConsumer  \\
exec:java

telnet协议

  • ps
  • ls
  • cd pwd
  • invoke
  • status

dubbo测试用例编写

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>hogwarts.testerhome.com</groupId>
    <artifactId>DubboTest7</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.8.RELEASE</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.6.5</version>
        </dependency>

        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
            <version>4.1.32.Final</version>
        </dependency>


        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>2.12.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-client</artifactId>
            <version>2.12.0</version>
        </dependency>
    </dependencies>
</project>

consumer.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <!-- consumer's application name, used for tracing dependency relationship (not a matching criterion),
    don't set it same as provider -->
    <dubbo:application name="demo-consumer"/>

    <!-- use multicast registry center to discover service -->
    <dubbo:registry group="aaa" address="zookeeper://jenkins.testing-studio.com:12181"/>

    <!-- generate proxy for the remote service, then demoService can be used in the same way as the
    local regular interface -->
    <dubbo:reference id="demoService" check="false" interface="org.apache.dubbo.samples.basic.api.DemoService"/>
    <dubbo:reference id="demoService2" check="false" interface="org.apache.dubbo.samples.basic.api2.DemoService"/>

</beans>

依赖的接口文件可以通过maven获取

测试用例

import org.apache.dubbo.samples.basic.api.DemoService;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;

public class ConsumerTest {
    static ClassPathXmlApplicationContext context;
    static DemoService demoService;
    @BeforeClass
    public static void beforeClass(){
        if(context==null) {
            context = new ClassPathXmlApplicationContext(
                    "consumer.xml");
            System.out.println("load");
            context.start();
            System.out.println("start");
        }
        demoService=(DemoService) context.getBean("demoService");

    }
    @Test
    public void consumer1() throws InterruptedException {

        System.out.println("bean");
        for(int i=0;i<10;i++) {
            System.out.println(demoService.sayHello("思寒 "+i));
        }

    }

    @Test
    public void sayHelloWithNotSafe(){
        String result=demoService.sayHello("");
        assertThat(result, containsString("seveniruby says hello to ,"));
        String result2=demoService.sayHello(" ");
        assertThat(result2, containsString("seveniruby says hello to  ,"));
    }

    @Test
    public void sayHelloWithSpec(){
        String result=demoService.sayHello("\\"");
        assertThat(result, containsString("seveniruby says hello to \\","));
    }

    @Test
    public void sayHelloWithSql(){
        String result=demoService.sayHello("' or 1=1");
        assertThat(result, containsString("seveniruby says hello to \\" or 1=1,"));
    }


}

测试理念 拔高

  • 泛化调用打造通用测试框架
  • 利用协议代理把http调用转化为dubbo调用,就可以使用RestAssured调用dubbo以及用于其他协议的测试

栽坑

  • zookeeper 3.3.3版本无法写入导致无法注册consumer
  • dubbo的xml namespace访问出错,mvn clean

作业1

使用consumer连接阿里云上的registry,并调用远程provider,把consumer的输出贴到回复里

作业2

编写基于junit或者testng的consumer测试用例,把测试用例贴到回复里

关闭