当前位置:网站首页>How to publish and subscribe messages with redis? What is the implementation principle?
How to publish and subscribe messages with redis? What is the implementation principle?
2022-07-20 22:25:00 【I am a cabbage】
Hello everyone , I'm cabbage . This article is mainly for explanation Redis Publish and subscribe messages , If you get something after reading the article , You can support bloggers three times in a row ~, Hee hee .
List of articles
One 、 What is publish and subscribe
Author's brief introduction : One for college students ,Java New star creators in the field ,Java、Python Learning , I look forward to learning and making progress with you ~
Personal home page : I am a cabbage's personal homepage
This column :Redis Theory and Practice
Remind yourself : Learn more, practice more, think more , Programming ability can be improved !
Redis Publish subscribe (pub/sub) It's a kind of
Message communication mode
: sender (pub) Send a message , subscriber (sub) receive messages .characteristic :
Redis Clients can subscribe to any number of channels .It's like fans pay attention to me , When I finish writing the article and publish it , You open it CSDN You will also receive my articles . here , I am the sender (pub); Fans are subscribers (sub).
Structure diagram is as follows : The publisher sends his message to redis The server , Subscribers from redis Get the message published by the publisher from the server
Two 、Redis Publish and subscribe to
To facilitate the later explanation of the use of publish and subscribe command lines , Let me introduce briefly with two pictures redis Realize the overall process of publishing and subscribing .
The client can subscribe to channels, as shown in the figure below :
- When you post a message to this channel , The message is sent to the subscribing client , Here's the picture :
3、 ... and 、redis Publish and subscribe to common commands
- The following table lists them redis Publish and subscribe to common commands :
Four 、 Order actual combat
- In the demo redis Before publishing and subscribing to messages , We need to open two clients , One acts as a News Publisher , One acts as a message recipient .
1、 Basic use
- First use a client to subscribe to the channel
csdn_blog
, Waiting for news :
127.0.0.1:6379> SUBSCRIBE csdn_blog
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "csdn_blog"
3) (integer) 1
- Then a client is
csdn_blog
Channel sends messageshello,cabbage
:
127.0.0.1:6379> PUBLISH csdn_blog "hello,cabbage"
(integer) 1
- Finally, the receiver receives the message :
1) "message"
2) "csdn_blog" # The channel where the message is received
3) "hello,cabbage" # Messages received
2、 Subscribe to qualified channels
- Subscribe to
csdn
The first channel ,*
For any character
127.0.0.1:6379> PSUBSCRIBE csdn*
Reading messages... (press Ctrl-C to quit)
1) "psubscribe"
2) "csdn*"
3) (integer) 1
- Publishers post messages on two channels :
127.0.0.1:6379> PUBLISH csdn_blog "hello"
(integer) 1
127.0.0.1:6379> PUBLISH csdn_sing "sing"
(integer) 1
- Finally, subscribers receive messages from two channels :
1) "pmessage"
2) "csdn*"
3) "csdn_blog"
4) "hello"
1) "pmessage"
2) "csdn*"
3) "csdn_sing"
4) "sing"
3、 View active channels
- Subscribers subscribe to two channels :
127.0.0.1:6379> SUBSCRIBE csdn_one csdn_two
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "csdn_one"
3) (integer) 1
1) "subscribe"
2) "csdn_two"
3) (integer) 2
- Publishers send messages on three channels :
127.0.0.1:6379> PUBLISH csdn_one "one"
(integer) 1
127.0.0.1:6379> PUBLISH csdn_two "two"
(integer) 1
127.0.0.1:6379> PUBLISH csdn_three "three"
(integer) 0
- Show active channels on the publisher client :
127.0.0.1:6379> PUBSUB channels
1) "csdn_one"
2) "csdn_two"
5、 ... and 、 Principle of publishing and subscribing
- Every Redis The server processes all maintain a... That represents the state of the server
redis.h
/redisServer
structure , Structuralpubsub_channels
Property is a dictionary , This dictionary is used to save subscription channel information , among , DictionaryKey is the channel being subscribed to
, And the dictionary isThe value is a linked list , All clients subscribing to this channel are saved in the linked list
.
1、 Subscription channel principle
- Suppose there is a channel as shown in the figure below and a client subscribing to the channel : It is clear from the picture ,channelA Three people subscribe to the channel ;channelB No one subscribes to the channel ;channelC Two people subscribe to the channel .
- When the client
clientF
callSUBSCRIBE
On command , The program will place the client and the channel to be subscribed inpubsub_channels
It's related in the dictionary . Execute the following instructions , The corresponding result graph is :
127.0.0.1:6379> SUBSCRIBE channelB channelC channelA
- Conclusion : adopt
pubsub_channels
Dictionaries , The program just checks whether a channel is the key of the dictionary , You can know if the channel is being subscribed by the client ; Just take the value of a key , You can get the information of all clients who subscribe to the channel .
2、 Principle of publishing information
Principle that :
When callingPUBLISH channel message
command , The procedure is based first onchannel
Key to dictionary , Then send the information to all clients in the dictionary value chain .For the image above , If the client channelA Carry out orders PUBLISH channelA “hello,cabbage”, that
clientA
,clientB
,clientC
,clientF
All four clients will receive "hello,cabbage" Information , By traversing all clients of the subscription channel .
3、 Unsubscribe information principle
principle :
UseUNSUBSCRIBE
The command can unsubscribe the specified channel , This command performs the reverse operation of subscription : It is frompubsub_channels
Given channel of Dictionary ( key ) in , Delete information about the current client , In this way, the channel information will not be sent to this client .
Reference article :http://t.zoukankan.com/rxbook-p-12652198.html
Thank you for reading , Progress together , Hee hee ~
边栏推荐
- 免费看知乎盐选
- 架构实战营第7模块作业
- [Bi design teaching] how to run the program in SD card by single chip microcomputer
- 2022徐特立科学营&BIT机器人队电控课程讲义
- ES6-11 学习笔记
- MES管理系统解决方案的参照标准是什么
- 史上最全的 IDEA Debug 调试技巧(超详细案例)
- Detailed interpretation: the use of reflection
- 连接无限·协同无界|融云首届全球企业通信云大会 WECC 来了
- The current situation of the industry is disappointing. After working, I returned to UC Berkeley to study for a doctoral degree
猜你喜欢
随机推荐
RoleApp聚焦Web3,用“数据可持有”的钥匙开启Web3的大门
同源策略,面经
史上最全的 IDEA Debug 调试技巧(超详细案例)
TS learning notes
Mysql死锁、锁超时、慢sql总结
(JS)不使用輔助空間找出數組中唯一成對的數
服务器状态码
永久解决 matplotlib 中文乱码
如何用Redis实现消息的发布和订阅?实现原理又是什么?
DNS域名解析
详细解读:反射的用法
EMQ映云科技成功入选《中国企业家》2022年度「新锐100」榜单
k线图经典图解的学习有利于正确
Debezium系列之:show slave status查看主从延迟可能存在的不同情形
7-15作业
分糖果 华为od js
uniapp使用Hbuilder X方式, 安装uivew插件步骤
Es6-11 learning notes
基于STM32HAL库ADS1256调试笔记
从数学推导开始,7万字一定带你学会支持向量机(免费PDF下载)