一.先了解下 dubbo 的原理,最好自己搭建一个案例
可参考以下方式搭建
http://09792bb8.wiz03.com/share/s/09uiKU3j2kR120MIpT2AdLm70pfBmE1zFApv2jiDZ01GhE8j
二.编写 dubbo 测试脚本
源码:
https://git.coding.net/mgjerome/jmeter_dubbo.git
1.创建 jmeter_dubbo Maven 项目
直接 Next 创建项目
2.添加 pom.xml 配置
<properties>
<spring.version>3.2.4.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--dubbo注册中心-->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
</dependency>
<!--zookeeper客户端-->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
3.resources 目录下创建 lib 文件夹存放, dubbo 接口 jar
ApacheJMeter_core.jar ApacheJMeter_java.jar (apache-jmeter-3.2\lib\ext 目录下)
jorphan.jar(apache-jmeter-3.2\lib 目录下 用于 main 调试执行)
dubbo.xsd(由于http://code.alibabatech.com/schema/dubbo/dubbo.xsd 服务已停用 从网上下载该文件,或者从 dubbo-2.5.3.jar META-INF 目录下导出
)
4.创建 dubbo 消费者 xml 文件 consumer.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
./dubbo.xsd"
>
<!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
<dubbo:application name="hello-consumer" />
<!-- 使用multicast广播注册中心暴露发现服务地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<dubbo:reference id="helloService" interface="com.service.HelloService" />
</beans>
5.编写 jmeter 脚本
package com.buddo;
import com.service.HelloService;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by CWGJ008 on 2017/10/13.
*/
public class Dubbo_port extends AbstractJavaSamplerClient {
private static ApplicationContext context = new ClassPathXmlApplicationContext("consumer.xml");
private static HelloService helloService;
private static long start = 0;
private static long end = 0;
public void setupTest(JavaSamplerContext arg0){
helloService=(HelloService)context.getBean("helloService");
start = System.currentTimeMillis();
}
public SampleResult runTest(JavaSamplerContext javaSamplerContext) {
SampleResult sr = new SampleResult();
sr.setSamplerData("dubbo测试案例");
sr.sampleStart();// jmeter 开始统计响应时间标记
String result = helloService.speakHello("chen");
System.out.println(result);
if(result.contains("chen")){
sr.setResponseData("结果是:" + result, null);
sr.setDataType(SampleResult.TEXT);
sr.setSuccessful(true);
}else {
sr.setSuccessful(false);
}
sr.sampleEnd();// jmeter 结束统计响应时间标记
return sr;
}
//测试结束时调用;
public void teardownTest(JavaSamplerContext arg0) {
end = System.currentTimeMillis();
// 总体耗时
System.err.println("cost time:" + (end - start) + "毫秒");
}
}
6.编写测试类 , 测试执行完 打包时记得注解
package com.buddo;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
/**
* Created by CWGJ008 on 2017/10/13.
*/
public class TestMain {
public static final void main(String [] args){
JavaSamplerContext arg0 = new JavaSamplerContext(new Arguments());
Dubbo_port test=new Dubbo_port();
test.setupTest(arg0);
test.runTest(arg0);
}
}
点击 OK
点击 OK
Build 文件
第一次 先 Clean
第二次在 Build 生成
9.重新启动 jmeter
10.jmeter GUI 界面使用方式
创建线程组
创建 java 请求
选择 dubbo 测试接口
添加察看树结果
执行脚本
查看最后的结果
到这里表示已执行成功