7

Redis--发布和订阅

 2 years ago
source link: http://blog.linrty.com/2021/07/20/Redis--%E5%8F%91%E5%B8%83%E5%92%8C%E8%AE%A2%E9%98%85/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

什么是发布和订阅

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

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

Redis的发布和订阅

  1. 客户端可以订阅频道如下图

image-20210720162532191

  1. 当给这个频道发布消息后,消息就会发送给订阅的客户端

    image-20210720162636940

发布订阅命令行实现

  1. 打开一个客户端订阅channel1

    SUBSCRIBE channel1

    image-20210720163201695

  2. 打开另一个客户端,给channel1发布消息hello

    publish channel1 hello

    image-20210720163515438

  3. 打开第一个客户端可以看到发送的消息

    image-20210720163528008

注:发布的消息没有持久化,如果在订阅的客户端收不到hello,只能收到订阅后发布的消息

本文作者: linrty
本文链接: http://blog.linrty.com/2021/07/20/Redis--%E5%8F%91%E5%B8%83%E5%92%8C%E8%AE%A2%E9%98%85/
版权声明: 本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。转载请注明出处!

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK