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

bauul · June 11, 2018 · Last by hello replied at October 19, 2018 · 1996 hits

缘由

因为开发那边在转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 条回复 时间 点赞
bauul #1 · June 13, 2018 作者

论坛变英文风格了...

bauul #2 · June 13, 2018 作者

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

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

bauul #4 · October 19, 2018 作者
Wayyt 回复

报什么错了?

实用,马克~

需要 Sign In 后方可回复, 如果你还没有账号请点击这里 Sign Up