Redis入门(七)——Redis发布订阅

Redis入门(七)——Redis发布订阅


目录:

  • Redis发布订阅简介
  • Redis发布订阅命令

1.Redis发布订阅简介

Redis 发布订阅(pub/sub)是一种消息通信模式:发送者(pub)发送消息,订阅者(sub)接收消息。

Redis 客户端可以订阅任意数量的频道。

下图展示了频道 channel1 , 以及订阅这个频道的三个客户端 —— client2 、 client5 和 client1 之间的关系:

当有新消息通过 PUBLISH 命令发送给频道 channel1 时, 这个消息就会被发送给订阅它的三个客户端:

2.Redis发布订阅命令

命令 描述 使用
PSUBSCRIBE 订阅一个或多个符合给定模式的频道。 PSUBSCRIBE pattern [pattern ...]
PUBSUB 查看订阅与发布系统状态。 PUBSUB subcommand [argument [argument ...]]
PUBLISH 将信息发送到指定的频道。 PUBLISH channel message
PUNSUBSCRIBE 退订所有给定模式的频道。 PUNSUBSCRIBE [pattern [pattern ...]]
SUBSCRIBE 订阅给定的一个或多个频道的信息。 SUBSCRIBE channel [channel ...]
UNSUBSCRIBE 指退订给定的频道。 UNSUBSCRIBE [channel [channel ...]]

实例操作案例:

更多命令参考:https://www.runoob.com/redis/redis-pub-sub.html

不过一般在实际应用中不会拿redis作为消息中间件来使用。

猜你喜欢

转载自www.cnblogs.com/zylhxd/p/11439875.html