当前位置:网站首页>Redis getting started complete tutorial: publish and subscribe

Redis getting started complete tutorial: publish and subscribe

2022-07-04 22:53:00 Gu Ge academic

Redis Provides the basis for “ Release / subscribe ” Message mechanism of pattern , In this mode , News release
The subscriber and the subscriber do not communicate directly , The publisher client sends the specified channel (channel) Release cancellation
Rest , Every client subscribing to this channel can receive this message , Pictured 3-16 Shown .Redis carry
Several commands are provided to support this function , In practical application development , Can provide implementers for such problems
Law .

 

3.7.1  command
Redis It mainly provides information release 、 Subscribed Channels 、 Unsubscribe and subscribe to and in accordance with the pattern
Unsubscribe and other commands .
1. Release the news
publish channel message
The following operation will give channel:sports The channel has a message “Tim won the
championship”, The return result is the number of subscribers , Because there is no subscription at this time , So return the result
by 0:
127.0.0.1:6379> publish channel:sports "Tim won the championship"
(integer) 0
2. Subscribe to news
subscribe channel [channel ...]
Subscribers can subscribe to one or more channels , The following operation subscribes for the current client
channel:sports channel :
127.0.0.1:6379> subscribe channel:sports
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "channel:sports"
3) (integer) 1
At this time, another client releases a message :

127.0.0.1:6379> publish channel:sports "James lost the championship"
(integer) 1
The current subscriber client will receive the following message :
127.0.0.1:6379> subscribe channel:sports
Reading messages... (press Ctrl-C to quit)
...
1) "message"
2) "channel:sports"
3) "James lost the championship"
If there are multiple clients subscribed to channel:sports, The whole process is as shown in the figure 3-17 the
in .
There are two points to note about subscription commands :
· The client enters the subscription state after executing the subscription command , receive calls only subscribe、
psubscribe、unsubscribe、punsubscribe The four orders of .
· New subscription client , Unable to receive previous messages from this channel , because Redis It won't be right
Persistence of published messages . 

chart 3-17 Multiple clients subscribe to channels at the same time channel:sports

 

Development tips
And many professional message queuing systems ( for example Kafka、RocketMQ) comparison ,Redis The hair of
The cloth is slightly rough , For example, message stacking and backtracking cannot be realized . But it's simple enough , If so
These shortcomings can be tolerated in the previous scenario , It's a good choice .
3. Unsubscribe
unsubscribe [channel [channel ...]]
The client can use the unsubscribe Command to unsubscribe from the specified channel , After the cancellation is successful ,
No more announcements from this channel :
127.0.0.1:6379> unsubscribe channel:sports
1) "unsubscribe"
2) "channel:sports"
3) (integer) 0
4. Subscribe and unsubscribe according to the pattern
psubscribe pattern [pattern...]
punsubscribe [pattern [pattern ...]]
except subcribe and unsubscribe command ,Redis The command also supports glob Style subscription command
psubscribe And unsubscribe commands punsubscribe, For example, the following operation subscribes to it All of the beginning
channel :
127.0.0.1:6379> psubscribe it*
Reading messages... (press Ctrl-C to quit)
1) "psubscribe"
2) "it*"
3) (integer) 1
5. Query subscription
(1) View active channels
pubsub channels [pattern]
The so-called active channel means that the current channel has at least one subscriber , among [pattern] Yes.
Specify the specific mode :
127.0.0.1:6379> pubsub channels
1) "channel:sports"
2) "channel:it"
3) "channel:travel"
127.0.0.1:6379> pubsub channels channel:*r*
1) "channel:sports"
2) "channel:travel"
(2) View channel subscriptions
pubsub numsub [channel ...]
At present channel:sports The number of subscriptions for the channel is 2:
127.0.0.1:6379> pubsub numsub channel:sports
1) "channel:sports"
2) (integer) 2
(3) View the number of mode subscriptions
pubsub numpat
At present, only one client subscribes through the mode :
127.0.0.1:6379> pubsub numpat
(integer) 1 

3.7.2  Use scenarios
The chat room 、 Bulletin board 、 Using message decoupling between services can use publish subscribe mode , Next
The surface is illustrated by simple service decoupling . Pictured 3-18 Shown , There are two sets of business , The above is regarded as
Frequency management system , Responsible for managing video information ; Here is the video service for customers , The user can go through
Various clients ( mobile phone 、 browser 、 Interface ) Get video information .

chart 3-18 Publish and subscribe for video information change notification

 

If the video administrator changes the video information in the video management system , Hope to get through in time
Tell the video server , You can use publish subscribe mode , Release the message of video information change to
Specify channel , Video service subscribes to this channel to update video information in time , In this way, there can be
Effectively solve the coupling of two services .
· Video service subscription video:changes The channels are as follows :
subscribe video:changes
· The video management system publishes a message to video:changes The channels are as follows :
publish video:changes "video1,video3,video5"
· When the video service receives a message , Update the video information , As shown below :
for video in video1,video3,video5
update {video} 

原网站

版权声明
本文为[Gu Ge academic]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207042229263196.html