当前位置:网站首页>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.订阅,关注
边栏推荐
- 使用 TiUP 部署 TiDB 集群
- [development environment] install the visual studio community 2013 development environment (download the installation package of visual studio community 2013 with update 5 version)
- MFC console printing, pop-up dialog box
- 871. 最低加油次数 : 简单优先队列(堆)贪心题
- 06_栈和队列转换
- 数据分析思维分析方法和业务知识——业务指标
- About text selection in web pages and counting the length of selected text
- LeetCode_字符串_简单_412.Fizz Buzz
- Bit by bit of OpenCV calling USB camera
- TiDB跨数据中心部署拓扑
猜你喜欢
关于网页中的文本选择以及统计选中文本长度
解决el-radio-group 回显后不能编辑问题
Error: NPM warn config global ` --global`, `--local` are deprecated Use `--location=global` instead.
XML配置文件
CodeCraft-22 and Codeforces Round #795 (Div. 2)D,E
Table responsive layout tips
【NOI模拟赛】伊莉斯elis(贪心,模拟)
Obsidian installs third-party plug-ins - unable to load plug-ins
Map介绍
Large top heap, small top heap and heap sequencing
随机推荐
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
Topology architecture of the minimum deployment of tidb cluster
AtCoder Beginner Contest 254
蜻蜓低代码安全工具平台开发之路
Have you learned the wrong usage of foreach
Li Chuang EDA learning notes 15: draw border or import border (DXF file)
C#代码审计实战+前置知识
C# 线程传参
LeetCode 209. Minimum length subarray
How to conduct TPC-C test on tidb
解决el-radio-group 回显后不能编辑问题
Printf function and scanf function in C language
[Space & single cellomics] phase 1: single cell binding space transcriptome research PDAC tumor microenvironment
PHP method to get the index value of the array item with the largest key value in the array
IE 浏览器正式退休
It's no exaggeration to say that this is the most user-friendly basic tutorial of pytest I've ever seen
Niuke Practice 101
AtCoder Beginner Contest 254
C code audit practice + pre knowledge
TiDB混合部署拓扑