redis的订阅和发布简介

进程间的一种消息通信模式:发送者(pub)发送消息,订阅者(sub)接收消息。

原理图:
redis的订阅和发布

redis 消息订阅和发布命令

  1. psubscribe pattern [pattern...] :订阅一个或多个给定模式的 频道

  2. punsubscribe [pattern [pttern...]]:退订所有频道给定模式的频道

  3. pubsub subcommand [argument [argument...]]:查看订阅与发布系统状态

  4. publish channel message:将信息发送到指定的频道

  5. subscribe channel [channel...]:订阅给定的一个或多个频道信息。

  6. unsubscribe [channel [channel...]]:指退订给定的频道。

redis的订阅和发布

案例

先订阅后发布才能收到消息

  1. 一次性订阅多个频道从c1,c2,c3,subscribe c1 c2 c3
    redis的订阅和发布

  2. 消息发布,向c2频道发布消息:publish c2 hello-redis
    redis的订阅和发布
    查看订阅者进程输出:
    redis的订阅和发布

  3. 订阅给定模式的频道,通配符* :psubscribe new*
    redis的订阅和发布

  4. 发送消息:publish new1 redis-hello
    redis的订阅和发布
    收到消息截图:
    redis的订阅和发布

相关文章:

  • 2021-12-24
  • 2022-12-23
  • 2022-03-06
  • 2021-07-14
  • 2021-07-04
  • 2022-12-23
猜你喜欢
  • 2021-11-06
  • 2021-10-14
  • 2021-11-23
  • 2021-10-15
  • 2021-06-08
相关资源
相似解决方案