最近工作中接到一個需求鸭叙,需要對一個Dubbo接口進行壓力測試,測試其性能冤狡,之前一直使用jmeter做壓力測試孙蒙,在踏了好多坑之后项棠,決定把這些記錄下來,順便也希望能幫助到大家挎峦。
開始測試之前香追,我們需要先知道什么是dubbo接口。
一坦胶、Dubbo簡介
dubbo是一個分布式服務(wù)框架透典,致力于提供高性能和透明化的RPC遠(yuǎn)程服務(wù)調(diào)用方案,以及SOA服務(wù)治理方案顿苇。其核心部分包含如下幾點:
1峭咒、遠(yuǎn)程通訊:提供對多種基于長連接的NIO框架抽象封裝,包括多種線程模型纪岁,序列化凑队,以及“請求-響應(yīng)”模式的信息交換方式;
2幔翰、集群容錯:提供基于接口方法的透明遠(yuǎn)程過程調(diào)用漩氨,包括多協(xié)議支持,以及軟負(fù)載均衡遗增,失敗容錯叫惊,地址路由,動態(tài)配置等集群支持做修;
3霍狰、自動發(fā)現(xiàn):基于注冊中心目錄服務(wù),使服務(wù)消費方能動態(tài)的查找服務(wù)提供方饰及,使地址透明蔗坯,使服務(wù)提供方可以平滑增加或減少機器;
4旋炒、dubbo簡化模型
5步悠、Dubbo架構(gòu)
Provider: 暴露服務(wù)的服務(wù)提供方签杈。
Consumer: 調(diào)用遠(yuǎn)程服務(wù)的服務(wù)消費方瘫镇。
Registry: 服務(wù)注冊與發(fā)現(xiàn)的注冊中心仓坞。(常見Zookeeper作為注冊中心)
Monitor: 統(tǒng)計服務(wù)的調(diào)用次數(shù)和調(diào)用時間的監(jiān)控中心生真。
Jmeter本身并不支持Dubbo接口,如果需要測試dubbo接口烘嘱,這里給大家介紹兩種方式鹦付,第一種需要借助第三方插件尚粘,可以從https://github.com/ningyu1/jmeter-plugins-dubbo/tree/master/dist下載,然后將jar包放入${JMETER_HOME}libext路徑下敲长,重啟即可郎嫁。
第二種可以借助腳本方式來實現(xiàn)秉继。咱們著重使用第二種。
首先我們先來用java做一個dubbo接口的sample泽铛,這是參考的dubbo官網(wǎng)的例子(http://dubbo.apache.org/en-us/)
1.我們首先導(dǎo)入POM文件到idea里
<?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.testfan.auto</groupId>
? ?<artifactId>Dubbo</artifactId>
? ?<version>1.0-SNAPSHOT</version>
? ?<properties>
? ? ? ?<dubbo.version>2.5.8</dubbo.version>
? ? ? ?<spring.version>4.3.12.RELEASE</spring.version>
? ?</properties>
? ?<dependencies>
? ? ? ?<dependency>
? ? ? ? ? ?<groupId>com.alibaba</groupId>
? ? ? ? ? ?<artifactId>dubbo</artifactId>
? ? ? ? ? ?<version>${dubbo.version}</version>
? ? ? ?</dependency>
? ? ? ?<dependency>
? ? ? ? ? ?<groupId>org.springframework</groupId>
? ? ? ? ? ?<artifactId>spring-beans</artifactId>
? ? ? ? ? ?<version>${spring.version}</version>
? ? ? ?</dependency>
? ?</dependencies>
</project>
2.接下來我們創(chuàng)建Provider端:
package com.testfan.auto.service;
public interface DemoService {
? ?String sayHello(String name);
}
創(chuàng)建接口的impl類
package com.testfan.auto.service.impl;
import com.testfan.auto.service.DemoService;
public class DemoServiceImpl implements DemoService {
? ?public String sayHello(String name) {
? ? ? ?return "Hello "+name;
? ?}
}
創(chuàng)建xml文件放到resource 下
<?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">
? ?<!--提供方應(yīng)用信息尚辑,用于計算依賴關(guān)系-->
? ?<dubbo:application name="hello-world-app"/>
? ?<!--使用multicast廣播注冊中心暴露服務(wù)地址-->
? ?<dubbo:registry address="multicast://224.5.6.7:1234"/>
? ?<!--使用dubbo協(xié)議在20880端口暴露服務(wù)-->
? ?<dubbo:protocol name="dubbo" port="20880"/>
? ?<!--聲明需要暴露的服務(wù)接口-->
? ?<dubbo:service interface="com.testfan.auto.service.DemoService" ref="demoService"/>
? ?<!--實現(xiàn)需要暴露的服務(wù)-->
? ?<bean id="demoService" class="com.testfan.auto.service.impl.DemoServiceImpl"/>
</beans>
通過使用xml來注冊接口
package com.testfan.auto.provider;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
public class Provider {
? ?public static void main(String []args)
? ?{
? ? ? ?ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:provider.xml");
? ? ? ?context.start();
? ? ? ?try {
? ? ? ? ? ?System.in.read();
? ? ? ?} catch (IOException e) {
? ? ? ? ? ?e.printStackTrace();
? ? ? ?}
? ?}
}
3.接下來我們來創(chuàng)建Consumer端
首先創(chuàng)建一個xml文件放到resources下
<?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">
? ?<!--消費放應(yīng)用名-->
? ?<dubbo:application name="consumer-of-helloworld-app"/>
? ?<!--使用multicast廣播注冊中心暴露發(fā)現(xiàn)服務(wù)地址-->
? ?<dubbo:registry address="multicast://224.5.6.7:1234"/>
? ?<!--生成遠(yuǎn)程服務(wù)代理,可以和本地bean一樣使用demoService-->
? ?<dubbo:reference id="demoService" interface="com.testfan.auto.service.DemoService" />
</beans>
通過使用一份xml配置文件進行測試
package com.testfan.auto.consumer;
import com.testfan.auto.service.DemoService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Consumer {
? ?public static String test(String inString){
? ? ? ?ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("classpath:consumer.xml");
? ? ? ?context.start();
? ? ? ?DemoService demoService= (DemoService) context.getBean("demoService");
? ? ? ?String hello = demoService.sayHello(inString);
? ? ? ?return hello;
? ?}
? ?public static void main(String []args)
? ?{
? ? ? ?String outString = Consumer.test("Chris");
? ? ? ?System.out.println(outString);
? ?}
}
運行的時候先運行Provider盔腔,等待Provider啟動之后杠茬,再啟動Consumer(源碼都可在github上下載https://github.com/chrisblue0605/dubboSample)
好了,我們以及用java實現(xiàn)了dubbo接口測試弛随,接下來我們將java實現(xiàn)dubbo接口測試與jmeter集成起來瓢喉。
二、Jmeter集成
在idea的右邊有一個maven窗口->Lifecycle->雙擊package舀透,將項目打包栓票,在target文件夾下面,會生成項目特有的jar文件
從jmeter官網(wǎng)(https://jmeter.apache.org/download_jmeter.cgi)下載jmeter運行文件愕够。
將打包生成的jar包以及項目所需要的所有jar包都放到j(luò)meter_homelibext下(所有jar包也上傳GitHub)
打開Jmeter之后逗载,新建線程組,在線程組里新建beanshell sample
import com.testfan.auto.consumer.Consumer;
String inString = "Chris";
String outString = Consumer.test(inString);
vars.put("outString", outString);
在保證Provider運行的前提下链烈,運行jmeter腳本
現(xiàn)在我們以及將dubbo接口與jmeter集成起來厉斟,分布式如何運行,未完待續(xù)...
作 者:Testfan Chris
出 處:微信公眾號:自動化軟件測試平臺
版權(quán)說明:歡迎轉(zhuǎn)載强衡,但必須注明出處擦秽,并在文章頁面明顯位置給出文章鏈接