1. 程式人生 > >Dubbo之多協議、多註冊中心、多版本。

Dubbo之多協議、多註冊中心、多版本。

多協議

         可以自行擴充套件協議。

不同服務不同協議

        比如,不同服務在效能上適用不同協議進行傳輸,比如大資料用短連線協議,小資料大併發用長連線協議。

<dubbo:application name="world" />
<dubbo:registry id="registry" address="10.20.141.150:9090" username="admin" password="hello1234" />
<!-- 多協議配置 -->
<dubbo:protocol name="dubbo" port="20880" />
<dubbo:protocol name="rmi" port="1099" />
<!-- 使用dubbo協議暴露服務 -->
<dubbo:service interface="com.alibaba.hello.api.HelloService" version="1.0.0" ref="helloService" protocol="dubbo" />
<!-- 使用rmi協議暴露服務 -->
<dubbo:service interface="com.alibaba.hello.api.DemoService" version="1.0.0" ref="demoService" protocol="rmi" />

多協議暴露服務

        比如:需要與http客戶端互操作。

<dubbo:application name="world" />
<dubbo:registry id="registry" address="10.20.141.150:9090" username="admin" password="hello1234" />
<!-- 多協議配置 -->
<dubbo:protocol name="dubbo" port="20880" />
<dubbo:protocol name="hessian" port="8080" />
<!-- 使用多個協議暴露服務 -->
<dubbo:service interface="com.alibaba.hello.api.HelloService" version="1.0.0" id="helloService" protocol="dubbo,hessian" />

多註冊中心

        可以自行擴充套件註冊中心。

多註冊中心註冊

        比如:中文站有些服務來不及在青島部署,只在杭州部署,而青島的其他應用需要引用此服務,就可以將服務同時註冊到兩個註冊中心。

<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,qinghaiRegistry" />

不同服務使用不同註冊中心

        比如:CRM需同時呼叫中文站和國際站的PC2服務,PC2在中文站和國際站均有部署,介面及版本號都一樣,但連的資料庫不一樣。

<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.HelloService" version="1.0.0" ref="demoService" registry="intlRegistry" />

多註冊中心引用

        比如:CRM需同時呼叫中心站和國際站的PC2服務,PC2在中文站和國際站均有部署,介面及版本號都一樣,但連的資料庫不一樣。

<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 interface="com.alibaba.hello.api.HelloService" version="1.0.0" id="helloService" registry="chinaRegistry" />
<!-- 向國際站註冊中心註冊 -->
<dubbo:service interface="com.alibaba.hello.api.HelloService" version="1.0.0" id="demoService" registry="intlRegistry" />

        如果只是測試環境臨時需要連線兩個不同註冊中心,使用豎號分隔多個不同註冊中心地址:

<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" />

多版本

        當一個介面實現,出現不相容升級時,可以用版本號過渡,版本號不同的服務相互間不引用。

  • 在低壓力時間段,先升級一半提供者為新版本
  • 再將所有消費者升級為新版本
  • 然後將剩下的一半提供者升級為新版本

<dubbo:service interface="com.foo.BarService" version="1.0.0" />
<dubbo:service interface="com.foo.BarService" version="2.0.0" />
<dubbo:reference id="barService" interface="com.foo.BarService" version="1.0.0" />
<dubbo:reference id="barService" interface="com.foo.BarService" version="2.0.0" />

        不區分版本:(2.2.0以上版本支援)

<dubbo:reference id="barService" interface="com.foo.BarService" version="*" />