Dubbo注册中心Resitry之Redis

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

数据结构
使用Redis的Hash结构存储数据
- 主Key为服务名和类型。
- Map中的Key为URL地址。
- Map中的Value为过期时间,用于判断脏数据,脏数据由监控中心删除。
使用Redis的Publish/Subscribe事件通知数据变更
- 通过事件的值区分事件类型:register、unregister、subscribe、unsubscribe。
- consumer订阅指定的provider,只会收到指定provider的register、unregister事件。
- 监控中心订阅/dubbo/*,会收到所有服务的所有变更事件。
调用过程
- provider启动时,向Key:/dubbo/top.guoj.api.HelloService/providers下,添加当前提供者的地址。
- 并向Channel:/dubbo/top.guoj.api.HelloService/providers发送register事件。
- 服务消费方启动时,从Channel:/dubbo/com.foo.BarService/consumers订阅register和unregister事件。
- 并向Key:/dubbo/com.foo.BarService/consumers下,添加当前消费者的地址。
- 服务消费方收到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下获取消费者地址列表。
启动provdier
127.0.0.1:6379> keys *
1) "/dubbo/top.guoj.api.HelloService/providers"
127.0.0.1:6379> type /dubbo/top.guoj.api.HelloService/providers
hash
127.0.0.1:6379> hgetall /dubbo/top.guoj.api.HelloService/providers
1) "dubbo://192.168.100.61:20880/top.guoj.api.HelloService?anyhost=true&application=demo-provider&dubbo=2.6.2&generic=false&interface=top.guoj.api.HelloService&methods=sayHello&pid=5252&side=provider×tamp=1533106855687"
2) "1533106976445"
启动consumer
127.0.0.1:6379> keys *
1) "/dubbo/top.guoj.api.HelloService/consumers"
2) "/dubbo/top.guoj.api.HelloService/providers"
127.0.0.1:6379> type /dubbo/top.guoj.api.HelloService/consumers
hash
127.0.0.1:6379> hgetall /dubbo/top.guoj.api.HelloService/consumers
1) "consumer://192.168.100.61/top.guoj.api.HelloService?application=dubbo-consumer&category=consumers&check=false&dubbo=2.6.2&interface=top.guoj.api.HelloService&methods=sayHello&pid=7196&side=consumer×tamp=1533107247126"
2) "1533107337246"
127.0.0.1:6379>
选项
- 可通过<dubbo:registry group=“dubbo”/>设置redis中key的前缀,缺省为dubbo。
- 可通过<dubbo:registry cluster=“replicate”/>设置redis集群策略,缺省为failover。
- failover: 只写入和读取任意一台,失败时重试另一台,需要服务器端自行配置数据同步。
- replicate: 在客户端同时写入所有服务器,只读取单台,服务器端不需要同步,注册中心集群增大,性能压力也会更大。