Redis
The basic usage and working principle of the Redis registry.
precondition
- Understand Dubbo basic development steps
- Install and start the Redis service
Instructions for use
<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" />
options
- You can set the prefix of the key in redis through
<dubbo:registry group="dubbo" />
, the default isdubbo
. - The redis cluster strategy can be set via
<dubbo:registry cluster="replicate" />
, the default isfailover
:failover
: Only write and read any one, and retry another one when it fails, the server needs to configure data synchronization by itselfreplicate
: Write to all servers at the same time on the client side, only read a single server, the server side does not need to be synchronized, the registration center cluster increases, and the performance pressure will also be greater
working principle
A registry implemented based on Redis [^1].
Redis expired data detects dirty data through heartbeat, the server time must be synchronized, and there is a certain pressure on the server, otherwise the expiration detection will be inaccurate
Use Redis’s Key/Map structure to store data structures:
- The main key is the service name and type
- The Key in the Map is the URL address
- The Value in the Map is the expiration time, which is used to judge dirty data, and the dirty data will be deleted by the monitoring center [^3]
Use Redis’s Publish/Subscribe events to notify data changes:
- Differentiate the event type by the value of the event:
register
,unregister
,subscribe
,unsubscribe
- Ordinary consumers directly subscribe to the Key of the specified service provider, and will only receive
register
,unregister
events of the specified service - The monitoring center subscribes to
/dubbo/*
through thepsubscribe
function, and will receive all change events of all services
Call process:
- When the service provider starts, add the address of the current provider to
Key:/dubbo/com.foo.BarService/providers
- And send
register
event toChannel:/dubbo/com.foo.BarService/providers
- When the service consumer starts, subscribe to
register
andunregister
events fromChannel:/dubbo/com.foo.BarService/providers
- Add the address of the current consumer to
Key:/dubbo/com.foo.BarService/consumers
- After receiving the
register
andunregister
events, the service consumer obtains the provider address list fromKey:/dubbo/com.foo.BarService/providers
- When the service monitoring center starts, subscribe to
register
andunregister
, andsubscribe
andunsubscribe
events fromChannel:/dubbo/*
- After receiving the
register
andunregister
events, the service monitoring center obtains the provider address list fromKey:/dubbo/com.foo.BarService/providers
- After receiving the
subscribe
andunsubscribe
events, the service monitoring center obtains the consumer address list fromKey:/dubbo/com.foo.BarService/consumers
Last modified January 2, 2023: Enhance en docs (#1798) (95a9f4f6c1)