当前位置:网站首页>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.订阅,关注
边栏推荐
- kibana 基础操作
- LeetCode 209. Minimum length subarray
- CodeCraft-22 and Codeforces Round #795 (Div. 2)D,E
- How does CTO help the business?
- php获取数组中键值最大数组项的索引值的方法
- 【NOI模拟赛】伊莉斯elis(贪心,模拟)
- 871. 最低加油次数 : 简单优先队列(堆)贪心题
- 牛客练习赛101
- MFC CString 转 char*
- 二叉树的遍历方式主要有:先序遍历、中序遍历、后序遍历、层次遍历。先序、中序、后序其实指的是父节点被访问的次序。若在遍历过程中,父节点先于它的子节点被访问,就是先序遍历;
猜你喜欢

C code audit practice + pre knowledge

The past and present lives of visual page building tools

TiDB数据迁移工具概览

How does CTO help the business?

06_栈和队列转换

Dragonfly low code security tool platform development path

学习使用php将时间戳转换为大写日期的方法代码示例
![[C language] explain the initial and advanced levels of the pointer and points for attention (1)](/img/61/1619bd2e959bae1b769963f66bab4e.png)
[C language] explain the initial and advanced levels of the pointer and points for attention (1)

Jenkins Pipeline 应用与实践

LeetCode 2310. The number of digits is the sum of integers of K
随机推荐
C语言习题---(数组)
Recommended configuration of tidb software and hardware environment
Topology architecture of the minimum deployment of tidb cluster
Implementation of n queen in C language
Deploy tidb cluster with tiup
Application and practice of Jenkins pipeline
C语言中的printf函数和scanf函数
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 进行 TPC-C 测试
为什么只会编程的程序员无法成为优秀的开发者?
Huawei interview question: no palindrome string
Socket and socket address
Btrace- (bytecode) dynamic tracking tool
C#延时、在线程中开启定时器、获取系统时间
C# 线程传参
Implement a server with multi process concurrency
MFC console printing, pop-up dialog box
LeetCode - 搜索二维矩阵
Edit the formula with MathType, and set it to include only mathjax syntax when copying and pasting
LeetCode_滑动窗口_中等_395.至少有 K 个重复字符的最长子串