缘由

因为开发那边在转 dubbo 服务,测试也需要了解相关知识,所以有了这篇

前提

zookeeper

dubbo 源码中默认的是 Multicast 注册,但实际工作中是用 zookeeper 的

  1. 官网下载 zookeeper
  2. 修改 zookeeper-3.4.12\conf\zoo_sample.cfg 为 zoo.cfg,并更新内容
  3. 运行:zookeeper-3.4.12\bin\zkServer.cmd
#配置文件更新内容:
dataDir=E:\\DevE\\zookeeper-3.4.12\\data
dataLogDir=E:\\DevE\\zookeeper-3.4.12\\log

dubbo 示例

在 dubbo 源码目录中有一个 demo 工程,dubbo-demo,使用 idea 打开

在 parent pom 中增加 zookeeper 依赖
<dependency>
    <groupId>org.apache.zookeeper</groupId>
    <artifactId>zookeeper</artifactId>
    <version>3.4.12</version>
</dependency>
<dependency>
    <groupId>com.101tec</groupId>
    <artifactId>zkclient</artifactId>
    <version>0.10</version>
</dependency>
修改 Provider 以及 Consumer 中的注册信息为:
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
按顺序打包:
  1. dubbo-demo-api
  2. dubbo-demo-provider
  3. dubbo-demo-consumer
按顺序运行:

dubbo-provider
dubbo-consumer

查看结果日志,单机版 helloworld 已完成

dubbo admin

安装 dubbo admin 主要方便查看管理 dubbo 服务

  1. 官网下载 tomcat8
  2. 下载 dubbo admin 源码,并打 war 包,dubbo-admin-2.0.0.war
  3. 将 war 包扔进 apache-tomcat-8.5.24\webapps
  4. 修改 tomcat 的默认端口,比如我的改成了 8123, apache-tomcat-8.5.24\conf\server.xml
  5. 启动 tomcat:apache-tomcat-8.5.24\bin\startup.bat
  6. 打开浏览器,访问:http://localhost:8123/dubbo-admin-2.0.0
  7. 默认用户名密码都是:root,存在配置文件中:dubbo-admin\src\main\webapp\WEB-INF\dubbo.properties 或修改 apache-tomcat-8.5.24\webapps\dubbo-admin-2.0.0\WEB-INF\dubbo.properties
<!--server.xml-->
<Connector port="8123" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />

至此环境搭建完成

自动化测试(junit 及 testng 用例设计)

新建一个 maven 工程,pom 如下
<?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>com.jollychic</groupId>
    <artifactId>dubbo-test</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <allure.version>1.5.4</allure.version>
        <lombok.version>1.16.18</lombok.version>
        <slf4j.version>1.7.25</slf4j.version>
        <jsonpath.version>2.4.0</jsonpath.version>

        <springframework.version>4.3.12.RELEASE</springframework.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.12</version>
        </dependency>
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.10</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.6.2</version>
        </dependency>

        <dependency>
            <groupId>ru.yandex.qatools.allure</groupId>
            <artifactId>allure-testng-adaptor</artifactId>
            <version>${allure.version}</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo-demo-provider</artifactId>
            <version>2.6.2</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${springframework.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${springframework.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${springframework.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${springframework.version}</version>
        </dependency>
    </dependencies>
</project>
resources 目录下新增 dubbo.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    <dubbo:application name="app"/>

    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>

    <dubbo:reference id="demoService" interface="com.alibaba.dubbo.demo.DemoService" url="dubbo://127.0.0.1:20880" timeout="1000"/>

</beans>
junit demo
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:/dubbo.xml"})
public class JunitDemo {

    @Autowired
    DemoService demoService;

    @Test
    public void testDemoService(){
        demoService.sayHello("haha my test");
    }

}
testng demo
@ContextConfiguration(locations = {"classpath:/dubbo.xml"})
public class TestngDemo extends AbstractTestNGSpringContextTests {

    @Autowired
    private DemoService demoService;

    @Test
    public void testDemoService(){
        demoService.sayHello("testng my test");
    }

}

性能测试(jmeter 4.0)

更新 pom 依赖(基于上面自动化的 pom 文件)
jmeter case demo
public class JmeterDemo extends AbstractJavaSamplerClient {

    private static final ApplicationContext context = new ClassPathXmlApplicationContext("dubbo.xml");
    private DemoService demoService;

    @Override
    public void setupTest(JavaSamplerContext arg0){
        demoService = (DemoService) context.getBean("demoService");
    }

    @Override
    public SampleResult runTest(JavaSamplerContext javaSamplerContext) {
        SampleResult sr = new SampleResult();
        sr.sampleStart();
        JSONObject result = demoService.sayHello("jmeter test");
        System.out.println("result:" + result.toJSONString());
        sr.setResponseData(result.get("result").toString(), "UTF-8");
        sr.setSuccessful(true);

        return sr;
    }
}

public class JmeterDemoTest {

    public static void  main(String [] args){
        JavaSamplerContext arg0 = new JavaSamplerContext(new Arguments());
        JmeterDemo test = new JmeterDemo();
        test.setupTest(arg0);
        test.runTest(arg0);
    }
}
run case on jmeter
  1. 修改 jmeter 配置文件:apache-jmeter-4.0\bin\jmeter.properties,将被测包及其依赖加入到 jmeter 中
  2. 启动 jmeter:apache-jmeter-4.0\bin\jmeter.bat
  3. 新建一个任务,线程组,java request,balabala
# 更新jmeter配置文件
search_paths=E:/Workspace/dubbo-test/target/lib;

问题

Reference

dubbo 源码及示例
dubbo admin 源码
dubbo admin 安装手册
tomcat 用户名密码修改
dubbo 基础运用 -- 英文
dubbo 用户手册


↙↙↙阅读原文可查看相关链接,并与作者交流