Redis的Publish/Subscribe

摘要:
Publish/Subscribe从字面上理解就是发布与订阅,在Redis中,你可以设定对某一个key值进行消息发布及消息订阅,当一个key值上进行了消息发布后,所有订阅它的客户端都会收到相应的消息。#对没有订阅者的频道发送信息redis˃publishbad_channel"cananybodyhearme?127.0.0.1:6379˃subscribechanneloneReadingmessages...1)"subscribe"2)"channelone"3)11)"message"2)"channelone"3)"hello"1)"message"2)"channelone"3)"world"按照一定模式批量订阅用下面命令可以订阅所有channel开头的通道127.0.0.1:6379˃psubscribechan*Readingmessages...1)"psubscribe"2)"chan*"3)1在另外一个客户端对两个通道发送推送消息;127.0.0.1:6379˃127.0.0.1:6379˃publishchanneloneheloo1127.0.0.1:6379˃publishchanneltwoworld1在第一个客户端就能收到推送的消息:127.0.0.1:6379˃psubscribechan*Readingmessages...1)"psubscribe"2)"chan*"3)11)"pmessage"2)"chan*"3)"channelone"4)"heloo"1)"pmessage"2)"chan*"3)"channeltwo"4)"world"命令说明:PSUBSCRIBEpattern[pattern...]订阅一个或多个符合给定模式的频道。

Publish/Subscribe 从字面上理解就是发布(Publish)与订阅(Subscribe),在Redis中,你可以设定对某一个key值进行消息发布及消息订阅,当一个key值上进行了消息发布后,所有订阅它的客户端都会收到相应的消息。这一功能最明显的用法就是用作实时消息系统,比如普通的即时聊天,群聊等功能。

相关命令参考:http://www.redisdoc.com/en/latest/pub_sub/index.html

订阅消息管道

用一个客户端订阅管道

127.0.0.1:6379> subscribe channelone
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "channelone"
3) (integer) 1

说明:

SUBSCRIBE channel [channel ...]

订阅给定的一个或多个频道的信息。

可用版本:
>= 2.0.0
时间复杂度:
O(N),其中 N 是订阅的频道的数量。
返回值:
接收到的信息

另外一个客户端往这个管道中推送消息。

127.0.0.1:6379>
127.0.0.1:6379> publish channelone hello
(integer) 1
127.0.0.1:6379> publish channelone world
(integer) 1
127.0.0.1:6379>

命令说明:

PUBLISH channel message

将信息 message 发送到指定的频道 channel 。

可用版本:
>= 2.0.0
时间复杂度:
O(N+M),其中 N 是频道 channel 的订阅者数量,而 M 则是使用模式订阅(subscribed patterns)的客户端的数量。
返回值:
接收到信息 message 的订阅者数量。
# 对没有订阅者的频道发送信息

redis> publish bad_channel "can any body hear me?"
(integer) 0

# 向有一个订阅者的频道发送信息

redis> publish msg "good morning"
(integer) 1

# 向有多个订阅者的频道发送信息

redis> publish chat_room "hello~ everyone"
(integer) 3

然后第一个客户端就能获取到推送的消息。

127.0.0.1:6379> subscribe channelone
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "channelone"
3) (integer) 1
1) "message"
2) "channelone"
3) "hello"
1) "message"
2) "channelone"
3) "world"

按照一定模式批量订阅

用下面命令可以订阅所有 channel 开头的通道

127.0.0.1:6379> psubscribe chan*
Reading messages... (press Ctrl-C to quit)
1) "psubscribe"
2) "chan*"
3) (integer) 1

在另外一个客户端对两个通道发送推送消息;

127.0.0.1:6379>
127.0.0.1:6379> publish channelone heloo
(integer) 1
127.0.0.1:6379> publish channeltwo world
(integer) 1

在第一个客户端就能收到推送的消息:

127.0.0.1:6379> psubscribe chan*
Reading messages... (press Ctrl-C to quit)
1) "psubscribe"
2) "chan*"
3) (integer) 1

1) "pmessage"
2) "chan*"
3) "channelone"
4) "heloo"
1) "pmessage"
2) "chan*"
3) "channeltwo"
4) "world"

命令说明:

PSUBSCRIBE pattern [pattern ...]

订阅一个或多个符合给定模式的频道。

每个模式以 * 作为匹配符,比如 it* 匹配所有以 it 开头的频道( it.news 、 it.blog 、 it.tweets 等等), news.* 匹配所有以 news. 开头的频道( news.it 、 news.global.today 等等),诸如此类。

可用版本:
>= 2.0.0
时间复杂度:
O(N), N 是订阅的模式的数量。
返回值:
接收到的信息(请参见下面的代码说明)。
# 订阅 news.* 和 tweet.* 两个模式

# 第 1 - 6 行是执行 psubscribe 之后的反馈信息
# 第 7 - 10 才是接收到的第一条信息
# 第 11 - 14 是第二条
# 以此类推。。。

redis> psubscribe news.* tweet.*
Reading messages... (press Ctrl-C to quit)
1) "psubscribe" # 返回值的类型:显示订阅成功
2) "news.*" # 订阅的模式
3) (integer) 1 # 目前已订阅的模式的数量

1) "psubscribe"
2) "tweet.*"
3) (integer) 2

1) "pmessage" # 返回值的类型:信息
2) "news.*" # 信息匹配的模式
3) "news.it" # 信息本身的目标频道
4) "Google buy Motorola" # 信息的内容

1) "pmessage"
2) "tweet.*"
3) "tweet.huangz"
4) "hello"

1) "pmessage"
2) "tweet.*"
3) "tweet.joe"
4) "@huangz morning"

1) "pmessage"
2) "news.*"
3) "news.life"
4) "An apple a day, keep doctors away"

参考资料:

Redis 命令参考
http://www.redisdoc.com/en/latest/index.html

十五分钟介绍 Redis数据结构
http://blog.nosqlfan.com/html/3202.html

Redis系统性介绍
http://blog.nosqlfan.com/html/3139.html

Redis之七种武器
http://blog.nosqlfan.com/html/2942.html

试用redis
http://try.redis.io/

Redis 设计与实现
http://www.redisbook.com/en/latest/

免责声明:文章转载自《Redis的Publish/Subscribe》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇JedisConnectionException: Failed connecting to host localhost:6379Keil新建工程步骤下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

nonebot 源码阅读笔记

前言 nonebot 是一个 QQ 消息机器人框架,它的一些实现机制,值得参考。 nonebot NoneBot 初始化(配置加载) 阅读 nonebot 文档,第一个示例如下: import nonebot if __name__ == '__main__': nonebot.init() nonebot.load_builtin_pl...

Linux安装redis,启动配置不生效(指定启动加载配置文件)

一、今天有个同学问我,为什么明明安装了redis,修改了配置,启动的时候,配置还是不生效。如下图是安装后的redis文件图。 二、想加载上图中的redis.conf,进入到src中寻找到启动文件redis-server 三、启动时指定加载的配置文件 四、如下图所示,加载的配置文件,你修改的信息均会生效。 个人公众号谢谢各位老铁支持...

ZAB协议简介

Zookeeper 使用 Zookeeper Atomic Broadcast (ZAB) 协议来保障分布式数据一致性。 ZAB是一种支持崩溃恢复的消息广播协议,采用类似2PC的广播模式保证正常运行时性能,并使用基于 Paxos 的策略保证崩溃恢复时的一致性。 在阅读本文前建议先了解2PC和Paxos ZAB协议中节点存在四种状态: Leading: 当...

Mozilla公布火狐4详情:更快 更支持开放标准

Mozilla公布火狐4详情:更快 更支持开放标准 火狐公布火狐4.0版的更多技术详情 北京时间5月11日消息,据国外媒体报道,火狐(Firefox)浏览器开发商Mozilla技术开发副总裁迈克·贝尔泽纳(Mike Beltzner)今天公布了火狐4.0版的更多技术详情,称火狐4.0版整体运行速度将更快,将更多支持开放互联网浏览标准,同时把隐私保护...

使用可视化工具redisclient连接redis

可视化工具推荐:http://database.51cto.com/art/201505/477692.htm 1.连接redis服务端   1.1 设置连接密码:在redis根目录下,双击redis-cli.exe, 输入命令:redis-cli.exe -h 127.0.0.1 -p 6379 -n 1    1就是密码     1.2  使用red...

java操作Redis

Java访问redis Java操作redis string(字符串) hash(哈希) list(列表) set(集合)  zset(sorted set:有序集合) package com.cjh; import redis.clients.jedis.Jedis; /** * @author * @site * @company * @...