用Jmeter实现dubbo接口测试的文章,网上可以找到很多,但是只看不练假把式。废话不多说,直接上干货。写这篇文章的过程也是自己不断学习的过程。
(1)自行下载安装zookeeper-3.4.6(这里的版本是我用的,可以自行下载自己喜好的版本)
(2)自行下载apache-jmeter-3.1,这是免安装的,解压后\Jmeter\apache-jmeter-3.1\bin目录下执行jmeter.bat即可启动。
(3)开发环境STS(即Spring Tool Suite)
(4)dubbo-admin-2.4.1(这个是非必须的,主要是为了查看Dubbo的服务提供者和消费者)
先明确下我们编写工程的过程,dubbo主要是通过实现一个接口,将服务暴露出去。所以我们创建工程的时候,最好分开创建。可以分为:
下面我们分别来看:
1.接口工程
通过STS创建一个Spring Project。命名为Interface-service。在这个工程里面只新建一个接口,命名为DubboService.java。其代码如下:
package com.dubbo.provider.Interface_service;
public interface DubboService {
public abstract void print();
}
其工程目录结构为:
此工程的其他内容都默认即可,不用做任何处理。
2.服务提供者dubbo-provider01的创建
通过STS创建一个Spring Project。命名为dubbo-provider01。其目录结构如下:
(1)通过pom.xml文件引入Interface-service的jar、dubbo服务的jar、zkclient的jar。这三个jar是必须要在pom文件中进行引用的,如果不引用后面会报错。引用代码为:
<dependency>
<groupId>com.dubbo.provider</groupId>
<artifactId>Interface-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
其中我们看Interface-service的引用,它的版本号哪里来的,我们在引用前,必须先对Interface-service这个工程进行maven install,生成其jar,其jar包在target目录下,如下图所示:
此时我们获取到Interface-service-0.01-SNAPSHOT.jar,在dubbo-provider01中可以引用了。
(2)编写dubbo-provider.xml文件。其文件内容为:
<?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">
<!-- <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"
default-lazy-init="true">
-->
<!-- 具体实现的bean -->
<bean id="dubboservice" class="com.dubbo.provider.demo.DubboServiceImpl" />
<!-- 定义应用名称 -->
<dubbo:application name="dubbo-provider01"/>
<!--zk注册中心的地址-->
<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" />
<!-- 用dubbo协议在21000端口暴露服务 -->
<dubbo:protocol name="dubbo" port="21000" />
<!-- 配置服务接口 -->
<dubbo:service interface="com.dubbo.provider.Interface_service.DubboService" ref="dubboservice" />
</beans>
其中在编写xml文件前,必须先在windows->>preferences->>XML Catalog中add,详见我的博客。
(3)配置log4j.properties文件,文件内容为:
log4j.rootLogger=info, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
(4)在src/main/java下新建包com.dubbo.provider.demo,再在包com.dubbo.provider.dem下新建接口DubboService.java的实现类DubboServiceImpl.java。其源码为:
package com.dubbo.provider.demo;
import org.springframework.stereotype.Service;
import com.dubbo.provider.Interface_service.DubboService;
@Service("dubboservice")
public class DubboServiceImpl implements DubboService{
public void print() {
System.out.println("欢迎您的操作");
}
}
(5)在src/test/java下新建包com.dubbo.provider.test,再在包com.dubbo.provider.test下新建启动服务的测试类TestProvider.java。其源码为:
package com.dubbo.provider.test;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.CommonAnnotationBeanPostProcessor;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.alibaba.dubbo.common.URL;
public class TestProvider {
private static final Log log = LogFactory.getLog(TestProvider.class);
public static void main(String[] args) {
try {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:dubbo-provider.xml");
context.start();
} catch (Exception e) {
log.error("== DubboProvider context start error:",e);
}
synchronized (TestProvider.class) {
while (true) {
try {
TestProvider.class.wait();
} catch (InterruptedException e) {
log.error("== synchronized error:",e);
}
}
}
}
}
(6)首先启动ZK、再次启动estProvider.java,在启动Tomcat。最后查看其服务注册情况:
可以看到我们的服务提供者已经创建成功了。(怎样使用dubbo-admin查看dubbo服务可以参考我的博文:)
3.消费者dubbo-consumer的创建
通过STS创建一个Spring Project,其目录结构如下图所示:
(1)pom.xml文件的引用,此时我们需要注意一个问题,作为消费者要调用服务提供者,所以必须要引入服务提供者的jar包,请注意我们引用的还应该是Interface-service的jar而并非是dubbo-provider01的jar。代码如下:
<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>org.springframework.samples</groupId>
<artifactId>dubbo-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<!-- Generic properties -->
<java.version>1.6</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- Spring -->
<spring-framework.version>3.2.3.RELEASE</spring-framework.version>
<!-- Hibernate / JPA -->
<hibernate.version>4.2.1.Final</hibernate.version>
<!-- Logging -->
<logback.version>1.0.13</logback.version>
<slf4j.version>1.7.5</slf4j.version>
<!-- Test -->
<junit.version>4.11</junit.version>
</properties>
<dependencies>
<!-- Spring and Transactions -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-framework.version}</version>
</dependency>
<dependency>
<groupId>com.dubbo.provider</groupId>
<artifactId>Interface-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring-framework.version}</version>
</dependency>
<!-- Logging with SLF4J & LogBack -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
<scope>runtime</scope>
</dependency>
<!-- Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!-- Test Artifacts -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring-framework.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
(2)编写dubbo-consumer.xml文件,其代码如下:
<?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="dubbo-consumer" />
<!-- 填写zk注册中心的地址 -->
<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" />
<!-- 引用服务提供接口的路径 -->
<dubbo:reference interface="com.dubbo.provider.Interface_service.DubboService" id="dubboservice" />
</beans>
(3)编写log4j.properties文件,内容与服务提供者的一致即可,不在赘述。
(4)在src/test/java下新建包com.dubbo.consumer.demo,再在包com.dubbo.consumer.demo下新建测试类TestConsumer.java,启动消费者,其源码如下:
package com.dubbo.consumer.demo;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.dubbo.provider.Interface_service.DubboService;
public class TestConsumer {
private static final Log log = LogFactory.getLog(TestConsumer.class);
public static void main(String[] args) throws InterruptedException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:dubbo-consumer.xml");
context.start();
DubboService demoService = (DubboService)context.getBean("dubboservice");
demoService.print();
// log.info("打印"+demoService.print());
Thread.sleep(100000);
}
}
(5)确保服务提供者正常启用的情况下,运行TestConsumer.java,运行结果如下:
(6)此时在查看dubbo服务:
我们可以看到dubbo-consumer这个dubbo服务业注册成功了,从此这个工程就搭建完毕了。
(1)导出dubbo-consumer工程依赖的所有jar包:导出方法cmd到工程所在路径下执行mvn dependency:copy-dependencies命令即可:此时会在工程的target目录下新增一个
dependency目录
(2)将dependency目录中的jar全部复制粘贴到:Jmeter的lib目录下,如果有重复的jar包,则保留Jmeter的jar包。
(3)Run as工程dubbo-consumer,选择maven install。生成dubbo-consumer-0.0.1-SNAPSHOT.jar。
将dubbo-consumer-0.0.1-SNAPSHOT.jar放到Jmeter的\Jmeter\apache-jmeter-3.1\lib\ext路径下。
(4)以管理员身份打开Jmeter。新建一个线程组,新建一个java请求,增加查看结果树;此时运行java请求:
剩下的事情就是如何写测试脚本了,这个我们以后慢慢说~~~~~