Dubbo:生产者和消费者一对多,多对多

本文涉及的产品
注册配置 MSE Nacos/ZooKeeper,182元/月
云原生网关 MSE Higress,422元/月
任务调度 XXL-JOB 版免费试用,400 元额度,开发版规格
简介: Dubbo:生产者和消费者一对多,多对多

一个生产者对应多个消费者

 

同一注册中心

  • 生产者,部署在不同的容器里(如tomcat)
  • 保证注册中心IP一致
  • 保证Dubbo协议端口不一致

示例

生产者1

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://code.alibabatech.com/schema/dubbo
    http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="dubbo_provider" />
    <!-- 使用zookeeper注册中心暴露服务地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />
    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880" />
    <!-- 声明需要暴露的服务接口  userService 使用注解已经声明-->
    <dubbo:service interface="com.dubbo.service.UserService"  ref="userService" />
    <bean id="userService" class="com.dubbo.service.impl.UserServiceImpl" />
</beans>


生产者2

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://code.alibabatech.com/schema/dubbo
    http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="dubbo_provider" />
    <!-- 使用zookeeper注册中心暴露服务地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />
    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20881" />
    <!-- 声明需要暴露的服务接口  userService 使用注解已经声明-->
    <dubbo:service interface="com.dubbo.service.UserService"  ref="userService" />
    <bean id="userService" class="com.dubbo.service.impl.UserServiceImpl" />
</beans>


消费者

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://code.alibabatech.com/schema/dubbo
    http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
  <!-- 提供方应用信息,用于计算依赖关系 -->
  <dubbo:application name="dubbo_consumer" />
  <!-- 使用zookeeper注册中心暴露服务地址 -->
  <dubbo:registry address="zookeeper://127.0.0.1:2181" />
  <!-- 声明需要暴露的服务接口 -->
  <dubbo:reference interface="com.dubbo.service.UserService"  id="userService" check="false"/>
</beans>

 

不同注册中心

  • 多个生产者,注册在不同的注册中心;
  • 搭建Zookeeper 集群
  • 消费者使用Zookeeper 集群配置(参考下文)

注意:多注册中心配置,竖号分隔表示同时连接多个不同注册中心,同一注册中心的多个集群地址用逗号分隔

Zookeeper 集群配置:

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

或:

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

同一 Zookeeper,分成多组注册中心:

<dubbo:registry id="chinaRegistry" protocol="zookeeper" address="10.20.153.10:2181" group="china" />
<dubbo:registry id="intlRegistry" protocol="zookeeper" address="10.20.153.10:2181" group="intl" 


多注册中心

Dubbo

  • 支持同一服务向多注册中心同时注册
  • 或者不同服务分别注册到不同的注册中心上去,
  • 甚至可以同时引用注册在不同注册中心上的同名服务
  • 另外,注册中心是支持自定义扩展的 。


多注册中心注册

比如:中文站有些服务来不及在青岛部署,只在杭州部署,而青岛的其它应用需要引用此服务,就可以将服务同时注册到两个注册中心。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
    <dubbo:application name="world"  />
    <!-- 多注册中心配置 -->
    <dubbo:registry id="hangzhouRegistry" address="10.20.141.150:9090" />
    <dubbo:registry id="qingdaoRegistry" address="10.20.141.151:9010" default="false" />
    <!-- 向多个注册中心注册 -->
    <dubbo:service interface="com.alibaba.hello.api.HelloService" version="1.0.0" ref="helloService" registry="hangzhouRegistry,qingdaoRegistry" />
</beans>


不同服务使用不同注册中心

比如:CRM 有些服务是专门为国际站设计的,有些服务是专门为中文站设计的。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
    <dubbo:application name="world"  />
    <!-- 多注册中心配置 -->
    <dubbo:registry id="chinaRegistry" address="10.20.141.150:9090" />
    <dubbo:registry id="intlRegistry" address="10.20.154.177:9010" default="false" />
    <!-- 向中文站注册中心注册 -->
    <dubbo:service interface="com.alibaba.hello.api.HelloService" version="1.0.0" ref="helloService" registry="chinaRegistry" />
    <!-- 向国际站注册中心注册 -->
    <dubbo:service interface="com.alibaba.hello.api.DemoService" version="1.0.0" ref="demoService" registry="intlRegistry" />
</beans>


多注册中心引用

比如:CRM 需同时调用中文站和国际站的 PC2 服务,PC2 在中文站和国际站均有部署,接口及版本号都一样,但连的数据库不一样。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
    <dubbo:application name="world"  />
    <!-- 多注册中心配置 -->
    <dubbo:registry id="chinaRegistry" address="10.20.141.150:9090" />
    <dubbo:registry id="intlRegistry" address="10.20.154.177:9010" default="false" />
    <!-- 引用中文站服务 -->
    <dubbo:reference id="chinaHelloService" interface="com.alibaba.hello.api.HelloService" version="1.0.0" registry="chinaRegistry" />
    <!-- 引用国际站站服务 -->
    <dubbo:reference id="intlHelloService" interface="com.alibaba.hello.api.HelloService" version="1.0.0" registry="intlRegistry" />
</beans>


如果只是测试环境临时需要连接两个不同注册中心,使用竖号分隔多个不同注册中心地址:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
    <dubbo:application name="world"  />
    <!-- 多注册中心配置,竖号分隔表示同时连接多个不同注册中心,同一注册中心的多个集群地址用逗号分隔 -->
    <dubbo:registry address="10.20.141.150:9090|10.20.154.177:9010" />
    <!-- 引用服务 -->
    <dubbo:reference id="helloService" interface="com.alibaba.hello.api.HelloService" version="1.0.0" />
</beans>

参考来源:http://dubbo.incubator.apache.org/zh-cn/docs/user/demos/multi-registry.html



相关文章
|
缓存 Dubbo Java
Dubbo服务消费者启动与订阅原理
该文章主要介绍了Dubbo服务消费者启动与订阅的原理,包括服务消费者的启动时机、启动过程以及订阅和感知最新提供者信息的方式。
Dubbo服务消费者启动与订阅原理
|
Dubbo 应用服务中间件 Apache
集成Nacos1.2.1和Dubbo2.7.6 消费者报错No provider available for the service xxx
集成Nacos1.2.1和Dubbo2.7.6 消费者报错No provider available for the service xxx
260 0
|
运维 Dubbo 中间件
Dubbo3 源码解读-宋小生-19:重新来过从一个服务消费者的Demo说起
> 完整电子书下载地址: https://developer.aliyun.com/ebook/7894 > Dubbo3 已经全面取代 HSF2 成为阿里的下一代服务框架,2022 双十一基于 Dubbo3 首次实现了关键业务不停推、不降级的全面用户体验提升,从技术上,大幅提高研发与运维效率的同时地址推送等关键资源利用率提升超 40%,基于三位一体的开源中间件体系打造了阿里在云上的单元化最佳实
244 0
Dubbo3 源码解读-宋小生-19:重新来过从一个服务消费者的Demo说起
|
Dubbo Java 应用服务中间件
<2>Dubbo中使用服务消费者调用生产者
接上一篇博客Dubbo快速入门 发布服务、启动服务
|
设计模式 Dubbo Java
dubbo生产者暴露服务流程
在这个过程中,url是我们需要进行关注的,此时我们可以看到基本上都是以url为主题进行组装操作。将所有需要放入的输入进行放入,同时最终会以观察者模式,实现配置的实时更新。低版本的dubbo则是以实现InitializingBean,重写AfterPropertiesSet方法。之所以改成基于ApplicationEvent,是因为可以进行更新,这是ApplicationEvent的优势。 同时通过对bubbo的学习,可以看到Netty的使用。
172 0
dubbo生产者暴露服务流程
|
Dubbo 应用服务中间件 数据库
Dubbo分布式架构中 消费者报错Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedded
Dubbo分布式架构中 消费者报错Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedded
286 0
Dubbo分布式架构中 消费者报错Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedded
|
负载均衡 监控 Dubbo
Spring Boot 整合Dubbo + Zookeeper 实现分布式 消费者与服务者的业务调用
Spring Boot 整合Dubbo + Zookeeper 实现分布式 消费者与服务者的业务调用
310 0
Spring Boot 整合Dubbo + Zookeeper 实现分布式 消费者与服务者的业务调用
|
XML Dubbo Java
通俗易懂的Dubbo学习(一) dubbo服务者和消费者的简单案例
通俗易懂的Dubbo学习(一) dubbo服务者和消费者的简单案例
320 0
|
Dubbo Java 应用服务中间件
SpringBoot 集成Dubbo 消费者远程调用服务报 TimeoutException 超时异常
SpringBoot 集成Dubbo 消费者远程调用服务报 TimeoutException 超时异常
713 0
|
Dubbo Java 应用服务中间件
源码分析Dubbo前置篇-寻找注册中心、服务提供者、服务消费者功能入口
本节主要阐述如下两个问题: Dubbo自定义标签实现。 dubbo通过Spring加载配置文件后,是如何触发注册中心、服务提供者、服务消费者按照Dubbo的设计执行相关的功能。 所谓的执行相关功能如下: 注册中心启动,监听消息提供者的注册服务、接收消息消费者的服务订阅(服务注册与发现机制)。
1891 0