(四).dubbo註冊中心

1、註冊中心類型

根據Dubbo官方文檔,Dubbo支持多註冊中。

  • ZooKeeper註冊中心,也是官方建議使用的註冊方式;
  • Multicast註冊中心;
  • Redis註冊中心;
  • Simple註冊中心

1.1、ZooKeeper註冊中心

流程說明:

  • 服務提供者啓動時
    • 向/dubbo/com.foo.BarService/providers目錄下寫入自己的URL地址。
  • 服務消費者啓動時
    • 訂閱/dubbo/com.foo.BarService/providers目錄下的提供者URL地址。
    • 並向/dubbo/com.foo.BarService/consumers目錄下寫入自己的URL地址。
  • 監控中心啓動時
    • 訂閱/dubbo/com.foo.BarService目錄下的所有提供者和消費者URL地址。

支持以下功能:

  • 當提供者出現斷電等異常停機時,註冊中心能自動刪除提供者信息。
  • 當註冊中心重啓時,能自動恢復註冊數據,以及訂閱請求。
  • 當會話過期時,能自動恢復註冊數據,以及訂閱請求。
  • 當設置<dubbo:registry check="false" />時,記錄失敗註冊和訂閱請求,後臺定時重試。
  • 可通過<dubbo:registry username="admin" password="1234" />設置zookeeper登錄信息。
  • 可通過<dubbo:registry group="dubbo" />設置zookeeper的根節點,不設置將使用無根樹。
  • 支持*號通配符<dubbo:reference group="*" version="*" />,可訂閱服務的所有分組和所有版本的提供者。

官方建議使用ZooKeeper註冊中,後面繼續重點描述ZooKeeper註冊中心。

1.2、Muticast註冊中心

優點:
不需要啓動任何中心節點,只要廣播地址一樣,就可以互相發現
缺點:
組播受網絡結構限制,只適合小規模應用或開發階段使用。
組播地址段: 224.0.0.0 - 239.255.255.255

  1. 提供方啓動時廣播自己的地址。
  2. 消費方啓動時廣播訂閱請求。
  3. 提供方收到訂閱請求時,單播自己的地址給訂閱者,如果設置了unicast=false,則廣播給訂閱者。
  4. 消費方收到提供方地址時,連接該地址進行RPC調用。

配置方式:

  • <dubbo:registry address="multicast://224.5.6.7:1234" />
  • <dubbo:registry protocol="multicast" address="224.5.6.7:1234" /> 

爲了減少廣播量,Dubbo缺省使用單播發送提供者地址信息給消費者,
如果一個機器上同時啓了多個消費者進程,消費者需聲明unicast=false,否則只會有一個消費者能收到消息:
<dubbo:registry address="multicast://224.5.6.7:1234?unicast=false" />或
<dubbo:registry protocol="multicast" address="224.5.6.7:1234">
    <dubbo:parameter key="unicast" value="false" />
</dubbo:registry>

1.3、redis註冊中心

優點:
           Redis說明:Redis是一個高效的KV存儲服務器,參見:http://redis.io
           Redis安裝:安裝方式參見: Redis安裝手冊,只需搭一個原生的Redis服務器,並將Quick Start中Provider和Consumer裏的conf/dubbo.properties中的dubbo.registry.addrss的值改爲redis://127.0.0.1:6379即可使用
缺點:
          1.Redis過期數據:通過心跳的方式檢測髒數據,服務器時間必須相同,並且對服務器有一定壓力。
          2.可靠性聲明:阿里內部並沒有採用Redis做爲註冊中心,而是使用自己實現的基於數據庫的註冊中心,即:Redis註冊中心並沒有在阿里內部長時間運行的可靠性保障,此Redis橋接實現只爲開源版本提供,其可靠性依賴於Redis本身的可靠性。
         3. 從2.1.0版本開始支持
數據結構:
使用Redis的Key/Map結構存儲數據。
          1.主Key爲服務名和類型。
          2.Map中的Key爲URL地址。
3.Map中的Value爲過期時間,用於判斷髒數據,髒數據由監控中心刪除。(注意:服務器時間必需同步,否則過期檢測會 不準確)
使用Redis的Publish/Subscribe事件通知數據變更。
1.通過事件的值區分事件類型:register, unregister, subscribe, unsubscribe。
2.普通消費者直接訂閱指定服務提供者的Key,只會收到指定服務的register, unregister事件。

3.監控中心通過psubscribe功能訂閱/dubbo/*,會收到所有服務的所有變更事件。

調用過程:

  1. 服務提供方啓動時,向Key:/dubbo/com.foo.BarService/providers下,添加當前提供者的地址。
  2. 並向Channel:/dubbo/com.foo.BarService/providers發送register事件。
  3. 服務消費方啓動時,從Channel:/dubbo/com.foo.BarService/providers訂閱register和unregister事件。
  4. 並向Key:/dubbo/com.foo.BarService/providers下,添加當前消費者的地址。
  5. 服務消費方收到register和unregister事件後,從Key:/dubbo/com.foo.BarService/providers下獲取提供者地址列表。
  6. 服務監控中心啓動時,從Channel:/dubbo/*訂閱register和unregister,以及subscribe和unsubsribe事件。
  7. 服務監控中心收到register和unregister事件後,從Key:/dubbo/com.foo.BarService/providers下獲取提供者地址列表。
  8. 服務監控中心收到subscribe和unsubsribe事件後,從Key:/dubbo/com.foo.BarService/consumers下獲取消費者地址列表。

選項:
可通過<dubbo:registry group="dubbo" />設置redis中key的前綴,缺省爲dubbo。
可通過<dubbo:registry cluster="replicate" />設置redis集羣策略,缺省爲failover。
      failover: 只寫入和讀取任意一臺,失敗時重試另一臺,需要服務器端自行配置數據同步。
      replicate: 在客戶端同時寫入所有服務器,只讀取單臺,服務器端不需要同步,註冊中心集羣增大,性能壓力也會更大。
註冊配置:

  1. <dubbo:registry address="redis://10.20.153.10:6379?backup=10.20.153.11:6379,10.20.153.12:6379" />
  2. <dubbo:registry address="redis://10.20.153.10:6379" />
  3. <dubbo:registry protocol="redis" address="10.20.153.10:6379,10.20.153.11:6379,10.20.153.12:6379" />
  4. <dubbo:registry protocol="redis" address="10.20.153.10:6379" />

 

1.4、Simple註冊中心

註冊中心本身就是一個普通的Dubbo服務,可以減少第三方依賴,使整體通訊方式一致。
此SimpleRegistryService只是簡單實現,不支持集羣,可作爲自定義註冊中心的參考,但不適合直接用於生產環境。

配置:

<?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://code.alibabatech.com/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
 
    <!-- 當前應用信息配置 -->
    <dubbo:application name="simple-registry" />
 
    <!-- 暴露服務協議配置 -->
    <dubbo:protocol port="9090" />
 
    <!-- 暴露服務配置 -->
    <dubbo:service interface="com.alibaba.dubbo.registry.RegistryService" ref="registryService" registry="N/A" ondisconnect="disconnect" callbacks="1000">
        <dubbo:method name="subscribe"><dubbo:argument index="1" callback="true" /></dubbo:method>
        <dubbo:method name="unsubscribe"><dubbo:argument index="1" callback="false" /></dubbo:method>
    </dubbo:service>
 
    <!-- 簡單註冊中心實現,可自行擴展實現集羣和狀態同步 -->
    <bean id="registryService" class="com.alibaba.dubbo.registry.simple.SimpleRegistryService" />
 
</beans>

引用:

<dubbo:registry address="127.0.0.1:9090" />

<dubbo:service interface="com.alibaba.dubbo.registry.RegistryService" group="simple" version="1.0.0" ... >

<dubbo:registry address="127.0.0.1:9090" group="simple" version="1.0.0" />

2、Zookeeper註冊中心

因爲官方建議使用Zookeeper註冊中心,主要是ZooKeeper的註冊中心的配置形式

2.1、單機配置

最簡單的一種配置,如下兩種方式進行配置:

  • <dubbo:registry address="zookeeper://10.20.153.10:2181" />
  • <dubbo:registry protocol="zookeeper" address="10.20.153.10:2181" />

2.2、ZooKeeper集羣配置

就是將ZooKeeper集羣配置到Dubbo配置文件中,主要爲了某個ZooKeeper掛了,不影響Dubbo的正常使用。也有兩種配置方式如下:

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

2.3、多註冊中心

2.3.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="http://code.alibabatech.com/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://code.alibabatech.com/schema/dubbohttp://code.alibabatech.com/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>

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

應用場景:
    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://code.alibabatech.com/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://code.alibabatech.com/schema/dubbohttp://code.alibabatech.com/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>

2.3.3多註冊中心引用

應用場景:
     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://code.alibabatech.com/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://code.alibabatech.com/schema/dubbohttp://code.alibabatech.com/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>

 

3.<dubbo:registry/>

dubbo註冊的其他屬性,暫時都沒有使用到,貼出官方的屬性的用法。

註冊中心配置:

配置類:com.alibaba.dubbo.config.RegistryConfig
說明:如果有多個不同的註冊中心,可以聲明多個<dubbo:registry>標籤,並在<dubbo:service>或<dubbo:reference>的registry屬性指定使用的註冊中心。

標籤 屬性 對應URL參數 類型 是否必填 缺省值 作用 描述 兼容性
<dubbo:registry> id   string 可選   配置關聯 註冊中心引用BeanId,可以在<dubbo:service registry="">或<dubbo:reference registry="">中引用此ID 1.0.16以上版本
<dubbo:registry> address <host:port> string 必填   服務發現 註冊中心服務器地址,如果地址沒有端口缺省爲9090,同一集羣內的多個地址用逗號分隔,如:ip:port,ip:port,不同集羣的註冊中心,請配置多個<dubbo:registry>標籤 1.0.16以上版本
<dubbo:registry> protocol <protocol> string 可選 dubbo 服務發現 注同中心地址協議,支持dubbo, http, local三種協議,分別表示,dubbo地址,http地址,本地註冊中心 2.0.0以上版本
<dubbo:registry> port <port> int 可選 9090 服務發現 註冊中心缺省端口,當address沒有帶端口時使用此端口做爲缺省值 2.0.0以上版本
<dubbo:registry> username <username> string 可選   服務治理 登錄註冊中心用戶名,如果註冊中心不需要驗證可不填 2.0.0以上版本
<dubbo:registry> password <password> string 可選   服務治理 登錄註冊中心密碼,如果註冊中心不需要驗證可不填 2.0.0以上版本
<dubbo:registry> transport registry.transporter string 可選 netty 性能調優 網絡傳輸方式,可選mina,netty 2.0.0以上版本
<dubbo:registry> timeout registry.timeout int 可選 5000 性能調優 註冊中心請求超時時間(毫秒) 2.0.0以上版本
<dubbo:registry> session registry.session int 可選 60000 性能調優 註冊中心會話超時時間(毫秒),用於檢測提供者非正常斷線後的髒數據,比如用心跳檢測的實現,此時間就是心跳間隔,不同註冊中心實現不一樣。 2.1.0以上版本
<dubbo:registry> file registry.file string 可選   服務治理 使用文件緩存註冊中心地址列表及服務提供者列表,應用重啓時將基於此文件恢復,注意:兩個註冊中心不能使用同一文件存儲 2.0.0以上版本
<dubbo:registry> wait registry.wait int 可選 0 性能調優 停止時等待通知完成時間(毫秒) 2.0.0以上版本
<dubbo:registry> check check boolean 可選 true 服務治理 註冊中心不存在時,是否報錯 2.0.0以上版本
<dubbo:registry> register register boolean 可選 true 服務治理 是否向此註冊中心註冊服務,如果設爲false,將只訂閱,不註冊 2.0.5以上版本
<dubbo:registry> subscribe subscribe boolean 可選 true 服務治理 是否向此註冊中心訂閱服務,如果設爲false,將只註冊,不訂閱 2.0.5以上版本
<dubbo:registry> dynamic dynamic boolean 可選 true 服務治理 服務是否動態註冊,如果設爲false,註冊後將顯示後disable狀態,需人工啓用,並且服務提供者停止時,也不會自動取消冊,需人工禁用。 2.0.5以上版本

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章