本文讲解jmeter测试dubbo接口的实现方式,文章以一个dubbo的接口为例子进行讲解,该dubbo接口实现的功能为:
代码架构为:
1:新建一个maven工程,pom文件为:
1
3 4.0.0
4
5 com.ustc.demo
6 dubbo-provider
7 0.0.1-SNAPSHOT
8 jar
9
10 dubbo-provider
11 http://maven.apache.org
12
13
14 UTF-8
15
16
17
18
19 junit
20 junit
21 3.8.1
22 test
23
24
25 com.alibaba
26 dubbo
27 2.4.9
28
29
30 com.github.sgroschupf
31 zkclient
32 0.1
33
34
35
36
37
38 maven-dependency-plugin
39
40
41 unpack
42 package
43
44 unpack
45
46
47
48
49 com.alibaba
50 dubbo
51 ${project.parent.version}
52 ${project.build.directory}/dubbo
53 META-INF/assembly/**
54
55
56
57
58
59
60
61 maven-assembly-plugin
62
63 src/main/assembly/assembly.xml
64
65
66
67 make-assembly
68 package
69
70 single
71
72
73
74
75
76
77
2:在src/main下新建文件夹assembly,然后在assembly文件夹下新建assembly.xml文件
1
2 assembly
3
4 tar.gz
5
6 true
7
8
9 ${project.build.directory}/dubbo/META-INF/assembly/bin
10
11 bin
12 0755
13
14
15 src/main/assembly/conf
16 conf
17 0644
18
19
20
21
22 lib
23
24
25
3:在src/main/assembly文件夹下新建conf文件夹,然后在conf文件夹下新建dubbo.properties文件,此处的zookeeper的地址根据实际进行修改
1 dubbo.container=log4j,spring
2 dubbo.application.name=demo-caiya
3 dubbo.application.owner=william
4 #dubbo.registry.address=multicast://224.5.x.7:1234
5 dubbo.registry.address=zookeeper://134.xx.xx.xx:2181
6 #dubbo.registry.address=redis://127.0.0.1:6379
7 #dubbo.registry.address=dubbo://127.0.0.1:9090
8 #dubbo.monitor.protocol=registry
9 dubbo.protocol.name=dubbo
10 dubbo.protocol.port=20880
11 #dubbo.service.loadbalance=roundrobin
12 #dubbo.log4j.file=logs/dubbo-demo-consumer.log
13 #dubbo.log4j.level=WARN
4:在src/test/resources包路径下,新建dubbo.properties文件,内容和上面的3中dubbo.properties文件内容相同
5:编写provider的接口sayHello,新建DemoService.java类
1 package com.ustc.demo.provider; 2 public interface DemoService { 3 public String sayHello(String name); 4 }
6:编写sayHello接口的实现类,新建DemoServiceImpl.java类
package com.ustc.demo.provider;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DemoServiceImpl implements DemoService{
public String sayHello(String name) {
String time = new SimpleDateFormat("HH:mm:ss").format(new Date());
System.out.println("from consumer:"+name);
return "The current time is:"+time;
}
7:编写spring的配置文件,在META-INF/spring文件夹下的demo-provider.xml文件
1
2
3
4
5
8:编写main方法,新建DemoServiceMain.java类
1 package com.ustc.demo.provider;
2 public class DemoServiceMain {
3 public static void main(String[] args) {
4 com.alibaba.dubbo.container.Main.main(args);
5 }
6 }
这样服务端的代码就写好了,实现的功能是当消费者来询问当前时间是几点的时候,返回当前时间
1:新建一个maven工程,pom文件为:
1
3 4.0.0
4 com.ustc.demo
5 consumer
6 0.0.1-SNAPSHOT
7 jar
8 consumer
9 http://maven.apache.org
10
11 UTF-8
12
13
14
15 junit
16 junit
17 3.8.1
18 test
19
20
21 com.alibaba
22 dubbo
23 2.4.9
24
25
26 com.github.sgroschupf
27 zkclient
28 0.1
29
30
31
32
33
34 maven-dependency-plugin
35
36
37 unpack
38 package
39
40 unpack
41
42
43
44
45 com.alibaba
46 dubbo
47 ${project.parent.version}
48 ${project.build.directory}/dubbo
49 META-INF/assembly/**
50
51
52
53
54
55
56
57 maven-assembly-plugin
58
59 src/main/assembly/assembly.xml
60
61
62
63 make-assembly
64 package
65
66 single
67
68
69
70
71
72
73
2:在src/main下新建文件夹assembly,然后在assembly文件夹下新建assembly.xml文件
1
2 assembly
3
4 tar.gz
5
6 true
7
8
9 ${project.build.directory}/dubbo/META-INF/assembly/bin
10
11 bin
12 0755
13
14
15 src/main/assembly/conf
16 conf
17 0644
18
19
20
21
22 lib
23
24
25
3:在src/main/assembly文件夹下新建conf文件夹,然后在conf文件夹下新建dubbo.properties文件,此处的zookeeper的地址根据实际进行修改
1 dubbo.container=log4j,spring
2 dubbo.application.name=demo-consumer
3 dubbo.application.owner=
4 #dubbo.registry.address=multicast://224.5.6.7:1234
5 dubbo.registry.address=zookeeper://134.64.xx.xx:2181
6 #dubbo.registry.address=redis://127.0.0.1:6379
7 #dubbo.registry.address=dubbo://127.0.0.1:9090
8 dubbo.monitor.protocol=registry
9 dubbo.log4j.file=logs/dubbo-demo-consumer.log
10 dubbo.log4j.level=WARN
4:在src/test/resources包路径下,新建dubbo.properties文件,内容和上面的3中dubbo.properties文件内容相同
5:编写provider的接口sayHello,新建DemoService.java类
1 package com.ustc.demo.provider; 2 3 public interface DemoService { 4 public String sayHello(String name); 5 }
6:编写消费端请求类调用sayHello方法,新建DemoAction.java类
1 package com.ustc.demo.consumer;
2 import com.ustc.demo.provider.DemoService;
3 public class DemoAction {
4
5 private DemoService demoService;
6
7 public void setDemoService(DemoService demoService) {
8 this.demoService = demoService;
9 }
10
11 public void start() throws Exception {
12 for (int i = 0; i < Integer.MAX_VALUE; i ++) {
13 try {
14 String hello = demoService.sayHello("hello,How much is the current time?");
15 System.out.println("from provider:"+hello);
16 } catch (Exception e) {
17 e.printStackTrace();
18 }
19 Thread.sleep(2000);
20 }
21 }
22 }
7:编写spring的配置文件,在META-INF/spring文件夹下的dubbo-demo-action.xml文件
1
2
3
4
5
6
8:编写spring的配置文件,在META-INF/spring文件夹下的dubbo-demo-consumer.xml文件
1
2
5
7
9:编写main方法,新建DemoServiceMain.java类
1 package com.ustc.demo.consumer;
2 public class DemoConsumerMain {
3 public static void main(String[] args) {
4 com.alibaba.dubbo.container.Main.main(args);
5 }
6 }
这样我们就完成了本地消费者代码,在编写符合jmeter格式的代码前,我们首先在本地开发工具中运行看看效果:
启动服务提供方的main方法,然后启动服务消费方的main方法:
服务消费者控制台:
服务提供者控制台:
这样调试发现消费端向服务端发送:How much is the current time?,然后服务端返回当前的时间,该dubbo接口的功能正常实现
因为jmeter支持java请求,故我们可以将服务提供方打包部署到服务器上运行,将服务消费方打成jar包放到jmeter的/lib/ext文件夹中,这样就能实现jmeter模拟消费方去请求服务端,进行性能测试
现在我们来讲解如何将上面的服务消费端的代码编写成可以打包放到jmeter中的jar包代码
只需要对上面的消费者代码进行3处修改即可:
1:pom.xml文件中添加对jmeter的支持,在
1
2
3 org.apache.jmeter
4 ApacheJMeter_core
5 3.0
6
7
8 org.apache.jmeter
9 ApacheJMeter_java
10 3.0
11
2:在src/main/resources下新建applicationConsumer.xml文件,zookeeper地址根据需要进行修改
1
2
9
10
11
12
13
14
15
16
3:在com.ustc.demo.consumer包下新建JmeDemoAction.java类
1 package com.ustc.demo.consumer;
2
3 import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
4 import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
5 import org.apache.jmeter.samplers.SampleResult;
6 import org.springframework.context.support.ClassPathXmlApplicationContext;
7
8 import com.ustc.demo.provider.DemoService;
9
10 /**
11 * ClassName:JmeDemoAction
12 * Function: TODO ADD FUNCTION.
13 * Reason: TODO ADD REASON.
14 * Date: 2016年12月3日 下午10:12:10
15 * @author meiling.yu
16 * @version
17 * @since JDK 1.7
18 * @see
19 */
20 public class JmeDemoAction extends AbstractJavaSamplerClient{
21 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
22 new String[] { "applicationConsumer.xml" });
23
24 public SampleResult runTest(JavaSamplerContext arg0) {
25 SampleResult sr = new SampleResult();
26 try {
27 sr.sampleStart();
28 context.start();
29 DemoService demoService = (DemoService) context.getBean("demoService");
30 String hello = demoService.sayHello("hello,How much is the current time?");
31 sr.setResponseData("from provider:"+hello, null);
32 sr.setDataType(SampleResult.TEXT);
33 sr.setSuccessful(true);
34 sr.sampleEnd();
35 } catch (Exception e) {
36 e.printStackTrace();
37 }
38 return sr;
39 }
40
41 }
这样就完成了jmeter的消费端代码编写
将consumer-0.0.1-SNAPSHOT-assembly.tar.gz中的lib文件夹下所有的jar包拷贝到jmeter的lib目录下,如果有重复的,则不替换用jmeter原生的jar包
将consumer-0.0.1-SNAPSHOT.jar拷贝到jmeter的lib/ext目录下
启动服务端:
新建消费端的java请求:
启动消费端,发现响应结果中返回了服务端的响应:
在看服务端的日志:
上图为两个线程测试了一下,发现调用通过成功,响应数据正常返回,故该脚本可以正常使用,至此jmeter测试dubbo接口整个的脚本制作过程就讲完了
至于如何用这个jmx脚本做性能测试,我就不在重复了,参考我的博文-jmeter命令行运行-单节点测试或者分布式测试
最后给出工程源码,也就是上面的例子的源代码jmeter测试dubbo接口:dubbor.rar中包含两个maven工程,dubbo-consumer和dubbo-provider
2023最新Jmeter接口测试从入门到精通(全套项目实战教程)