多個註冊中心

在 Dubbo 中將同一服務註冊到多個註冊中心

功能說明

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:dubbo="https://dubbo.dev.org.tw/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd https://dubbo.dev.org.tw/schema /dubbo https://dubbo.dev.org.tw/schema/dubbo/dubbo.xsd">
    <dubbo:application name="world" />
    <!-- Multi-registry configuration -->
    <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" />
</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="https://dubbo.dev.org.tw/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd https://dubbo.dev.org.tw/schema /dubbo https://dubbo.dev.org.tw/schema/dubbo/dubbo.xsd">
    <dubbo:application name="world" />
    <!-- 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" />
</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="https://dubbo.dev.org.tw/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd https://dubbo.dev.org.tw/schema /dubbo https://dubbo.dev.org.tw/schema/dubbo/dubbo.xsd">
    <dubbo:application name="world" />
    <!-- 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" />
</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="https://dubbo.dev.org.tw/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd https://dubbo.dev.org.tw/schema /dubbo https://dubbo.dev.org.tw/schema/dubbo/dubbo.xsd">
    <dubbo:application name="world" />
    <!-- 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" />
</beans>

  1. 您可以自行擴充註冊中心,請參閱:註冊中心擴充 ↩︎


上次修改時間:2023 年 1 月 2 日:增強英文文件 (#1798) (95a9f4f6c1c)