多注册中心
1 关联服务和多个注册中心
1.1 全局默认注册中心
Dubbo 注册中心和服务是独立配置的。通常情况下,开发者不需要设置服务和注册中心组件之间的关系。Dubbo 框架会自动执行以下操作
- 对于所有 Service 服务,将服务地址注册到所有全局默认注册中心。
- 对于所有 Reference 服务,从所有全局默认注册中心订阅服务地址。
# application.yml (Spring Boot)
dubbo
registries
beijing Registry
address: zookeeper://:2181
shanghai Registry
address: zookeeper://:2182
@DubboService
public class DemoServiceImpl implements DemoService {}
@DubboService
public class HelloServiceImpl implements HelloService {}
以上以 Spring Boot 开发为例(XML 和 API 方式类似),配置了 beijingRegistry 和 shanghaiRegistry 两个全局默认注册中心,DemoService 和 HelloService 服务都会分别注册到这两个默认注册中心。
除了上述框架为服务自动设置全局注册中心外,还有两种方式可以灵活调整服务与多个注册中心的关联关系。
1.2 设置全局默认注册中心
# application.yml (Spring Boot)
dubbo
registries
beijing Registry
address: zookeeper://:2181
default: true
shanghai Registry
address: zookeeper://:2182
default: false
default
用于设置全局默认注册中心,默认值为 true
,即视为全局注册中心。未指定注册中心 id 的服务将自动注册或订阅全局默认注册中心。
1.3 显示关联服务和注册中心
通过将注册中心配置添加到 Dubbo 服务定义组件中,可以将服务与注册中心相关联。
@DubboServiceregistry = {"beijingRegistry"}
public class DemoServiceImpl implements DemoService {}
@DubboServiceregistry = {"shanghaiRegistry"}
public class HelloServiceImpl implements HelloService {}
添加上述配置后,DemoService 将只注册到 beijingRegistry,而 HelloService 将注册到 shanghaiRegistry。
2 多注册中心订阅
由于服务订阅涉及地址聚合和路由地址选择,逻辑会比较复杂。从单个服务订阅的角度来看,如果存在多注册中心订阅,可以根据注册中心之间的地址是否聚合分为两种场景。
2.1 多注册中心地址不聚合
<dubbo:registry id="hangzhouRegistry" address="10.20.141.150:9090" />
<dubbo:registry id="qingdaoRegistry" address="10.20.141.151:9010" />
对于上述独立配置的注册中心组件,默认情况下,地址列表在消费者端是完全隔离的,负载均衡地址选择需要经过两个步骤
- 注册中心集群间选址,选择一个集群
- 注册中心集群内选址,在集群内进行地址筛选
下面我们将重点介绍如何控制**注册中心集群间选址**,可选策略如下:**随机** 每个请求随机分配到一个注册中心集群
随机过程中会有可用性检查,即每个集群必须保证至少有一个地址可用才能被选中。
优先级
<dubbo:registry id="hangzhouRegistry" address="10.20.141.150:9090" preferred="true"/>
<dubbo:registry id="qingdaoRegistry" address="10.20.141.151:9010" />
如果配置了 preferred="true"
的注册中心集群,所有流量都将路由到该集群。
加权
<dubbo:registry id="hangzhouRegistry" address="10.20.141.150:9090" weight="100"/>
<dubbo:registry id="qingdaoRegistry" address="10.20.141.151:9010" weight="10" />
基于加权随机负载均衡,上述集群之间将有大约 10:1 的流量分配。
同区域优先
<dubbo:registry id="hangzhouRegistry" address="10.20.141.150:9090" zone="hangzhou" />
<dubbo:registry id="qingdaoRegistry" address="10.20.141.151:9010" zone="qingdao" />
RpcContext.getContext().setAttachment("registry_zone", "qingdao");
根据 Invocation 中携带的流量参数或通过 context 上下文在当前节点设置的参数,流量将被精确地引导到相应的集群。
2.2 多注册中心地址聚合
<dubbo:registry address="multiple://127.0.0.1:2181?separator=;&reference-registry=zookeeper://address11?backup=address12,address13;zookeeper://address21?backup=address22,address23" />
这里添加了一个以特殊多协议开头的注册中心,其中
multiple://127.0.0.1:2181
没有具体含义,只是一个特定格式的占位符,地址可以随意指定reference-registry
指定要聚合的注册中心集群列表。在示例中,有两个集群,zookeeper://address11?backup=address12,address13
和zookeeper://address21?backup=address22
,address23,同时也指定了集群分隔符
separator=";"`。
如下图所示,不同注册中心集群的地址将被聚合成一个地址池,用于在消费者端进行负载均衡或路由地址选择。
在 3.1.0 及以上版本中,还支持在每个注册中心集群上设置特定的 attachments 属性,以实现对注册中心集群下地址的特定标记,然后配合 TagRouter 等 Router 组件扩展,实现跨机房流量管理能力。
<dubbo:registry address="multiple://127.0.0.1:2181?separator=;&reference-registry=zookeeper://address11?attachments=zone=hangzhou,tag=middleware;zookeeper://address21" />
添加 attachments=zone=hangzhou,tag=middleware
后,来自该注册中心的所有 URL 地址都会自动携带 zone
和 tag
两个标识,方便消费者端进行更灵活的流量管理。
3 场景示例
3.1 场景一:跨地域注册服务
例如:中文站有些服务来不及在青岛部署,只部署在杭州,而青岛的其它应用需要引用该服务,此时可以将服务同时注册到两个注册中心。
<dubbo:registry id="hangzhouRegistry" address="10.20.141.150:9090" />
<dubbo:registry id="qingdaoRegistry" address="10.20.141.151:9010" default="false" />
<!-- Register with multiple registries -->
<dubbo:service interface="com.alibaba.hello.api.HelloService" version="1.0.0" ref="helloService" registry="hangzhouRegistry,qingdaoRegistry" />
3.2 场景二:基于业务隔离
部分 CRM 服务是专门为国际站服务的,部分服务是专门为中文站服务的。
<!-- Multi-registry configuration -->
<dubbo:registry id="chinaRegistry" address="10.20.141.150:9090" />
<dubbo:registry id="intlRegistry" address="10.20.154.177:9010" default="false" />
<!-- Register with the Chinese Station Registration Center -->
<dubbo:service interface="com.alibaba.hello.api.HelloService" version="1.0.0" ref="helloService" registry="chinaRegistry" />
<!-- Register with the International Station Registration Center -->
<dubbo:service interface="com.alibaba.hello.api.DemoService" version="1.0.0" ref="demoService" registry="intlRegistry" />
3.3 场景三:基于业务调用服务
CRM 需要同时调用中文站和国际站的 PC2 服务,PC2 在中文站和国际站都有部署,接口和版本号都一样,但连接的数据库不同。
<!-- Multi-registry configuration -->
<dubbo:registry id="chinaRegistry" address="10.20.141.150:9090" />
<dubbo:registry id="intlRegistry" address="10.20.154.177:9010" default="false" />
<!-- Quote Chinese station service -->
<dubbo:reference id="chinaHelloService" interface="com.alibaba.hello.api.HelloService" version="1.0.0" registry="chinaRegistry" />
<!-- Reference international station service -->
<dubbo:reference id="intlHelloService" interface="com.alibaba.hello.api.HelloService" version="1.0.0" registry="intlRegistry" />
如果只是测试环境暂时需要连接两个不同的注册中心,可以使用竖线分隔多个不同的注册中心地址
<!-- Multi-registry configuration, separated by a vertical sign means connecting to multiple different registries at the same time, and multiple cluster addresses of the same registrant are separated by commas -->
<dubbo:registry address="10.20.141.150:9090|10.20.154.177:9010" />
<!-- Reference service -->
<dubbo:reference id="helloService" interface="com.alibaba.hello.api.HelloService" version="1.0.0" />