1.下载用于测试dubbo的spring boot项目
参考文章:
http://t.zoukankan.com/111testing-p-11297038.html
https://zhuanlan.zhihu.com/p/539190478
dubbo介绍:
https://www.cnblogs.com/liuyuelinfighting/p/14972958.html
https://dubbo.apache.org/zh/docs3-v2/java-sdk/quick-start/spring-boot/
git clone -b master https://github.com/apache/dubbo-samples.git
cd dubbo-samples/dubbo-samples-spring-boot
mvn clean package // 打包成jar
//启动生产者服务
cd ./dubbo-samples-spring-boot-provider
java -jar ./target/dubbo-samples-spring-boot-provider-1.0-SNAPSHOT.jar
// 启动消费者服务
java -jar ./target/dubbo-samples-spring-boot-consumer-1.0-SNAPSHOT.jar
示例核心流程
1. 定义服务接口
dubbo-samples-spring-boot-interface/DemoService.java
package org.apache.dubbo.samples.basic.api;
public interface DemoService {
String sayHello(String name);
}
2. 提供方实现接口并暴露服务
dubbo-samples-spring-boot-provider/DemoServiceImpl.java
@DubboService
public class DemoServiceImpl implements DemoService {
@Override
public String sayHello(String name) {
System.out.println("Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
return "Hello " + name;
}
}
3. 配置 application.yml 文件
dubbo-samples-spring-boot-provider/resources/application.yml
dubbo:
application:
name: dubbo-springboot-demo-provider
protocol:
name: dubbo
port: -1
registry:
id: zk-registry
address: zookeeper://127.0.0.1:2181
config-center:
address: zookeeper://127.0.0.1:2181
metadata-report:
address: zookeeper://127.0.0.1:2181
4. 定义 Spring Boot 主函数,其中,@EnableDubbo 必须配置。
dubbo-samples-spring-boot-provider/ProviderApplication.java
@SpringBootApplication
@EnableDubbo
public class ProviderApplication {
public static void main(String[] args) throws Exception {
new EmbeddedZooKeeper(2181, false).start();
SpringApplication.run(ProviderApplication.class, args);
System.out.println("dubbo service started");
new CountDownLatch(1).await();
}
}
5. 引用远程服务
dubbo-samples-spring-boot-consumer/ConsumerApplication.java
public class ConsumerApplication {
@DubboReference
private DemoService demoService;
}
6. 定义 application.yml
dubbo-samples-spring-boot-consumer/application.yml
dubbo:
application:
name: dubbo-springboot-demo-consumer
protocol:
name: dubbo
port: -1
registry:
id: zk-registry
address: zookeeper://127.0.0.1:2181
config-center:
address: zookeeper://127.0.0.1:2181
metadata-report:
address: zookeeper://127.0.0.1:2181
7. 加载 Spring 配置,并调用远程服务
dubbo-samples-spring-boot-consumer/ConsumerApplication.java
@SpringBootApplication
@Service
@EnableDubbo
public class ConsumerApplication {
@DubboReference
private DemoService demoService;
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(ConsumerApplication.class, args);
ConsumerApplication application = context.getBean(ConsumerApplication.class);
String result = application.doSayHello("world");
System.out.println("result: " + result);
}
}
注意:
1.启动生产者(provider)项目时会自动关联启动zookeeper,端口占用:2181
2.dubbo服务默认占用端口:20880
3.jmeter测试时可以使用jar启动项目,也可以使用spring boot启动项目(只启动provider端即可)
2.下载jmeter测试dubbo所需要的插件,我使用的版本是2.7.8,将插件放到jmeter/lib/ext目录下,然后打开jmeter搞事情了
地址: https://github.com/thubbo/jmeter-plugins-for-apache-dubbo/releases
这里的sayGoodbye方法是自己手动添加的,用于加深对这块的理解
3.中途遇到的问题以及处理
1.dubbo请求失败,这篇文章里有提到处理:https://blog.csdn.net/inlauallnoany/article/details/119237582,如果是在错误信息里面看到172.17.160.1,则说明iis服务有冲突,需要关闭iis服务,关闭iis服务:https://www.jb51.net/os/win11/812370.html
2.下载的dubbo项目,默认的interface包路径是错误的,需要替换为具体的接口路径