Redis的Publish/Subscribe

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/

原文地址:https://www.cnblogs.com/ghj1976/p/3559547.html