Zookeeper的數據模型很簡單,有一系列被稱爲ZNode的數據節點組成,與傳統的磁盤文件系統不一樣的是,zk將全量數據存儲在內存中,可謂是高性能,並且支持集羣,可謂高可用,另外支持事件監聽。這些特色決定了zk特別適合做爲註冊中心(數據發佈/訂閱)。
Zookeeper註冊中心
流程說明: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客戶端的健狀性。服務器
缺省配置:數據結構
<
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實現,請配置:
<
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的Key/Map結構存儲數據。
- 主Key爲服務名和類型。
- Map中的Key爲URL地址。
- Map中的Value爲過時時間,用於判斷髒數據,髒數據由監控中心刪除。(注意:服務器時間必需同步,不然過時檢測會不許確)
- 使用Redis的Publish/Subscribe事件通知數據變動。
- 經過事件的值區分事件類型:register, unregister, subscribe, unsubscribe。
- 普通消費者直接訂閱指定服務提供者的Key,只會收到指定服務的register, unregister事件。
- 監控中心經過psubscribe功能訂閱/dubbo/*,會收到全部服務的全部變動事件。
調用過程:
- 服務提供方啓動時,向Key:/dubbo/com.foo.BarService/providers下,添加當前提供者的地址。
- 並向Channel:/dubbo/com.foo.BarService/providers發送register事件。
- 服務消費方啓動時,從Channel:/dubbo/com.foo.BarService/providers訂閱register和unregister事件。
- 並向Key:/dubbo/com.foo.BarService/providers下,添加當前消費者的地址。
- 服務消費方收到register和unregister事件後,從Key:/dubbo/com.foo.BarService/providers下獲取提供者地址列表。
- 服務監控中心啓動時,從Channel:/dubbo/*訂閱register和unregister,以及subscribe和unsubsribe事件。
- 服務監控中心收到register和unregister事件後,從Key:/dubbo/com.foo.BarService/providers下獲取提供者地址列表。
- 服務監控中心收到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註冊中心
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"
/>
|