卡飞资源网

专业编程技术资源共享平台

Redis发布/订阅(Pub/Sub)

1.linux下连接redis后命令

192.168.217.128:6379> help @pubsub

PSUBSCRIBE pattern [pattern ...]
summary: Listen for messages published to channels matching the given patterns

PUBLISH channel message
summary: Post a message to a channel

PUBSUB subcommand [argument [argument ...]]
summary: Inspect the state of the Pub/Sub subsystem

PUNSUBSCRIBE [pattern [pattern ...]]
summary: Stop listening for messages posted to channels matching the given patterns

SUBSCRIBE channel [channel ...]
summary: Listen for messages published to the given channels

UNSUBSCRIBE [channel [channel ...]]
summary: Stop listening for messages posted to the given channels

2.发布订阅示例

先开启订阅channel消息,待向channel发布消息时,订阅者(支持多个)才能正确接收消息。

3.实时和历史消息怎么查看?

上图存在弊端,当发布消息客户端发布消息后,还没有来得及放到sorted set中保存起来,就宕机了。这时候,查看3天之内消息的时候,就发现消息丢失了。怎么解决呢?见下图:

建了两个redis,一个负责发布消息,一个负责订阅消息。实时发送的消息客户端直接接收。3天之内的消息,负责订阅消息的redis接收并保存到sorted set中。更久的消息,通过kafka等消息中间件,最终保存到数据库中。

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言