Jmeter分布式測(cè)試dubbo接口1

最近工作中接到一個(gè)需求,需要對(duì)一個(gè)Dubbo接口進(jìn)行壓力測(cè)試,測(cè)試其性能,之前一直使用jmeter做壓力測(cè)試,在踏了好多坑之后,決定把這些記錄下來(lái),順便也希望能幫助到大家。

開(kāi)始測(cè)試之前,我們需要先知道什么是dubbo接口。

一、Dubbo簡(jiǎn)介

dubbo是一個(gè)分布式服務(wù)框架,致力于提供高性能和透明化的RPC遠(yuǎn)程服務(wù)調(diào)用方案,以及SOA服務(wù)治理方案。其核心部分包含如下幾點(diǎn):

1、遠(yuǎn)程通訊:提供對(duì)多種基于長(zhǎng)連接的NIO框架抽象封裝,包括多種線(xiàn)程模型,序列化,以及“請(qǐng)求-響應(yīng)”模式的信息交換方式;

2、集群容錯(cuò):提供基于接口方法的透明遠(yuǎn)程過(guò)程調(diào)用,包括多協(xié)議支持,以及軟負(fù)載均衡,失敗容錯(cuò),地址路由,動(dòng)態(tài)配置等集群支持;

3、自動(dòng)發(fā)現(xiàn):基于注冊(cè)中心目錄服務(wù),使服務(wù)消費(fèi)方能動(dòng)態(tài)的查找服務(wù)提供方,使地址透明,使服務(wù)提供方可以平滑增加或減少機(jī)器;

4、dubbo簡(jiǎn)化模型

5、Dubbo架構(gòu)

Provider: 暴露服務(wù)的服務(wù)提供方。

Consumer: 調(diào)用遠(yuǎn)程服務(wù)的服務(wù)消費(fèi)方。

Registry: 服務(wù)注冊(cè)與發(fā)現(xiàn)的注冊(cè)中心。(常見(jiàn)Zookeeper作為注冊(cè)中心)

Monitor: 統(tǒng)計(jì)服務(wù)的調(diào)用次數(shù)和調(diào)用時(shí)間的監(jiān)控中心。

Jmeter本身并不支持Dubbo接口,如果需要測(cè)試dubbo接口,這里給大家介紹兩種方式,第一種需要借助第三方插件,可以從https://github.com/ningyu1/jmeter-plugins-dubbo/tree/master/dist下載,然后將jar包放入${JMETER_HOME}libext路徑下,重啟即可。

第二種可以借助腳本方式來(lái)實(shí)現(xiàn)。咱們著重使用第二種。

首先我們先來(lái)用java做一個(gè)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.接下來(lái)我們創(chuàng)建Provider端:

package com.testfan.auto.service;

public interface DemoService {

? ?String sayHello(String name);

}

創(chuàng)建接口的impl類(lèi)

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)用信息,用于計(jì)算依賴(lài)關(guān)系-->

? ?<dubbo:application name="hello-world-app"/>

? ?<!--使用multicast廣播注冊(cè)中心暴露服務(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"/>

? ?<!--實(shí)現(xiàn)需要暴露的服務(wù)-->

? ?<bean id="demoService" class="com.testfan.auto.service.impl.DemoServiceImpl"/>

</beans>

通過(guò)使用xml來(lái)注冊(cè)接口

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.接下來(lái)我們來(lái)創(chuàng)建Consumer端

首先創(chuàng)建一個(gè)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">

? ?<!--消費(fèi)放應(yīng)用名-->

? ?<dubbo:application name="consumer-of-helloworld-app"/>

? ?<!--使用multicast廣播注冊(cè)中心暴露發(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>

通過(guò)使用一份xml配置文件進(jìn)行測(cè)試

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);

? ?}

}

運(yùn)行的時(shí)候先運(yùn)行Provider,等待Provider啟動(dòng)之后,再啟動(dòng)Consumer(源碼都可在github上下載https://github.com/chrisblue0605/dubboSample)

好了,我們以及用java實(shí)現(xiàn)了dubbo接口測(cè)試,接下來(lái)我們將java實(shí)現(xiàn)dubbo接口測(cè)試與jmeter集成起來(lái)。

二、Jmeter集成

在idea的右邊有一個(gè)maven窗口->Lifecycle->雙擊package,將項(xiàng)目打包,在target文件夾下面,會(huì)生成項(xiàng)目特有的jar文件

從jmeter官網(wǎng)(https://jmeter.apache.org/download_jmeter.cgi)下載jmeter運(yùn)行文件。

將打包生成的jar包以及項(xiàng)目所需要的所有jar包都放到j(luò)meter_homelibext下(所有jar包也上傳GitHub)

打開(kāi)Jmeter之后,新建線(xiàn)程組,在線(xiàn)程組里新建beanshell sample

import com.testfan.auto.consumer.Consumer;

String inString = "Chris";

String outString = Consumer.test(inString);

vars.put("outString", outString);

在保證Provider運(yùn)行的前提下,運(yùn)行jmeter腳本

現(xiàn)在我們以及將dubbo接口與jmeter集成起來(lái),分布式如何運(yùn)行,未完待續(xù)...

作  者:Testfan Chris

出  處:微信公眾號(hào):自動(dòng)化軟件測(cè)試平臺(tái)

版權(quán)說(shuō)明:歡迎轉(zhuǎn)載,但必須注明出處,并在文章頁(yè)面明顯位置給出文章鏈接

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容