Jmeter測試dubbo接口【轉】

本文講解jmeter測試dubbo接口的實現方式,文章以一個dubbo的接口爲例子進行講解,該dubbo接口實現的功能爲:java

 

  • 一:首先咱們看服務端代碼

代碼架構爲:git

 

1:新建一個maven工程,pom文件爲:github

複製代碼

1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 2     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 3     <modelVersion>4.0.0</modelVersion>
 4 
 5     <groupId>com.ustc.demo</groupId>
 6     <artifactId>dubbo-provider</artifactId>
 7     <version>0.0.1-SNAPSHOT</version>
 8     <packaging>jar</packaging>
 9 
10     <name>dubbo-provider</name>
11     <url>http://maven.apache.org</url>
12 
13     <properties>
14         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15     </properties>
16 
17     <dependencies>
18         <dependency>
19             <groupId>junit</groupId>
20             <artifactId>junit</artifactId>
21             <version>3.8.1</version>
22             <scope>test</scope>
23         </dependency>
24         <dependency>
25             <groupId>com.alibaba</groupId>
26             <artifactId>dubbo</artifactId>
27             <version>2.4.9</version>
28         </dependency>
29         <dependency>
30             <groupId>com.github.sgroschupf</groupId>
31             <artifactId>zkclient</artifactId>
32             <version>0.1</version>
33         </dependency>
34     </dependencies>
35     <build>
36         <plugins>
37             <plugin>
38                 <artifactId>maven-dependency-plugin</artifactId>
39                 <executions>
40                     <execution>
41                         <id>unpack</id>
42                         <phase>package</phase>
43                         <goals>
44                             <goal>unpack</goal>
45                         </goals>
46                         <configuration>
47                             <artifactItems>
48                                 <artifactItem>
49                                     <groupId>com.alibaba</groupId>
50                                     <artifactId>dubbo</artifactId>
51                                     <version>${project.parent.version}</version>
52                                     <outputDirectory>${project.build.directory}/dubbo</outputDirectory>
53                                     <includes>META-INF/assembly/**</includes>
54                                 </artifactItem>
55                             </artifactItems>
56                         </configuration>
57                     </execution>
58                 </executions>
59             </plugin>
60             <plugin>
61                 <artifactId>maven-assembly-plugin</artifactId>
62                 <configuration>
63                     <descriptor>src/main/assembly/assembly.xml</descriptor>
64                 </configuration>
65                 <executions>
66                     <execution>
67                         <id>make-assembly</id>
68                         <phase>package</phase>
69                         <goals>
70                             <goal>single</goal>
71                         </goals>
72                     </execution>
73                 </executions>
74             </plugin>
75         </plugins>
76     </build>
77 </project>

複製代碼

2:在src/main下新建文件夾assembly,而後在assembly文件夾下新建assembly.xml文件redis

複製代碼

1 <assembly>
 2     <id>assembly</id>
 3     <formats>
 4         <format>tar.gz</format>
 5     </formats>
 6     <includeBaseDirectory>true</includeBaseDirectory>
 7     <fileSets>
 8         <fileSet>
 9             <directory>${project.build.directory}/dubbo/META-INF/assembly/bin
10             </directory>
11             <outputDirectory>bin</outputDirectory>
12             <fileMode>0755</fileMode>
13         </fileSet>
14         <fileSet>
15             <directory>src/main/assembly/conf</directory>
16             <outputDirectory>conf</outputDirectory>
17             <fileMode>0644</fileMode>
18         </fileSet>
19     </fileSets>
20     <dependencySets>
21         <dependencySet>
22             <outputDirectory>lib</outputDirectory>
23         </dependencySet>
24     </dependencySets>
25 </assembly>

複製代碼

3:在src/main/assembly文件夾下新建conf文件夾,而後在conf文件夾下新建dubbo.properties文件,此處的zookeeper的地址根據實際進行修改spring

複製代碼

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文件內容相同apache

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 <?xml version="1.0" encoding="UTF-8" ?> 
2 <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">
3 <bean id="demoService" class="com.ustc.demo.provider.DemoServiceImpl" /> 
4 <dubbo:service interface="com.ustc.demo.provider.DemoService" ref="demoService"/> 
5 </beans>

複製代碼

8:編寫main方法,新建DemoServiceMain.java類app

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 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 2     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 3     <modelVersion>4.0.0</modelVersion>
 4     <groupId>com.ustc.demo</groupId>
 5     <artifactId>consumer</artifactId>
 6     <version>0.0.1-SNAPSHOT</version>
 7     <packaging>jar</packaging>
 8     <name>consumer</name>
 9     <url>http://maven.apache.org</url>
10     <properties>
11         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
12     </properties>
13     <dependencies>
14         <dependency>
15             <groupId>junit</groupId>
16             <artifactId>junit</artifactId>
17             <version>3.8.1</version>
18             <scope>test</scope>
19         </dependency>
20         <dependency>
21             <groupId>com.alibaba</groupId>
22             <artifactId>dubbo</artifactId>
23             <version>2.4.9</version>
24         </dependency>
25         <dependency>
26             <groupId>com.github.sgroschupf</groupId>
27             <artifactId>zkclient</artifactId>
28             <version>0.1</version>
29         </dependency>
30     </dependencies>
31     <build>
32         <plugins>
33             <plugin>
34                 <artifactId>maven-dependency-plugin</artifactId>
35                 <executions>
36                     <execution>
37                         <id>unpack</id>
38                         <phase>package</phase>
39                         <goals>
40                             <goal>unpack</goal>
41                         </goals>
42                         <configuration>
43                             <artifactItems>
44                                 <artifactItem>
45                                     <groupId>com.alibaba</groupId>
46                                     <artifactId>dubbo</artifactId>
47                                     <version>${project.parent.version}</version>
48                                     <outputDirectory>${project.build.directory}/dubbo</outputDirectory>
49                                     <includes>META-INF/assembly/**</includes>
50                                 </artifactItem>
51                             </artifactItems>
52                         </configuration>
53                     </execution>
54                 </executions>
55             </plugin>
56             <plugin>
57                 <artifactId>maven-assembly-plugin</artifactId>
58                 <configuration>
59                     <descriptor>src/main/assembly/assembly.xml</descriptor>
60                 </configuration>
61                 <executions>
62                     <execution>
63                         <id>make-assembly</id>
64                         <phase>package</phase>
65                         <goals>
66                             <goal>single</goal>
67                         </goals>
68                     </execution>
69                 </executions>
70             </plugin>
71         </plugins>
72     </build>
73 </project>

複製代碼

2:在src/main下新建文件夾assembly,而後在assembly文件夾下新建assembly.xml文件

複製代碼

1 <assembly>
 2     <id>assembly</id>
 3     <formats>
 4         <format>tar.gz</format>
 5     </formats>
 6     <includeBaseDirectory>true</includeBaseDirectory>
 7     <fileSets>
 8         <fileSet>
 9             <directory>${project.build.directory}/dubbo/META-INF/assembly/bin
10             </directory>
11             <outputDirectory>bin</outputDirectory>
12             <fileMode>0755</fileMode>
13         </fileSet>
14         <fileSet>
15             <directory>src/main/assembly/conf</directory>
16             <outputDirectory>conf</outputDirectory>
17             <fileMode>0644</fileMode>
18         </fileSet>
19     </fileSets>
20     <dependencySets>
21         <dependencySet>
22             <outputDirectory>lib</outputDirectory>
23         </dependencySet>
24     </dependencySets>
25 </assembly>

複製代碼

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 <?xml version="1.0" encoding="UTF-8" ?> 
2 <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-2.5.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
3  <bean class="com.ustc.demo.consumer.DemoAction" init-method="start">
4   <property name="demoService" ref="demoService" /> 
5   </bean>
6 </beans>

複製代碼

8:編寫spring的配置文件,在META-INF/spring文件夾下的dubbo-demo-consumer.xml文件

複製代碼

1 <?xml version="1.0" encoding="UTF-8" ?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
4     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
5     <dubbo:reference id="demoService"
6         interface="com.ustc.demo.provider.DemoService" />
7 </beans>

複製代碼

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接口的功能正常實現

 

  • 三:咱們如今想對dubbo接口進行性能測試,能夠用jmeter模擬服務消費方併發調用服務提供方

由於jmeter支持java請求,故咱們能夠將服務提供方打包部署到服務器上運行,將服務消費方打成jar包放到jmeter的/lib/ext文件夾中,這樣就能實現jmeter模擬消費方去請求服務端,進行性能測試

如今咱們來說解如何將上面的服務消費端的代碼編寫成能夠打包放到jmeter中的jar包代碼

只須要對上面的消費者代碼進行3處修改便可:

1:pom.xml文件中添加對jmeter的支持,在<dependencies></dependencies>之間添加以下代碼

複製代碼

1 <!-- java jmeter依賴jar包 -->
 2          <dependency>
 3              <groupId>org.apache.jmeter</groupId>
 4              <artifactId>ApacheJMeter_core</artifactId>
 5              <version>3.0</version>
 6          </dependency>
 7          <dependency>
 8              <groupId>org.apache.jmeter</groupId>
 9              <artifactId>ApacheJMeter_java</artifactId>
10              <version>3.0</version>
11          </dependency>

複製代碼

2:在src/main/resources下新建applicationConsumer.xml文件,zookeeper地址根據須要進行修改

複製代碼

1 <?xml version="1.0" encoding="UTF-8"?>  
 2 <beans xmlns="http://www.springframework.org/schema/beans"  
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"  
 4     xsi:schemaLocation="http://www.springframework.org/schema/beans  
 5         http://www.springframework.org/schema/beans/spring-beans.xsd  
 6         http://code.alibabatech.com/schema/dubbo  
 7         http://code.alibabatech.com/schema/dubbo/dubbo.xsd  
 8         ">  
 9   
10     <!-- consumer application name -->  
11     <dubbo:application name="consumer-jmeter" />  
12     <!-- registry address, used for consumer to discover services -->  
13     <dubbo:registry address="zookeeper://134.64.xx.xx:2181" />  
14     <!-- which service to consume? -->  
15     <dubbo:reference id="demoService" interface="com.ustc.demo.provider.DemoService" />  
16 </beans>

複製代碼

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 <br/> 
12  * Function: TODO ADD FUNCTION. <br/> 
13  * Reason:   TODO ADD REASON. <br/> 
14  * Date:     2016年12月3日 下午10:12:10 <br/> 
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的消費端代碼編寫

 

  • 四:將消費端和服務端打包maven install,打包完成後能夠看到消費端的target下生成了兩個文件一個consumer-0.0.1-SNAPSHOT-assembly.tar.gz還有一個consumer-0.0.1-SNAPSHOT.jar

將consumer-0.0.1-SNAPSHOT-assembly.tar.gz中的lib文件夾下全部的jar包拷貝到jmeter的lib目錄下,若是有重複的,則不替換用jmeter原生的jar包

將consumer-0.0.1-SNAPSHOT.jar拷貝到jmeter的lib/ext目錄下

 

  • 五:部署服務端,而後消費端爲:啓動jmeter,驗證該jar功能是否正常,新建一個java請求

啓動服務端:

新建消費端的java請求:

啓動消費端,發現響應結果中返回了服務端的響應:

在看服務端的日誌:

上圖爲兩個線程測試了一下,發現調用經過成功,響應數據正常返回,故該腳本能夠正常使用,至此jmeter測試dubbo接口整個的腳本製做過程就講完了

至於如何用這個jmx腳本作性能測試,我就不在重複了,參考個人博文-jmeter命令行運行-單節點測試或者分佈式測試

最後給出工程源碼,也就是上面的例子的源代碼jmeter測試dubbo接口:dubbor.rar中包含兩個maven工程,dubbo-consumer和dubbo-provider

相關文章
相關標籤/搜索