Redis provides a simple message service that supports publish/subscribe (publish/subscribe). Redis clients can subscribe to one or more channels (Channel), this behavior is called subscribe. Other Redis clients send messages to these channels, called publish, and clients subscribed to these channels can receive these messages.
The publish/subscribe mode can be applied to event notifications in Spring Boot, such as configuration file updates, cache updates, etc. You can also use the NoSQL feature of Redis as a data cache, and also use the Pub/Sub feature to issue cache update events, so that the Spring Boot application can update the cache.
Redis also provides a subscription specified pattern (pattern), use the psubscribe command
127.0.0.1:6379>psubscribe news.*
Subscribe to all channels starting with news
The supported modes are:
model | match description |
news.* | All channels starting with news. |
news-? | Subscribe to news-1, new2-2 channels |
news[123] | Subscribe to news-1, news-2, news-3 channels |
Subscribe to a news channel
127.0.0.1-6379:0>subscribe news
Switch to Pub/Sub mode. Close console tab to stop listen for messages.
1) "subscribe"
2) "news"
3) "1"
1) "message"
2) "news"
3) "hello"
1) "message"
2) "news"
3) "world"
The first line is the fixed string "subscribe"
The second line is the subscribed channel name
The third line is a number indicating how many total subscribers the channel has
make an announcement:
127.0.0.1-6379:0>publish news "hello"
"1"
127.0.0.1-6379:0>publish news "world"
"1"
The publish command sends a message to the terminal and returns an integer indicating how many subscribers received the message.