当前位置:网站首页>17_Redis_Redis发布订阅
17_Redis_Redis发布订阅
2022-07-02 12:00:00 【听*雨声】
Redis 发布订阅(pub/sub)是一种消息通信模式∶发送者(pub)发送消息,订阅者(sub)接收消息。
Redis客户端可以订阅任意数量的频道。例如:一个用户可以关注任意多的up主
订阅/发布消息图∶
第一个:消息发送者
第二个:频道
第三个:消息订阅者
下图展示了频道channel1,以及订阅这个频道的三个客户端―—client2、 client5和client1之间的关系∶
当有新消息通过PUBLISH 命令发送给频道channel1时,这个消息就会被发送给订阅它的三个客户端:
命令:这些命令被广泛用于构建即时通信应用,比如网络聊天室(chatroom)和实时广播、实时提醒等。
测试
订阅端:
127.0.0.1:6379> SUBSCRIBE xsy666 // 订阅一个频道:xsy666
Reading messages… (press Ctrl-C to quit)
1 ) “subscribe”
2 ) “xsy666”
3 ) (integer) 1
// 等待读取推送的信息
1 ) “message” // 消息
2 ) “xsy666” // 哪个频道的消息
3 ) “hello,xsy” // 消息的具体内容
1 ) “message”
2 ) “xsy666”
3 ) “hello,zhangsan”
1 ) “message”
2 ) “xsy666”
3 ) “byebye”
发送端:
127.0.0.1:6379> PUBLISH xsy666 hello,xsy // 发布者发布消息到频道!
(integer) 1
127.0.0.1:6379> PUBLISH xsy666 hello,zhangsan
(integer) 1
127.0.0.1:6379> PUBLISH xsy666 byebye
(integer) 1
原理
Redis是使用C实现的,通过分析Redis源码里的pubsub.c文件,了解发布和订阅机制的底层实现,籍此加深对Redis的理解。
Redis通过 PUBLISH、SUBSCRIBE 和PSUBSCRIBE等命令实现发布和订阅功能。
通过SUBSCRIBE命令订阅某频道后,redis-server里维护了一个字典,字典的键就是一个个channel,而字典的值则是一个链表,链表中保存了所有订阅这个channel的客户端。SUBSCRIBE命令的关键,就是将客户端添加到给定channel的订阅链表中。
通过PUBLISH命令向订阅者发送消息,redis-server会使用给定的频道作为键,在它所维护的channel字典中查找记录了订阅这个频道的所有客户端的链表,遍历这个链表,将消息发布给所有订阅者。
Pub/Sub从字面上理解就是发布( Publish )与订阅( Subscribe ),在Redis中,你可以设定对某一个key值进行消息发布及消息订阅,当一个key值上进行了消息发布后,所有订阅它的客户端都会收到相应的消息。这一功能最明显的用法就是用作实时消息系统,比如普通的即时聊天,群聊等功能。
使用场景:
1.实时消息系统
2.实时聊天
3.订阅,关注
边栏推荐
- MFC timer usage
- 【无标题】LeetCode 2321. 拼接数组的最大分数
- MFC CString to char*
- 學習使用php實現公曆農曆轉換的方法代碼
- 【C语言】详解指针的初阶和进阶以及注意点(1)
- Topology architecture of the minimum deployment of tidb cluster
- Some Chinese character codes in the user privacy agreement are not standardized, which leads to the display of garbled codes on the web page. It needs to be found and handled uniformly
- 蜻蜓低代码安全工具平台开发之路
- TiDB跨数据中心部署拓扑
- C language exercises - (array)
猜你喜欢
【无标题】LeetCode 2321. 拼接数组的最大分数
buuctf-pwn write-ups (7)
Solve the problem that El radio group cannot be edited after echo
Jenkins Pipeline 应用与实践
[C language] explain the initial and advanced levels of the pointer and points for attention (1)
LeetCode - 搜索二维矩阵
Introduction to C language -- array
[noi simulation] Elis (greedy, simulation)
[untitled] leetcode 2321 Maximum score of concatenated array
关于网页中的文本选择以及统计选中文本长度
随机推荐
JMeter script parameterization
Topology architecture of the minimum deployment of tidb cluster
关于网页中的文本选择以及统计选中文本长度
华为面试题: 没有回文串
TiDB 环境与系统配置检查
Socket and socket address
c语言入门--数组
Apprendre le Code de la méthode de conversion du calendrier lunaire grégorien en utilisant PHP
C语言习题---(数组)
C # delay, start the timer in the thread, and obtain the system time
Application of CDN in game field
C thread transfer parameters
Learn the method code example of converting timestamp to uppercase date using PHP
Implementation of n queen in C language
二叉树的遍历方式主要有:先序遍历、中序遍历、后序遍历、层次遍历。先序、中序、后序其实指的是父节点被访问的次序。若在遍历过程中,父节点先于它的子节点被访问,就是先序遍历;
如何对 TiDB 进行 TPC-C 测试
. Net core logging system
编译原理课程实践——实现一个初等函数运算语言的解释器或编译器
Table responsive layout tips
CTO如何帮助业务?