Dubbo中应用级,与接口级配置中心的使用,包括单配置中心与多配置中心

接口级或应用级服务发现

Dubbo3 默认采用 “应用级服务发现 + 接口级服务发现” 的双注册模式

可以通过配置 dubbo.registry.register-mode=instance/interface/all 来改变注册行为。

  • instance : 应用级
  • interface : 接口级
  • all :两者都注册(默认模式)

Zookeeper

增加 Maven 依赖

<properties>
    <dubbo.version>3.0.8</dubbo.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.apache.dubbo</groupId>
        <artifactId>dubbo</artifactId>
        <version>${dubbo.version}</version>
    </dependency>
    <!-- This dependency helps to introduce Curator and Zookeeper dependencies that are necessary for Dubbo to work with zookeeper as transitive dependencies  -->
    <dependency>
        <groupId>org.apache.dubbo</groupId>
        <artifactId>dubbo-dependencies-zookeeper</artifactId>
        <version>${dubbo.version}</version>
        <type>pom</type>
    </dependency>
</dependencies>

dubbo-dependencies-zookeeper 将自动为应用增加 Zookeeper 相关客户端的依赖,减少用户使用 Zookeeper 成本,如使用中遇到版本兼容问题,用户也可以不使用 dubbo-dependencies-zookeeper,而是自行添加 CuratorZookeeper Client 等依赖。

由于 Dubbo 使用 Curator 作为与 Zookeeper Server 交互的编程客户端,因此,要特别注意 Zookeeper Server Dubbo 版本依赖的兼容性

Zookeeper Server 版本Dubbo 版本Dubbo Zookeeper 依赖包说明
3.4.x 及以下3.0.x 及以上dubbo-dependencies-zookeeper传递依赖 Curator 4.x 、Zookeeper 3.4.x
3.5.x 及以上3.0.x 及以上dubbo-dependencies-zookeeper-curator5传递依赖 Curator 5.x 、Zookeeper 3.7.x
3.4.x 及以上2.7.x 及以下dubbo-dependencies-zookeeper传递依赖 Curator 4.x 、Zookeeper 3.4.x
3.5.x 及以上2.7.x 及以下须自行添加 Curator、Zookeeper 等相关客户端依赖

配置 Zookeeper

yml方式

dubbo
 registry
   address: zookeeper://localhost:2181

properties 文件方式

dubbo.registry.address=zookeeper://localhost:2181 

xml 文件方式

<dubbo:registry address="zookeeper://localhost:2181" /> 

address 是启用 zookeeper 注册中心唯一必须指定的属性,而在生产环境下,address 通常被指定为集群地址,如

address=zookeeper://10.20.153.10:2181?backup=10.20.153.11:2181,10.20.153.12:2181

protocol 与 address 分开配置的模式也可以,如

<dubbo:registry protocol="zookeeper" address="10.20.153.10:2181,10.20.153.11:2181,10.20.153.12:2181" />

高级配置

认证与鉴权

如果 Zookeeper 开启认证,Dubbo 支持指定 username、password 的方式传入身份标识。

dubbo
 registry
  address: zookeeper://localhost:2181    
  username: root    
  password: 1234 

也可以直接将参数扩展在 address 上 address=zookeeper://root:1234@localhost:2181

分组隔离

通过指定 group 属性,可以在同一个 Zookeeper 集群内实现微服务地址的逻辑隔离。比如可以在一套集群内隔离出多套开发环境,在地址发现层面实现隔离。

dubbo 
 registry   
  address: zookeeper://localhost:2181
  group: group1

其他扩展配置

配置连接、会话过期时间

dubbo
 registry
   address: zookeeper://localhost:2181
   timeout: 30 * 1000* # 连接超时时间,默认 30s
   session: 60 * 1000* # 会话超时时间,默认 60s

Zookeeper 注册中心还支持其他一些控制参数,具体可参见Registry 配置项手册

工作原理

Dubbo2 节点结构

img

流程:

  • 服务提供者启动时: 向 /dubbo/com.foo.BarService/providers 目录下写入自己的 URL 地址。
  • 服务消费者启动时: 订阅 /dubbo/com.foo.BarService/providers 目录下的提供者 URL 地址。并向 /dubbo/com.foo.BarService/consumers 目录下写入自己的 URL 地址
  • 监控中心启动时: 订阅 /dubbo/com.foo.BarService 目录下的所有提供者和消费者 URL 地址。

支持以下功能:

  • 当提供者出现断电等异常停机时,注册中心能自动删除提供者信息
  • 当注册中心重启时,能自动恢复注册数据,以及订阅请求
  • 当会话过期时,能自动恢复注册数据,以及订阅请求
  • 当设置 <dubbo:registry check=“false” /> 时,记录失败注册和订阅请求,后台定时重试
  • 可通过 <dubbo:registry username=“admin” password=“1234” /> 设置 zookeeper 登录信息
  • 可通过 <dubbo:registry group=“dubbo” /> 设置 zookeeper 的根节点,不配置将使用默认的根节点。
  • 支持 * 号通配符 <dubbo:reference group=“" version="” />,可订阅服务的所有分组和所有版本的提供者

Nacos

Maven依赖

<dependencies>
    <dependency>
        <groupId>org.apache.dubbo</groupId>
        <artifactId>dubbo</artifactId>
        <version>3.1.2</version>
    </dependency>
    <dependency>
      <groupId>com.alibaba.nacos</groupId>
      <artifactId>nacos-client</artifactId>
      <version>2.1.0</version>
    </dependency>
     <!-- Introduce Dubbo Nacos extension, or you can add Nacos dependency directly as shown above-->
     <!--
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-registry-nacos</artifactId>
            <version>3.1.2</version>
        </dependency>
     -->
</dependencies>

Dubbo 3.0.0 及以上版本需 nacos-client 2.0.0 及以上版本

配置 Nacos

yml方式

dubbo
 registry
   address: nacos://localhost:8848

properties 文件方式

dubbo.registry.address=nacos://localhost:8848

xml 文件方式

<dubbo:registry address="nacos://localhost:2181" /> 

高级配置

认证

dubbo
 registry
   address: nacos://localhost:8848?username=nacos&password=nacos
dubbo.registry.address: nacos://nacos:nacos@localhost:8848

自定义命名空间

dubbo:
 registry:
   address: nacos://localhost:8848
   parameters.namespace: 5cbb70a5-xxx-xxx-xxx-d43479ae0932
   # 或者拼接在address后面
   #address: nacos://localhost:8848?namespace=5cbb70a5-xxx-xxx-xxx-d43479ae0932

自定义分组

dubbo:
 registry:
   address: nacos://localhost:8848
   group: nacos1

如果不配置的话,group 是由 Nacos 默认指定。group 和 namespace 在 Nacos 中代表不同的隔离层次,通常来说 namespace 用来隔离不同的用户或环境,group 用来对同一环境内的数据做进一步归组。

注册接口级消费者

Dubbo3.0.0版本以后,增加了是否注册消费者的参数,如果需要将消费者注册到nacos注册中心上,需要将参数(register-consumer-url)设置为true,默认是false。

dubbo:
  registry:
    #address: nacos://localhost:8848?register-consumer-url=true
    address: nacos://localhost:8848
    parameters.register-consumer-url: true

多注册中心

与单注册中心的区别

单注册中心 : dubbo.registriy

多注册中心 :dubbo.registries

关联服务与多注册中心

全局默认注册中心

Dubbo 注册中心和服务是独立配置的,通常开发者不用设置服务和注册中心组件之间的关联关系,Dubbo 框架会将自动执行以下动作:

  • 对于所有的 Service 服务,向所有全局默认注册中心注册服务地址。
  • 对于所有的 Reference 服务,从所有全局默认注册中心订阅服务地址。

设置全局默认注册中心

dubbo
 registries
  beijingRegistry
   address: zookeeper://localhost:2181
   default: true
  shanghaiRegistry
   address: zookeeper://localhost:2182
   default: false

default 用来设置全局默认注册中心,默认值为 true 即被视作全局注册中心。未指定注册中心 id 的服务将自动注册或订阅全局默认注册中心。

显示关联服务与注册中心

通过在 Dubbo 服务定义组件上增加 registry 配置,将服务与注册中心关联起来。

@DubboServiceregistry = {"beijingRegistry"}
public class DemoServiceImpl implements DemoService {}

@DubboServiceregistry = {"shanghaiRegistry"}
public class HelloServiceImpl implements HelloService {}

增加以上配置后,DemoService 将只注册到 beijingRegistry,而 HelloService 将注册到 shanghaiRegistry

多注册中心订阅

服务订阅由于涉及到地址聚合和路由选址,因此逻辑会更加复杂一些。从单个服务订阅的视角,如果存在多注册中心订阅的情况,则可以根据注册中心间的地址是否聚合分为两种场景。

多注册中心地址不聚合

<dubbo:registry id="hangzhouRegistry" address="10.20.141.150:9090" /> 
<dubbo:registry id="qingdaoRegistry" address="10.20.141.151:9010" /> 
dubbo.registries.beijing.address=zookeeper://192.168.179.82:2181
dubbo.registries.shanghai.address=nacos://192.168.179.82:8848

如以上所示独立配置的注册中心组件,地址列表在消费端默认是完全隔离的,负载均衡选址要经过两步:

  1. 注册中心集群间选址,选定一个集群
  2. 注册中心集群内选址,在集群内进行地址筛选

img

下面我们着重分析下如何控制 注册中心集群间选址,可选的策略有如下几种 随机 每次请求都随机的分配到一个注册中心集群

随机的过程中会有可用性检查,即每个集群要确保至少有一个地址可用才有可能被选到。

preferred 优先

<dubbo:registry id="hangzhouRegistry" address="10.20.141.150:9090" preferred="true"/> 
<dubbo:registry id="qingdaoRegistry" address="10.20.141.151:9010" /> 
dubbo.registries.beijing.preferred=true

如果有注册中心集群配置了 preferred=“true”,则所有流量都会被路由到这个集群。

weighted

<dubbo:registry id="hangzhouRegistry" address="10.20.141.150:9090" weight="100"/> 
<dubbo:registry id="qingdaoRegistry" address="10.20.141.151:9010" weight="10" /> 
dubbo.registries.beijing.weight=100

基于权重的随机负载均衡,以上集群间会有大概 10:1 的流量分布。

同 zone 优先

<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 协议开头的注册中心,其中:

  • multiple://127.0.0.1:2181 并没有什么具体含义,只是一个特定格式的占位符,地址可以随意指定
  • reference-registry 指定了要聚合的注册中心集群的列表,示例中有两个集群,分别是 zookeeper://address11?backup=address12,address13zookeeper://address21?backup=address22,address23,其中还特别指定了集群分隔符 separator=";"

如下图所示,不同注册中心集群的地址会被聚合到一个地址池后在消费端做负载均衡或路由选址。

img

在 3.1.0 版本及之后,还支持每个注册中心集群上设置特定的 attachments 属性,以实现对该注册中心集群下的地址做特定标记,后续配合 Router 组件扩展如 TagRouter 等就可以实现跨机房间的流量治理能力。

<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 两个标识,方便消费端更灵活的做流量治理。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值