爲何推薦Zookeeper做註冊中心

Zookeeper的數據模型很簡單,有一系列被稱爲ZNode的數據節點組成,與傳統的磁盤文件系統不一樣的是,zk將全量數據存儲在內存中,可謂是高性能,並且支持集羣,可謂高可用,另外支持事件監聽。這些特色決定了zk特別適合做爲註冊中心(數據發佈/訂閱)。

Zookeeper註冊中心

(+) (#)git

建議使用dubbo-2.3.3以上版本的zookeeper註冊中心客戶端
Zookeeper說明
Zookeeper是Apacahe Hadoop的子項目,是一個樹型的目錄服務,支持變動推送,適合做爲Dubbo服務的註冊中心,工業強度較高,可用於生產環境,並推薦使用,參見:http://zookeeper.apache.org
Zookeeper安裝
安裝方式參見: Zookeeper安裝手冊,只需搭一個原生的Zookeeper服務器,並將Quick Start中Provider和Consumer裏的conf/dubbo.properties中的dubbo.registry.addrss的值改成zookeeper://127.0.0.1:2181便可使用
可靠性聲明
阿里內部並無採用Zookeeper作爲註冊中心,而是使用本身實現的基於數據庫的註冊中心,即:Zookeeper註冊中心並無在阿里內部長時間運行的可靠性保障,此Zookeeper橋接實現只爲開源版本提供,其可靠性依賴於Zookeeper自己的可靠性。
兼容性聲明
因2.0.8最初設計的zookeeper存儲結構不能擴充不一樣類型的數據,2.0.9版本作了調整,因此不兼容,需所有改用2.0.9版本才行,之後的版本會保持兼容2.0.9。
2.2.0版本改成基於zkclient實現,需增長zkclient的依賴包,2.3.0版本增長了基於curator的實現,做爲可選實現策略。

流程說明:github

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

支持如下功能:redis

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

在provider和consumer中增長zookeeper客戶端jar包依賴:spring

< dependency >
     < groupId >org.apache.zookeeper</ groupId >
     < artifactId >zookeeper</ artifactId >
     < version >3.3.3</ version >
</ dependency >

或直接下載:http://repo1.maven.org/maven2/org/apache/zookeeper/zookeeper數據庫

支持zkclient和curator兩種Zookeeper客戶端實現:apache

ZKClient Zookeeper Registry

從2.2.0版本開始缺省爲zkclient實現,以提高zookeeper客戶端的健狀性。服務器

ZKClient是Datameer開源的一個Zookeeper客戶端實現,開源比較早,參見:https://github.com/sgroschupf/zkclient

缺省配置:數據結構

< dubbo:registry ... client = "zkclient" />

或:app

dubbo.registry.client=zkclient

或:maven

zookeeper://10.20.153.10:2181?client=zkclient

需依賴:

< dependency >
     < groupId >com.github.sgroschupf</ groupId >
     < artifactId >zkclient</ artifactId >
     < version >0.1</ version >
</ dependency >

或直接下載:http://repo1.maven.org/maven2/com/github/sgroschupf/zkclient

Curator Zookeeper Registry

從2.3.0版本開始支持可選curator實現。

Curator是Netflix開源的一個Zookeeper客戶端實現,比較活躍,參見:https://github.com/Netflix/curator

若是須要改成curator實現,請配置:

< dubbo:registry ... client = "curator" />

或:

dubbo.registry.client=curator

或:

zookeeper://10.20.153.10:2181?client=curator

需依賴:

< dependency >
     < groupId >com.netflix.curator</ groupId >
     < artifactId >curator-framework</ artifactId >
     < version >1.1.10</ version >
</ dependency >

或直接下載:http://repo1.maven.org/maven2/com/netflix/curator/curator-framework

Zookeeper單機配置:

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

Or:

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

Zookeeper集羣配置:

< dubbo:registry address = "zookeeper://10.20.153.10:2181?backup=10.20.153.11:2181,10.20.153.12:2181" />

Or:

< dubbo:registry protocol = "zookeeper" address = "10.20.153.10:2181,10.20.153.11:2181,10.20.153.12:2181" />

同一Zookeeper,分紅多組註冊中心:

< dubbo:registry id = "chinaRegistry" protocol = "zookeeper" address = "10.20.153.10:2181" group = "china" />
< dubbo:registry id = "intlRegistry" protocol = "zookeeper" address = "10.20.153.10:2181" group = "intl" />

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便可使用
Redis過時數據
經過心跳的方式檢測髒數據,服務器時間必須相同,而且對服務器有必定壓力。
可靠性聲明
阿里內部並無採用Redis作爲註冊中心,而是使用本身實現的基於數據庫的註冊中心,即:Redis註冊中心並無在阿里內部長時間運行的可靠性保障,此Redis橋接實現只爲開源版本提供,其可靠性依賴於Redis自己的可靠性。
從2.1.0版本開始支持

數據結構:

  • 使用Redis的Key/Map結構存儲數據。
    • 主Key爲服務名和類型。
    • Map中的Key爲URL地址。
    • Map中的Value爲過時時間,用於判斷髒數據,髒數據由監控中心刪除。(注意:服務器時間必需同步,不然過時檢測會不許確)
  • 使用Redis的Publish/Subscribe事件通知數據變動。
    • 經過事件的值區分事件類型:register, unregister, subscribe, unsubscribe。
    • 普通消費者直接訂閱指定服務提供者的Key,只會收到指定服務的register, unregister事件。
    • 監控中心經過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: 在客戶端同時寫入全部服務器,只讀取單臺,服務器端不須要同步,註冊中心集羣增大,性能壓力也會更大。

Config redis registry:

< dubbo:registry address = "redis://10.20.153.10:6379" />

Or:

< dubbo:registry address = "redis://10.20.153.10:6379?backup=10.20.153.11:6379,10.20.153.12:6379" />

Or:

< dubbo:registry protocol = "redis" address = "10.20.153.10:6379" />

Or:

< dubbo:registry protocol = "redis" address = "10.20.153.10:6379,10.20.153.11:6379,10.20.153.12:6379" />

Simple註冊中心

(+) (#)

Dogfooding
註冊中心自己就是一個普通的Dubbo服務,能夠減小第三方依賴,使總體通信方式一致。
適用性說明
此SimpleRegistryService只是簡單實現,不支持集羣,可做爲自定義註冊中心的參考,但不適合直接用於生產環境。

Export simple registry service:

<? xml version = "1.0" encoding = "UTF-8" ?>
     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 >

Reference the simple registry service:

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

Or:

< 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" />
相關文章
相關標籤/搜索