通用技术 [dubbo][dubbo admin] 环境搭建及 demo 用例设计

bauul · 2018年06月11日 · 最后由 hello 回复于 2018年10月19日 · 2890 次阅读

缘由

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

前提

  • java8 及环境变量配置(略)

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 依赖
  • 增加复制所有依赖包和 snapshot 包到目标位置:${project.build.directory}/lib

    <jmeter.version>4.0</jmeter.version>
    
    <dependency>
        <groupId>org.apache.jmeter</groupId>
        <artifactId>ApacheJMeter_java</artifactId>
        <version>${jmeter.version}</version>
    </dependency>
    
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        <!--<overWriteReleases>true</overWriteReleases>-->
                        <!--<overWriteSnapshots>true</overWriteSnapshots>-->
                        <overWriteIfNewer>true</overWriteIfNewer>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    
        <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
                <execution>
                    <phase>package</phase>
                    <configuration>
                        <tasks>
                            <copy todir="${project.build.directory}/lib">
                                <fileset dir="${project.build.directory}">
                                    <include name="*.jar" />
                                </fileset>
                            </copy>
                        </tasks>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    
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 用户手册

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
共收到 5 条回复 时间 点赞

论坛变英文风格了...

dubbo 接口的自动化测试和性能测试的用例如何重用呢?

现在卡到了打 war 这里,打 war 包一直不成功。请教一下是什么问题

Wayyt 回复

报什么错了?

实用,马克~

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册