zoukankan      html  css  js  c++  java
  • redis发布订阅

     

    发布publish

    订阅subscribe

    Redis 通过 PUBLISH 、 SUBSCRIBE 等命令实现了订阅与发布模式。

     

    举例1:

    qq群的公告,单个发布者,多个收听者

    发布/订阅 实验

    发布订阅的命令

    复制代码
    PUBLISH channel msg
        将信息 message 发送到指定的频道 channel
    SUBSCRIBE channel [channel ...] 订阅频道,可以同时订阅多个频道
    UNSUBSCRIBE [channel ...] 取消订阅指定的频道, 如果不指定频道,则会取消订阅所有频道 PSUBSCRIBE pattern [pattern ...] 订阅一个或多个符合给定模式的频道,每个模式以
    * 作为匹配符,比如 it* 匹配所 有以 it 开头的频道( it.news 、 it.blog 、 it.tweets 等等), news.* 匹配所有 以 news. 开头的频道( news.it 、 news.global.today 等等),诸如此类 PUNSUBSCRIBE [pattern [pattern ...]] 退订指定的规则, 如果没有参数则会退订所有规则 PUBSUB subcommand [argument [argument ...]] 查看订阅与发布系统状态 注意:使用发布订阅模式实现的消息队列,当有客户端订阅channel后只能收到后续发布到该频道的消息,之前发送的不会缓存,必须Provider和Consumer同时在线。
    复制代码

    发布订阅:

    窗口1,启动两个redis-cli窗口,均订阅diantai 频道(channel)

    窗口2,启动发布者向频道 diantai发送消息

    [root@web02 ~]# redis-cli
    127.0.0.1:6379> PUBLISH diantai 'jinyewugenglaiwojia'
    (integer) 2

    窗口3,查看订阅者的消息状态

    订阅一个或者多个符合模式的频道

    窗口1,启动两个redis-cli窗口,均订阅 wang*频道(channel)

    复制代码
    127.0.0.1:6379> PSUBSCRIBE wang*
    Reading messages... (press Ctrl-C to quit)
    1) "psubscribe"
    2) "wang*"
    3) (integer) 1

    1) "pmessage" 2) "wang*" 3) "wangbaoqiang" 4) "jintian zhennanshou "
    复制代码

    窗口2,启动redis-cli窗口,均订阅wang*频道

    复制代码
    127.0.0.1:6379> PSUBSCRIBE wang*
    Reading messages... (press Ctrl-C to quit)
    1) "psubscribe"
    2) "wang*"
    3) (integer) 1
    
    
    
    1) "pmessage"
    2) "wang*"
    3) "wangbaoqiang"
    4) "jintian zhennanshou "
    复制代码

    窗口3,发布者消息

    [root@web02 ~]# redis-cli
    127.0.0.1:6379> PUBLISH wangbaoqiang "jintian zhennanshou "
    (integer) 2
  • 相关阅读:
    【leetcode】1403. Minimum Subsequence in Non-Increasing Order
    【leetcode】1399. Count Largest Group
    【leetcode】1396. Design Underground System
    【leetcode】1395. Count Number of Teams
    【leetcode】1394. Find Lucky Integer in an Array
    【leetcode】1391. Check if There is a Valid Path in a Grid
    【leetcode】1390. Four Divisors
    【leetcode】1389. Create Target Array in the Given Order
    modelsim仿真基本流程
    Quartus调用MOdelsim仿真过程
  • 原文地址:https://www.cnblogs.com/chenyibai/p/10107824.html
Copyright © 2011-2022 走看看