<返回更多

Dubbo高级特性应用之多注册中心

2020-07-18    
加入收藏

概述

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

多注册中心注册

首先dubbo:registry定义多个注册中心,每个注册中心使用id作为唯一标识;然后曝露服务时,在dubbo:service的registry属性上引用要注册的注册中心,多个注册中心id使用逗号分隔

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.Apache.org/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <!-- 服务提供方应用名称,方便用于依赖跟踪,另外,指定使用slf4j日志-->
    <dubbo:Application name="coffee-operation-center" logger="slf4j"/>

    <!-- 声明华南、华中、华北三个注册中心 -->
    <dubbo:registry id="huananRegistry" address="zookeeper://192.168.8.156:2181"/>
    <dubbo:registry id="huazhongRegistry" address="zookeeper://192.168.8.157:2181" default="false"/>
    <dubbo:registry id="huabeiRegistry" address="zookeeper://192.168.8.158:2181" default="false"/>

    <!-- 声明orderService的实现bean -->
    <bean id="orderService" class="com.fandou.coffee.order.service.OrderServiceImpl" />
    <!-- 曝露订单服务:注册到三个注册中心 -->
    <dubbo:service registry="huananRegistry,huazhongRegistry,huabeiRegistry" interface="com.fandou.coffee.api.order.OrderService" ref="orderService"/>
</beans>

不同服务注册到不同注册中心

比如支付服务,在不同的国家地区,可能使用不同的支付服务,有些支付服务可以跨国家地区,有些只能在本地区使用

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <!-- 服务提供方应用名称,方便用于依赖跟踪,另外,指定使用slf4j日志-->
    <dubbo:application name="coffee-operation-center" logger="slf4j"/>

    <!-- 声明美洲、亚洲、欧洲三个注册中心 -->
    <dubbo:registry id="americaRegistry" address="zookeeper://192.168.8.156:2181"/>
    <dubbo:registry id="asiaRegistry" address="zookeeper://192.168.8.157:2181" default="false"/>
    <dubbo:registry id="europeRegistry" address="zookeeper://192.168.8.158:2181" default="false"/>

    <!-- 多种在线支付服务 -->
    <bean id="weChatPayService" class="com.fandou.coffee.pay.service.WeChatPayServiceImpl" />
    <bean id="aliPayService" class="com.fandou.coffee.pay.service.AliPayServiceImpl" />
    <bean id="applePayService" class="com.fandou.coffee.pay.service.ApplePayServiceImpl" />
    <bean id="paypalPayService" class="com.fandou.coffee.pay.service.PaypalPayServiceImpl" />

    <!-- 微信、支付宝在亚洲注册中心,paypal在美洲和欧洲注册中心,applePay注册到全部注册中心-->
    <dubbo:service registry="asiaRegistry" group="pay.wechat" interface="com.fandou.coffee.api.pay.PayService" ref="weChatPayService"/>
    <dubbo:service registry="asiaRegistry" group="pay.ali" interface="com.fandou.coffee.api.pay.PayService" ref="aliPayService"/>
    <dubbo:service registry="americaRegistry,asiaRegistry,europeRegistry" group="pay.apply" interface="com.fandou.coffee.api.pay.PayService" ref="applePayService"/>

    <dubbo:service registry="americaRegistry,europeRegistry" group="pay.paypal" interface="com.fandou.coffee.api.pay.PayService" ref="paypalPayService"/>
</beans>

多注册中心引用

对于服务消费者方,需要用到哪个注册中心,就声明哪个注册中心,无需将全部注册中心进行声明。引用服务的时候,按需引用即可。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <!-- 服务消费方应用名称,方便用于依赖跟踪 -->
    <dubbo:application name="coffee-user-center" logger="slf4j">
        <dubbo:parameter key="qos.enable" value="true" />
        <dubbo:parameter key="qos.accept.foreign.ip" value="false" />
        <dubbo:parameter key="qos.port" value="33333" />
    </dubbo:application>

    <!-- 用到欧洲和亚洲两个注册中心 -->
    <dubbo:registry id="asiaRegistry" address="zookeeper://192.168.8.157:2181"/>
    <dubbo:registry id="europeRegistry" address="zookeeper://192.168.8.158:2181" default="false"/>

    <dubbo:protocol name="dubbo" port="20881"/>
    
    <!-- 不同的支付服务引用从不同的注册中心中引用 -->
    <dubbo:reference registry="asiaRegistry"  id="weChatPayService" group="pay.wechat" interface="com.fandou.coffee.api.pay.PayService" check="false" />
    <dubbo:reference registry="asiaRegistry"  id="aliPayService" group="pay.ali" interface="com.fandou.coffee.api.pay.PayService" check="false" />
    <dubbo:reference registry="europeRegistry"  id="applyPayService" group="pay.apply" interface="com.fandou.coffee.api.pay.PayService" check="false" />
</beans>

总结

在Dubbo中,一个服务可以向多个注册中心同时注册,不同服务可以分别注册到不同的注册中心上去。

Dubbo高级特性应用之多注册中心

dubbo的多注册中心应用

声明:本站部分内容来自互联网,如有版权侵犯或其他问题请与我们联系,我们将立即删除或处理。
▍相关推荐
更多资讯 >>>