Redis
Basic usage and working principle of the Redis registry.
Prerequisites
- Understand Dubbo basic development steps
- Install and start the Redis service
Instructions
Add Dependency
Starting from Dubbo3, the Redis registry adaptation is no longer embedded in Dubbo and needs to be separately introduced as an independent module.
<dependency>
<groupId>org.apache.dubbo.extensions</groupId>
<artifactId>dubbo-registry-redis</artifactId>
<version>3.3.0</version>
</dependency>
Basic Configuration
<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" />
Other Configuration Items
- You can set the prefix for the keys in Redis with
<dubbo:registry group="dubbo" />
, default isdubbo
. - You can set the Redis cluster policy with
<dubbo:registry cluster="replicate" />
, default isfailover
:failover
: Only write and read from any one, retry another on failure, data synchronization needs to be configured on the server side.replicate
: Write to all servers simultaneously from the client, read from one, no synchronization needed on the server side, the registration center cluster increases and so does performance pressure.
Working Principle
The registry is implemented based on Redis [^1].
Redis expired data is detected for dirty data through heartbeat; server time must be synchronized and there is certain pressure on the server, otherwise the expiration detection may be inaccurate.
Using Redis’s Key/Map structure to store data:
- 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, used to determine dirty data, dirty data is deleted by the monitoring center [^3].
Using Redis’s Publish/Subscribe event to notify data changes:
- Distinguish event types through the event value:
register
,unregister
,subscribe
,unsubscribe
. - Ordinary consumers subscribe directly to the Key of the specified service provider, only receiving
register
,unregister
events for that service. - The monitoring center subscribes to
/dubbo/*
throughpsubscribe
, receiving all change events for all services.
Call process:
- When the service provider starts, it adds the current provider’s address under
Key:/dubbo/com.foo.BarService/providers
. - And sends the
register
event toChannel:/dubbo/com.foo.BarService/providers
. - When the service consumer starts, it subscribes to
register
andunregister
events fromChannel:/dubbo/com.foo.BarService/providers
. - And adds the current consumer’s address under
Key:/dubbo/com.foo.BarService/consumers
. - The service consumer retrieves the provider address list from
Key:/dubbo/com.foo.BarService/providers
after receivingregister
andunregister
events. - When the service monitoring center starts, it subscribes to
register
,unregister
,subscribe
, andunsubscribe
events fromChannel:/dubbo/*
. - The service monitoring center retrieves the provider address list from
Key:/dubbo/com.foo.BarService/providers
after receivingregister
andunregister
events. - The service monitoring center retrieves the consumer address list from
Key:/dubbo/com.foo.BarService/consumers
after receivingsubscribe
andunsubscribe
events.
Feedback
Was this page helpful?
Yes No
Last modified September 30, 2024: Update & Translate Overview Docs (#3040) (d37ebceaea7)