Jmeter分布式测试dubbo接口1

最近工作中接到一个需求,需要对一个Dubbo接口进行压力测试,测试其性能,之前一直使用jmeter做压力测试,在踏了好多坑之后,决定把这些记录下来,顺便也希望能帮助到大家。

开始测试之前,我们需要先知道什么是dubbo接口。

一、Dubbo简介

dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案。其核心部分包含如下几点:

1、远程通讯:提供对多种基于长连接的NIO框架抽象封装,包括多种线程模型,序列化,以及“请求-响应”模式的信息交换方式;

2、集群容错:提供基于接口方法的透明远程过程调用,包括多协议支持,以及软负载均衡,失败容错,地址路由,动态配置等集群支持;

3、自动发现:基于注册中心目录服务,使服务消费方能动态的查找服务提供方,使地址透明,使服务提供方可以平滑增加或减少机器;

4、dubbo简化模型

5、Dubbo架构

Provider: 暴露服务的服务提供方。

Consumer: 调用远程服务的服务消费方。

Registry: 服务注册与发现的注册中心。(常见Zookeeper作为注册中心)

Monitor: 统计服务的调用次数和调用时间的监控中心。

Jmeter本身并不支持Dubbo接口,如果需要测试dubbo接口,这里给大家介绍两种方式,第一种需要借助第三方插件,可以从https://github.com/ningyu1/jmeter-plugins-dubbo/tree/master/dist下载,然后将jar包放入${JMETER_HOME}libext路径下,重启即可。

第二种可以借助脚本方式来实现。咱们着重使用第二种。

首先我们先来用java做一个dubbo接口的sample,这是参考的dubbo官网的例子(http://dubbo.apache.org/en-us/)

1.我们首先导入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.接下来我们创建Provider端:

package com.testfan.auto.service;

public interface DemoService {

String sayHello(String name);

}

创建接口的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;

}

}

创建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">

<!--提供方应用信息,用于计算依赖关系-->

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

<!--使用multicast广播注册中心暴露服务地址-->

<dubbo:registry address="multicast://224.5.6.7:1234"/>

<!--使用dubbo协议在20880端口暴露服务-->

<dubbo:protocol name="dubbo" port="20880"/>

<!--声明需要暴露的服务接口-->

<dubbo:service interface="com.testfan.auto.service.DemoService" ref="demoService"/>

<!--实现需要暴露的服务-->

<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.接下来我们来创建Consumer端

首先创建一个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">

<!--消费放应用名-->

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

<!--使用multicast广播注册中心暴露发现服务地址-->

<dubbo:registry address="multicast://224.5.6.7:1234"/>

<!--生成远程服务代理,可以和本地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实现了dubbo接口测试,接下来我们将java实现dubbo接口测试与jmeter集成起来。

二、Jmeter集成

在idea的右边有一个maven窗口->Lifecycle->双击package,将项目打包,在target文件夹下面,会生成项目特有的jar文件

从jmeter官网(https://jmeter.apache.org/download_jmeter.cgi)下载jmeter运行文件。

将打包生成的jar包以及项目所需要的所有jar包都放到jmeter_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脚本

现在我们以及将dubbo接口与jmeter集成起来,分布式如何运行,未完待续...

作 者:Testfan Chris

出 处:微信公众号:自动化软件测试平台

版权说明:欢迎转载,但必须注明出处,并在文章页面明显位置给出文章链接

发表评论
留言与评论(共有 0 条评论)
   
验证码:

相关文章

推荐文章

'); })();