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"
  • 相关阅读:
    移动端获取屏幕的宽度,根据屏幕大小动态设置html的rem字体大小
    解析CSS3伪类选择器nth-of-type和nth-child的用法,以及两者的区别
    移动端的1px的解决方案
    Vue中import from的来源:省略后缀与加载文件夹
    flex布局
    前端开发人员快速创建本地服务器
    centos6.5Xen4.2安装
    centos6.5kvm虚拟化安装部署
    CentOS搭建svn服务器支持https访问
    CentOS6.5搭建LNMP
  • 原文地址:https://www.cnblogs.com/zzliu/p/10671915.html
Copyright © 2011-2022 走看看