Dubbo集成Spring Boot
文章目录
Maven构建
添加什么依赖集成Spring Boot和Dubbo呢?可以看下git地址,pom.xml还是很简单的。
另外还是推荐官方例子:
https://github.com/apache/dubbo-spring-boot-project
Hello World
这个例子中实现简单的Dubbo与Spring Boot集成,并测试消费者暴露的endpoint进行概念验证:
Git地址:https://gitee.com/oneslideicywater/dubbo-playground.git
文档地址: Spring Boot Integration with Dubbo
使用Zookeeper进行服务注册
切到zookeeper分支
特别注意使用:
dubbo.registry.use-as-config-center=false
参考:java.lang.IllegalStateException: zookeeper not connected
生产者和消费者配置基本都是一样的。下面是配置消费者和生产者的两种方式:
application.properties
# Spring boot application,你必须给每个应用添加一个名字
spring.application.name=dubbo-auto-configuration-provider-demo
# Base packages to scan Dubbo Component: @org.apache.dubbo.config.annotation.Service
# 扫描注解了@Service类所在的包
dubbo.scan.base-packages=com.oneslide.helloserviceprovider
# Dubbo Protocol
dubbo.protocol.name=dubbo
## Random port
dubbo.protocol.port=-1
## Dubbo Registry
dubbo.registry.address=zookeeper://192.168.1.3:2181
# 非服务注册中心
dubbo.registry.use-as-config-center=false
YAML方式
spring:
application:
name: dubbo-hello-consumer
demo:
service:
version: 1.0.0
dubbo:
registry:
address: zookeeper://192.168.1.3:2181
use-as-config-center: false
测试
Zookeeper
查看Zookeeper的节点:
lide.api.hello.DemoService%26methods%3DsayHello%26pid%3D27708%26release%3D2.7.6%26revision%3D1.0.0%26side%3Dprovider%26timestamp%3D1587192273124%26version%3D1.0.0
[zk: 192.168.1.3:2181(CONNECTED) 33] ls /dubbo
[com.oneslide.api.hello.DemoService]
# 这里是dubbo DemoService服务的树形目录内容
[zk: 192.168.1.3:2181(CONNECTED) 34] ls /dubbo/com.oneslide.api.hello.DemoService
[configurators, consumers, providers, routers]
# 这里是服务消费者对应的Znode,是一个长字符串
[zk: 192.168.1.3:2181(CONNECTED) 35] ls /dubbo/com.oneslide.api.hello.DemoService/consumers
[consumer%3A%2F%2F192.168.59.1%2Fcom.oneslide.api.hello.DemoService%3Fapplication%3Ddubbo-hello-consumer%26category%3Dconsumers%26check%3Dfalse%26dubbo%3D2.0.2%26init%3Dfalse%26interface%3Dcom.oneslide.api.hello.DemoService%26methods%3DsayHello%26pid%3D27072%26qos.enable%3Dfalse%26release%3D2.7.6%26revision%3D1.0.0%26side%3Dconsumer%26sticky%3Dfalse%26timestamp%3D1587192301558%26version%3D1.0.0]
# 这里是服务提供者对应的Znode,是一个长字符串,不过是个空值节点
[zk: 192.168.1.3:2181(CONNECTED) 36] ls /dubbo/com.oneslide.api.hello.DemoService/providers
[dubbo%3A%2F%2F192.168.59.1%3A20880%2Fcom.oneslide.api.hello.DemoService%3Fanyhost%3Dtrue%26application%3Ddubbo-auto-configuration-provider-demo%26deprecated%3Dfalse%26dubbo%3D2.0.2%26dynamic%3Dtrue%26generic%3Dfalse%26interface%3Dcom.oneslide.api.hello.DemoService%26methods%3DsayHello%26pid%3D27708%26release%3D2.7.6%26revision%3D1.0.0%26side%3Dprovider%26timestamp%3D1587192273124%26version%3D1.0.0]
# 这里看到服务消费者对应的Znode是自己的IP地址。
[zk: 192.168.1.3:2181(CONNECTED) 37] get /dubbo/com.oneslide.api.hello.DemoService/consumers/consumer%3A%2F%2F192.168.59.1%2Fcom.oneslide.api.hello.DemoService%3Fapplication%3Ddubbo-hello-consumer%26category%3Dconsumers%26check%3Dfalse%26dubbo%3D2.0.2%26init%3Dfalse%26interface%3Dcom.oneslide.api.hello.DemoService%26methods%3DsayHello%26pid%3D27072%26qos.enable%3Dfalse%26release%3D2.7.6%26revision%3D1.0.0%26side%3Dconsumer%26sticky%3Dfalse%26timestamp%3D1587192301558%26version%3D1.0.0
192.168.59.1
根据上面命令的执行简单分析下,dubbo使用zookeeper作为服务注册中心时,如何实现?
需要明白Zookeeper的两个特性:
- 临时Znode(ephemeral node)在Zookeeper客户端断连Zookeeper Master自动被删除,从这个角度来讲,服务生产者和消费者都是Zookeeper客户端。
- watch机制: 客户端可以在节点上添加watch(监视器),接受节点事件(新增子节点,删除,更改数据等)。
那么知道上面两个背景知识,简单分析一下:
参考:架构
服务提供者在启动时,向注册中心注册自己提供的服务
以上面的例子,/dubbo/com.oneslide.api.hello.DemoService/providers
注册一个临时节点:
[zk: 192.168.1.3:2181(CONNECTED) 46] stat /dubbo/com.oneslide.api.hello.DemoService/providers/dubbo%3A%2F%2F192.168.59.1%3A20880%2Fcom.oneslide.api.hello.DemoService%3Fanyhost%3Dtrue%26application%3Ddubbo-auto-configuration-provider-demo%26deprecated%3Dfalse%26dubbo%3D2.0.2%26dynamic%3Dtrue%26generic%3Dfalse%26interface%3Dcom.oneslide.api.hello.DemoService%26methods%3DsayHello%26pid%3D27708%26release%3D2.7.6%26revision%3D1.0.0%26side%3Dprovider%26timestamp%3D1587192273124%26version%3D1.0.0
cZxid = 0x24
ctime = Sat Apr 18 02:44:37 EDT 2020
mZxid = 0x24
mtime = Sat Apr 18 02:44:37 EDT 2020
pZxid = 0x24
cversion = 0
dataVersion = 0
aclVersion = 0
#这里表示这是一个临时节点,值为客户端的SessionID
ephemeralOwner = 0x100002a40e4000b
dataLength = 12
numChildren = 0
但并不是顺序节点,其节点区分有个部分(端口)不一样。
服务消费者在启动时,向注册中心订阅自己所需的服务
在相应的临时节点/dubbo/com.oneslide.api.hello.DemoService/providers
添加watch,当有新的服务提供者出现或者有的崩溃,接受事件回调。
怎么查看Zookeeper某个节点注册的watch呢?找了半天没找到!!!
Load-Balance
dubbo支持多种负载均衡,原理不赘述,可以查看Dubbo官网。这里我找了半天文档,总结出四种负载均衡的具体配置方式:
@Service(version = "1.0.0",loadbalance ="roundrobin")
可选值:
roundrobin
-
random
(default) -
leastactive
请求处理慢的获得更少的请求(最小活跃连接数) -
consistenthash
一致性hash
上一篇: IDEA中运行Spring boot web项目无法访问页面
下一篇: Spring Cloud