Concept introduction
Basic understanding of
- Redis publish subscribe is a message communication mode
- Redis has three roles in publishing subscriptions: publisher, subscriber, and channel
Realize the principle of
Take an easy to understand example: wechat public account
Now there are three characters
- A represents the creator (publisher) of A wechat public account (assume VC).
- B1,B2,B3…. Represents people who subscribe to VC (subscribers)
- S stands for channel, namely wechat public account itself.
So the relationship between them is shown here
Simple publish subscribe via Redis
The commands used to publish subscriptions
Specific operation
To implement Redis publishing and subscription, two Redis- CLI clients need to be enabled
- Subscriber: subscribe to a wechat public number VC
127.0.0.1:6379 > subscribe VC# Subscribe to a wechat official account VC
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "VC"(3)integer1)Copy the code
- Publisher: Publishes messages
127.0.0.1:6379 > publish VC"this is a good artical" Send the first message
(integer) 1
127.0.0.1:6379> publish VC "do you like to read it more than once" # Send the second message
(integer6379 > 1 127.0.0.1) :Copy the code
When you look at the subscriber, you will find that you have received the message published by the publisher