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

    发布订阅简介

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


    ## 准备工作 两台安装了redis的机器(虚拟机),一号机器上运行一个redis服务端和一个发布者客户端,二号机器上运行多个订阅者客户端

    配置文件

    在一号服务器的 /opt/redis_conf/ 目录下新建一个redis-6379.conf配置文件,写入如下内容:

    # 端口
    port 6379
    
    # 是否后台运行
    daemonize yes
    
    # pid文件的存放路径
    pidfile /data/6379/redis.pid
    
    # 日志级别
    loglevel notice
    
    # 日志文件路径
    logfile "/data/6379/redis.log"
    
    # 数据保存路径
    dir /data/6379
    
    # 是否开启保护模式,保护模式开启后远程客户端无法连接此服务端
    protected-mode no
    
    # 密码
    requirepass 123
    
    

    将二号服务器的redis配置文件的```bind 127.0.0.1```改为: ``` bind 192.168.1.17 # 一号服务器的ip地址
    
    <br>
    ## 在一号服务器上启动redis-server
    
    ```linux
    redis-server /opt/redis_conf/redis-6379.conf
    
    

    确认redis-server是否启动

    [root@localhost redis_conf]# ps -ef | grep redis
    root      31308      1  0 17:07 ?        00:00:00 redis-server *:6379   # 说明成功启动了
    root      31316  10855  0 17:07 pts/2    00:00:00 grep --color=auto redis
    
    

    一号服务器上登录,新建一个发布者 ``` [root@localhost redis_conf]# redis-cli 127.0.0.1:6379> PUBLISH music 'shilian' (error) NOAUTH Authentication required.
    
    出现上述提示说明需要输入密码
    
    

    [root@localhost redis_conf]# redis-cli
    127.0.0.1:6379> PUBLISH music 'shilian'
    (error) NOAUTH Authentication required.
    127.0.0.1:6379>
    127.0.0.1:6379>
    127.0.0.1:6379> AUTH 123
    OK
    127.0.0.1:6379> PUBLISH music 'shilian'
    (integer) 0

    
    发布成功
    
    <br>
    在二号机器上用redis客户端远程连接一号机器的redis服务端,订阅music频道
    远程连接的格式为
    
    

    redis-cli -h ip地址 -p 端口号

    端口号为6379时可以省略不写
    
    <br>
    
    

    [root@localhost ~]# redis-cli -h 192.168.1.17 -p 6379
    192.168.1.17:6379> auth 123
    OK
    192.168.1.17:6379> SUBSCRIBE music
    Reading messages... (press Ctrl-C to quit)

    1. "subscribe"
    2. "music"
    3. (integer) 1
    
    
    <br>
    发布一条消息
    

    127.0.0.1:6379> PUBLISH music 'yasugongshang'
    (integer) 2

    
    <br>
    查看订阅者接收情况
    
    第一个订阅者
    
    

    [root@localhost ~]# redis-cli -h 192.168.1.17 -p 6379
    192.168.1.17:6379> auth 123
    OK
    192.168.1.17:6379> SUBSCRIBE music
    Reading messages... (press Ctrl-C to quit)

    1. "subscribe"
    2. "music"
    3. (integer) 1
    4. "message"
    5. "music"
    6. "yasugongshang"
    
    
    第二个订阅者
    
    

    [root@localhost ~]# redis-cli -h 192.168.1.17 -p 6379
    192.168.1.17:6379> AUTH 123
    OK
    192.168.1.17:6379> SUBSCRIBE music
    Reading messages... (press Ctrl-C to quit)

    1. "subscribe"
    2. "music"
    3. (integer) 1
    4. "message"
    5. "music"
    6. "yasugongshang"
  • 相关阅读:
    centos8 将SSSD配置为使用LDAP并要求TLS身份验证
    Centos8 搭建 kafka2.8 .net5 简单使用kafka
    .net core 3.1 ActionFilter 拦截器 偶然 OnActionExecuting 中HttpContext.Session.Id 为空字符串 的问题
    Springboot根据不同环境加载对应的配置
    VMware Workstation12 安装 Centos8.3
    .net core json配置文件小结
    springboot mybatisplus createtime和updatetime自动填充
    .net core autofac依赖注入简洁版
    .Net Core 使用 redis 存储 session
    .Net Core 接入 RocketMQ
  • 原文地址:https://www.cnblogs.com/zzliu/p/10671915.html
Copyright © 2011-2022 走看看