写在前面
这里记录下springboot-dubbo的学习记录
-
准备
找到官方提供的示例项目,clone到本地开发工具,项目地址
zookeeper 我这里用的 3.4.10 版本
还有zookeeper可视化工具 下载地址,
其实还有个 Dubbo-server-admin,也可以监控Dubbo服务,路由监控和发起RPC调用等.
一、启动 Zookeeper和可视化工具
- windows窗口,双击zkServer.cmd或者命令行启动
-
zk 可视化,解压后进入build目录执行命令
java -jar zookeeper-dev-ZooInspector.jar
二、测试registry-samples --> zookeeper
先启动provider,再启动comsumer
2.1 Provider配置、使用
application.properties
# Spring boot application
spring.application.name=dubbo-registry-zookeeper-provider-sampleLS
# Base packages to scan Dubbo Component: @org.apache.dubbo.config.annotation.Service
dubbo.scan.base-packages=org.apache.dubbo.spring.boot.sample.provider.service
# Dubbo Application
## The default value of dubbo.application.name is ${spring.application.name}
## dubbo.application.name=${spring.application.name}
# Dubbo Protocol
dubbo.protocol.name=dubbo
## Random port
dubbo.protocol.port=-1
## Dubbo Registry
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.registry.file = ${user.home}/dubbo-cache/${spring.application.name}/dubbo.cache
## DemoService version
demo.service.version=1.0.0
使用时用 DubboService,这里只是简单使用,还可以配置并发数,token等等
@DubboService(version = "${demo.service.version}")
public class DefaultDemoService implements DemoService {
/**
* The default value of ${dubbo.application.name} is ${spring.application.name}
*/
@Value("${dubbo.application.name}")
private String serviceName;
@Override
public String sayHello(String name) {
return String.format("[%s] : Hello, %s", serviceName, name);
}
}
2.2 Consumer
application.properties
spring:
application:
name: dubbo-registry-zookeeper-consumer-sampleLS
demo:
service:
version: 1.0.0
embedded:
zookeeper:
port: 2181
dubbo:
registry:
address: zookeeper://127.0.0.1:${embedded.zookeeper.port}
file: ${user.home}/dubbo-cache/${spring.application.name}/dubbo.cache
使用,这种注入@DubboReference(version = “${demo.service.version}”),还可以配置 缓存,存根,负载均衡,参数等等
@DubboReference(version = "${demo.service.version}")
private DemoService demoService;
public ApplicationRunner runner() {
return args -> logger.info(demoService.sayHello("mercyblitz"));
}