当前位置:网站首页>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 .

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 .

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}
边栏推荐
- Serial port data frame
- Business is too busy. Is there really no reason to have time for automation?
- Insert sort, select sort, bubble sort
- SPSS installation and activation tutorial (including network disk link)
- 【lua】int64的支持
- md5工具类
- 特征缩放 标准化 归一化
- 醒悟的日子,我是怎么一步一步走向软件测试的道路
- 质量体系建设之路的分分合合
- Locust performance test - environment construction and use
猜你喜欢

串口数据帧

Redis入门完整教程:客户端通信协议

攻防世界 MISC 進階區 Erik-Baleog-and-Olaf

Li Kou 98: verify binary search tree

堆排序代码详解

Redis入門完整教程:Pipeline

Wake up day, how do I step by step towards the road of software testing

Redis入门完整教程:键管理

攻防世界 misc 进阶区 2017_Dating_in_Singapore

PMO: compare the sample efficiency of 25 molecular optimization methods
随机推荐
Attack and defense world misc advanced area Hong
High school physics: linear motion
Tla+ introductory tutorial (1): introduction to formal methods
Logo Camp d'entraînement section 3 techniques créatives initiales
Three stage operations in the attack and defense drill of the blue team
Google Earth Engine(GEE)——基于 MCD64A1 的 GlobFire 日常火灾数据集
蓝队攻防演练中的三段作战
业务太忙,真的是没时间搞自动化理由吗?
Redis入門完整教程:Pipeline
Introducing QA into the software development lifecycle is the best practice that engineers should follow
Talk about Middleware
新版判断PC和手机端代码,手机端跳转手机端,PC跳转PC端最新有效代码
Why is Dameng data called the "first share" of domestic databases?
Feature scaling normalization
剑指Offer 68 - II. 二叉树的最近公共祖先
Attack and defense world misc advanced zone 2017_ Dating_ in_ Singapore
Li Kou 98: verify binary search tree
9 - 类
It is said that software testing is very simple, but why are there so many dissuasions?
Install the gold warehouse database of NPC