源码下载:点此下载
1.创建一个Maven工程
pom.xml
application.properties
报如下异常:
Caused by: java.lang.ClassNotFoundException: org.w3c.dom.ElementTraversal
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 41 more
解决方法: 上面的pom.xml中已经有添加
再次执行报以下异常
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console. Set system property 'org.apache.logging.log4j.simplelog.StatusLogger.level' to TRACE to show Log4j2 internal initialization logging.
解决方法: 在classpath下添加log4j2.xml,内容参考:http://blog.csdn.net/qq_34021712/article/details/78870407
TestDubbo.zip 和 TestDubbo.jar 其中TestDubbo.zip中也有包含TestDubbo.jar 还包含有lib文件,lib中有我们需要的jar包
然后将解压TestDubbo.zip中的lib包改名为lib-dependency 拷贝到jmeter的lib下 (lib-dependency名字可以随便起)
修改jmeter.properties 中的 search_paths,配置相关的依赖的jar的路径
启动jmeter测试
使用jmeter测试dubbo接口 和使用 jmeter测试 java请求是类似的
jmeter测试java接口:http://blog.csdn.net/qq_34021712/article/details/78870407
1.创建一个Maven工程
pom.xml
<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.test.jmeter</groupId>
<artifactId>TestDubbo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<!--项目依赖的版本号统一管理-->
<properties>
<!--dubbo rest-->
<dubbo.version>2.8.4</dubbo.version>
<!--spring-->
<spring.version>4.3.10.RELEASE</spring.version>
<!--zookeeper-->
<zookeeper.version>3.4.6</zookeeper.version>
<zkclient.version>0.10</zkclient.version>
<!-- 阿里FastJson-->
<fastjson.version>1.2.36</fastjson.version>
</properties>
<dependencies>
<dependency>
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
<version>1.4.01</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- zookeeper注册中心 -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>${zookeeper.version}</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.jmeter</groupId>
<artifactId>ApacheJMeter_core</artifactId>
<version>3.3</version>
<exclusions>
<exclusion>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
</exclusion>
<exclusion>
<groupId>commons-math3</groupId>
<artifactId>commons-math3</artifactId>
</exclusion>
<exclusion>
<groupId>commons-pool2</groupId>
<artifactId>commons-pool2</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.jmeter</groupId>
<artifactId>ApacheJMeter_java</artifactId>
<version>3.3</version>
<exclusions>
<exclusion>
<groupId>commons-math3</groupId>
<artifactId>commons-math3</artifactId>
</exclusion>
<exclusion>
<groupId>commons-pool2</groupId>
<artifactId>commons-pool2</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 整合dubbox -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.curator</groupId>
<artifactId>curator-client</artifactId>
</exclusion>
<exclusion>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- https://mvnrepository.com/artifact/com.101tec/zkclient -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>${zkclient.version}</version>
</dependency>
<!-- 引入 要测试的dubbo 服务的PAI-->
<dependency>
<groupId>com.test.dubbo</groupId>
<artifactId>MISS.UserCenter.API</artifactId>
<version>1.0-RELEASE</version>
</dependency>
<!--spring核心pom依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>${spring.version}</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Spring AOP -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
</dependencies>
<build>
<!--打包后的项目名称 -->
<finalName>TestDubbo</finalName>
<plugins>
<!-- java编译插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- 打jar包的插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib</classpathPrefix>
<!-- 程序启动入口 -->
<mainClass>com.test.jmeter.Application</mainClass>
</manifest>
<manifestEntries>
<!-- 将lib包抽到上一层文件夹中, classpathPrefix属性是包名-->
<Class-Path>./</Class-Path>
</manifestEntries>
</archive>
<excludes>
<!-- 将config/**抽离出来 -->
<exclude>config/**</exclude>
<exclude>spring/**</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<!-- not append assembly id in release file name -->
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<!-- 注意这里的路径 -->
<descriptor>src/main/build/package.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
以上pom文件配置都是一些基本的配置,还需要自己添加要测试的接口的依赖的jar包。
dubbo-consumer.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 加载配置文件 -->
<context:property-placeholder location="classpath:config/*.properties" />
<dubbo:application name="${dubbo.application.name}"/>
<!-- 使用zookeeper注册中心暴露服务地址,使用多个注册中心 -->
<dubbo:registry protocol="zookeeper" address="${dubbo.zookeeper.address}" />
<!-- 配置全局超时时间 -->
<dubbo:consumer timeout="${dubbo.consumer.timeout}"/>
<!-- 要测试的dubbo接口 -->
<dubbo:reference interface="com.service.UserInfoCenterService"
id="userInfoCenterService"
protocol="dubbo"
version="1.0"
check="false"/>
</beans>
application.properties
#zk配置中心
dubbo.zookeeper.address=127.0.0.1:2181
dubbo.application.name=TestDubbo
dubbo.consumer.timeout=6000
以上配置完毕后,开始写代码,需要继承Jmeter的AbstractJavaSamplerClient 类,并实现runTest方法
DubboTest.javapackage com.test.jmeter;
import com.ruubypay.miss.returntype.ModelsReturn;
import com.ruubypay.miss.usercenter.interfaces.models.ModelsGetBindingUserRealNameInfoRequest;
import com.ruubypay.miss.usercenter.interfaces.models.ModelsGetBindingUserRealNameInfoResponse;
import com.ruubypay.miss.usercenter.interfaces.service.UserInfoCenterService;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author: wangsaichao
* @date: 2017/12/22
* @description:
*/
public class DubboTest extends AbstractJavaSamplerClient {
private static final Logger logger = LoggerFactory.getLogger(DubboTest.class);
private static final ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/*.xml");
private static UserInfoCenterService userInfoCenterService;
/**
* 这个方法是用来自定义java方法入参的
* params.addArgument("num1","");表示入参名字叫num1,默认值为空。
* @return
*/
@Override
public Arguments getDefaultParameters() {
Arguments params = new Arguments();
//id
params.addArgument("userID", "");
//手机号
params.addArgument("phoneNum", "");
return params;
}
/**
* 每个线程测试前执行一次,做一些初始化工作
* 获取输入的参数,赋值给变量
* @param arg0
*/
@Override
public void setupTest(JavaSamplerContext arg0) {
userInfoCenterService = (UserInfoCenterService)context.getBean("userInfoCenterService");
}
/**
* 真正执行逻辑的方法
* @param javaSamplerContext
* @return
*/
@Override
public SampleResult runTest(JavaSamplerContext javaSamplerContext) {
SampleResult sr = new SampleResult();
//获取之前的请求参数
String userID = javaSamplerContext.getParameter("userID");
String phoneNum = javaSamplerContext.getParameter("phoneNum");
sr.setSamplerData("请求参数userID:"+userID+"\n请求参数phoneNum:"+phoneNum);
try {
// jmeter 开始统计响应时间标记
sr.sampleStart();
//该类是dubbo接口需要的参数
ModelsGetBindingUserRealNameInfoRequest modelsGetBindingUserRealNameInfoRequest = new ModelsGetBindingUserRealNameInfoRequest();
modelsGetBindingUserRealNameInfoRequest.setUserID(userID);
modelsGetBindingUserRealNameInfoRequest.setPhoneNum(phoneNum);
ModelsReturn<ModelsGetBindingUserRealNameInfoResponse> bindingUserRealNameInfo = userInfoCenterService.getBindingUserRealNameInfo(modelsGetBindingUserRealNameInfoRequest);
String json = FastJsonUtil.beanToJson(bindingUserRealNameInfo);
logger.info(json);
sr.setResponseData(json,null);
sr.setDataType(SampleResult.TEXT);
//返回正确
sr.setSuccessful(true);
} catch (Throwable e) {
sr.setSuccessful(false);
e.printStackTrace();
} finally {
// jmeter 结束统计响应时间标记
sr.sampleEnd();
}
return sr;
}
/**
* 测试结束后调用
* @param arg0
*/
@Override
public void teardownTest(JavaSamplerContext arg0) {
logger.info("方法调用结束");
}
}
调试代码
先使用main方法测试,测试成功之后再打包放入jmeter
Application.javapackage com.test.jmeter;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
/**
* @author: wangsaichao
* @date: 2017/12/22
* @description:
*/
public class Application {
/**
* @param args
*/
public static void main(String[] args) {
Arguments params = new Arguments();
//设置参数
params.addArgument("userID", "");
//设置参数
params.addArgument("phoneNum", "18555555555");
JavaSamplerContext arg0 = new JavaSamplerContext(params);
DubboTest test = new DubboTest();
test.setupTest(arg0);
test.runTest(arg0);
test.teardownTest(arg0);
}
}
报如下异常:
Caused by: java.lang.ClassNotFoundException: org.w3c.dom.ElementTraversal
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 41 more
解决方法: 上面的pom.xml中已经有添加
<dependency>
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
<version>1.4.01</version>
</dependency>
再次执行报以下异常
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console. Set system property 'org.apache.logging.log4j.simplelog.StatusLogger.level' to TRACE to show Log4j2 internal initialization logging.
解决方法: 在classpath下添加log4j2.xml,内容参考:http://blog.csdn.net/qq_34021712/article/details/78870407
使用jmeter测试
调试通之后,将代码打成jra包,执行 执行maven clean package 后,targer下面会有两个东西,是我们需要的:TestDubbo.zip 和 TestDubbo.jar 其中TestDubbo.zip中也有包含TestDubbo.jar 还包含有lib文件,lib中有我们需要的jar包
解压TestDubbo.zip
将其中的TestDubbo.jar的jar 文件拷贝到jmeter的 lib/ext里面然后将解压TestDubbo.zip中的lib包改名为lib-dependency 拷贝到jmeter的lib下 (lib-dependency名字可以随便起)
修改jmeter.properties 中的 search_paths,配置相关的依赖的jar的路径
启动jmeter测试