Dubbo之旅--扩展注册中心

 

     在上篇文章中我们介绍了关于协议的扩展,并了解扩展它所需要的需求.本篇主要是对注册中心的扩展进行着重的探索.

 

      同样的问题,为什么我们需要去扩展注册中心的?主要有以下三个需求.

 

(1) 多注册中心注册

 

需求:xx银行有些服务来不及在上海部署,只在北京部署,而上海的其它应用需要引用此服务,就可以将服务同时注册到两个注册中心。

 

consumer.xml

 

<?xmlversion="1.0"encoding="UTF-8"?>

<beansxmlns=" http://www.springframework.org/schema/beans"

    xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance"

    xmlns:dubbo=" http://code.alibabatech.com/schema/dubbo"

    xsi:schemaLocation=" http://www.springframework.org/schema/beans

         http://www.springframework.org/schema/beans/spring-beans.xsd

         http://code.alibabatech.com/schema/dubbo

         http://code.alibabatech.com/schema/dubbo/dubbo.xsd

        ">

 

    <dubbo:applicationname="world"/>

 

    <!-- 多注册中心配置 -->

    <dubbo:registryid="beijingRegistry"address="10.20.141.150:9090"/>

    <dubbo:registryid="shanghaiRegistry"address="10.20.141.151:9010"default="false"/>

 

    <!-- 向多个注册中心注册 -->

   <dubbo:serviceinterface="com.alibaba.hello.api.HelloService"version="1.0.0"ref="helloService"registry="beijingRegistry,shanghaiRegistry"/>

 

</beans>

 

          以上的工作便是扩展了注册中心,多注册中心注册,HelloService的服务同时注册到上海和北京的注册中心

 

(2) 不同服务使用不同注册中心

 

需求:xx银行有些服务是专门为国外设计的,有些服务是专门为国内设计的。

 

consumer.xml

 

<?xmlversion="1.0"encoding="UTF-8"?>

<beansxmlns=" http://www.springframework.org/schema/beans"

    xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance"

    xmlns:dubbo=" http://code.alibabatech.com/schema/dubbo"

    xsi:schemaLocation=" http://www.springframework.org/schema/beans

         http://www.springframework.org/schema/beans/spring-beans.xsd

         http://code.alibabatech.com/schema/dubbo

         http://code.alibabatech.com/schema/dubbo/dubbo.xsd

        ">

 

    <dubbo:applicationname="world"/>

 

    <!-- 多注册中心配置 -->

   <dubbo:registryid="chinaRegistry"address="10.20.141.150:9090"/>

   <dubbo:registryid="intlRegistry"address="10.20.154.177:9010"default="false"/>

 

    <!-- 向国内注册中心注册 -->

   <dubbo:serviceinterface="com.alibaba.hello.api.HelloService"version="1.0.0"ref="helloService"registry="chinaRegistry"/>

 

    <!-- 向国外注册中心注册 -->

   <dubbo:serviceinterface="com.alibaba.hello.api.DemoService"version="1.0.0"ref="demoService"registry="intlRegistry"/>

 

</beans>

 

        不同服务使用不同注册中心是注册中心扩展的第二个需求,当然这个内容对于开发者而言非常有用,尤其是在本地调试进行开发的时候一些服务是我本地所不能提供的,这时候这种需求就需要我们扩展注册中心.

 

(3) 多注册中心引用

 

需求:xx银行需同时调用国内和国外的xxx服务,xxx服务在中文站和国际站均有部署,接口及版本号都一样,但连的数据库不一样。

 

consumer.xml

 

<?xmlversion="1.0"encoding="UTF-8"?>

<beansxmlns=" http://www.springframework.org/schema/beans"

    xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance"

    xmlns:dubbo=" http://code.alibabatech.com/schema/dubbo"

    xsi:schemaLocation=" http://www.springframework.org/schema/beans

         http://www.springframework.org/schema/beans/spring-beans.xsd

         http://code.alibabatech.com/schema/dubbo

         http://code.alibabatech.com/schema/dubbo/dubbo.xsd

        ">

 

    <dubbo:applicationname="world"/>

 

    <!-- 多注册中心配置 -->

   <dubbo:registryid="chinaRegistry"address="10.20.141.150:9090"/>

   <dubbo:registryid="intlRegistry"address="10.20.154.177:9010"default="false"/>

 

    <!-- 引用中文站服务 -->

   <dubbo:referenceid="chinaHelloService"interface="com.alibaba.hello.api.HelloService"version="1.0.0"registry="chinaRegistry"/>

 

    <!-- 引用国际站站服务 -->

   <dubbo:referenceid="intlHelloService"interface="com.alibaba.hello.api.HelloService"version="1.0.0"registry="intlRegistry"/>

 

</beans>

 

 

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

 

consumer.xml

 

<?xmlversion="1.0"encoding="UTF-8"?>

<beansxmlns=" http://www.springframework.org/schema/beans"

    xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance"

    xmlns:dubbo=" http://code.alibabatech.com/schema/dubbo"

    xsi:schemaLocation=" http://www.springframework.org/schema/beans

         http://www.springframework.org/schema/beans/spring-beans.xsd

         http://code.alibabatech.com/schema/dubbo

         http://code.alibabatech.com/schema/dubbo/dubbo.xsd

        ">

 

    <dubbo:applicationname="world"/>

 

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

   <dubbo:registryaddress="10.20.141.150:9090|10.20.154.177:9010"/>

 

    <!-- 引用服务 -->

   <dubbo:referenceid="helloService"interface="com.alibaba.hello.api.HelloService"version="1.0.0"/>

 

</beans>

 

 

         通过对扩展注册中心和扩展协议的了解,dubbo本身还是非常灵活的.当然,这里的协议和注册中心只是它多个可扩展内容的一部分.它还具有集群扩展,线程池扩展,缓存扩展,容器扩展等等丰富的扩展内容.其他的扩展内容我在项目中还未实际用到.或许会在以后更为特殊的需求上面用到他们.

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值