一、发布/订阅模式

在软件工程里面,发布/订阅是一种消息模式,这种模式旨在将消息发送者和消息接收者解耦。发送者不需要关心将消息发送给谁,接收者也不需要知道消息的发送者是谁。发送者将消息发布以后就结束动作,接收者可以订阅自己感兴趣的消息。

除了发布/订阅模式还有一种和它很类似的,消息队列,是一种典型的面向消息中间件的系统。许多消息系统都会同时支持发布/订阅和消息队列模型,例如Java Message Service(JMS)

参见:维基百科

二、redis的发布/订阅

我们从一个简单的示例开始,首先我们启动redis-server服务端,然后打开两个redis-cli客户端,我们将两个客户端分别称作client1、client2

1)client1 使用 subscribe 命令订阅了频道 channel:chat

127.0.0.1:6379> subscribe channel:chat
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "channel:chat"
3) (integer) 1

2)client2 向频道channel:chat发布了一个消息

127.0.0.1:6379> publish channel:chat "hey client1"
(integer) 1

3)我们看到client1接收到的消息如下:

127.0.0.1:6379> subscribe channel:chat
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "channel:chat"
3) (integer) 1
1) "message"
2) "channel:chat"
3) "hey client1"

message 表示 消息类型是消息发布的接收,频道是channel:chat,消息主体是:hey client1

以上,我们通过redis简单实现了一个发布订阅操作。

三、消息的格式

一个发布/订阅消息是一个数组,包括三个元素。

第一个元素是消息的类型:

1)subscribe: 表示订阅成功类型

2) unsubscribe: 表示退订成功类型

3) message: 表示接收到发布的消息

第二个元素表示的是当前订阅或者退订的频道

第三个元素表示当前订阅的频道数量或者是消息体

四、订阅多个频道

redis的发布/订阅支持订阅多个频道,如:

subscribe channel:first channel:second

当接收到对应频道的消息时,按照消息格式接收消息

五、匹配订阅频道

redis中支持通过通配符来订阅频道,需要用到命令:psubscribe,如:

client1订阅了channel:*通配符表示的多个频道

127.0.0.1:6379> psubscribe channel:*
Reading messages... (press Ctrl-C to quit)
1) "psubscribe"
2) "channel:*"
3) (integer) 1

client2分别在channel:1和channel:2两个频道发布了消息

127.0.0.1:6379> publish channel:chat "hey client1"
(integer) 1
127.0.0.1:6379> publish channel:1 "hey this is channel1"
(integer) 1
127.0.0.1:6379> publish channel:2 "hey this is channel2"
(integer) 1

我们看到client1接收到了两个频道的消息

127.0.0.1:6379> psubscribe channel:*
Reading messages... (press Ctrl-C to quit)
1) "psubscribe"
2) "channel:*"
3) (integer) 1
1) "pmessage"
2) "channel:*"
3) "channel:1"
4) "hey this is channel1"
1) "pmessage"
2) "channel:*"
3) "channel:2"
4) "hey this is channel2"

六、发布订阅的命令

psubscribe: 订阅

publish: 发布

pubsub: 检查

punsubscribe: 取消订阅

subscribe: 订阅

unsubscribe: 取消订阅

具体使用参考:redis官网

redis(3)发布订阅的更多相关文章

  1. redis的发布订阅模式

    概要 redis的每个server实例都维护着一个保存服务器状态的redisServer结构 struct redisServer {     /* Pubsub */     // 字典,键为频道, ...

  2. StackExchange.Redis 使用-发布订阅 (二)

    使用Redis的发布订阅功能 redis另一个常见的用途是发布订阅功能 . 它非常的简单 ,当连接失败时 ConnectionMultiplexer 会自动重新进行订阅 . ISubscriber s ...

  3. .net core 使用Redis的发布订阅

    Redis是一个性能非常强劲的内存数据库,它一般是作为缓存来使用,但是他不仅仅可以用来作为缓存,比如著名的分布式框架dubbo就可以用Redis来做服务注册中心.接下来介绍一下.net core 使用 ...

  4. redis的发布订阅模式pubsub

    前言 redis支持发布订阅模式,在这个实现中,发送者(发送信息的客户端)不是将信息直接发送给特定的接收者(接收信息的客户端),而是将信息发送给频道(channel),然后由频道将信息转发给所有对这个 ...

  5. java实现 redis的发布订阅(简单易懂)

    redis的应用场景实在太多了,现在介绍一下它的几大特性之一   发布订阅(pub/sub). 特性介绍: 什么是redis的发布订阅(pub/sub)?   Pub/Sub功能(means Publ ...

  6. spring boot 使用redis进行发布订阅

    异步消息的方式有很多,这篇博客介绍如何使用redis进行发布订阅, 完成这个示例只需要三个文件 1.redis消息监听配置 @Configuration public class RedisListe ...

  7. 【springboot】【redis】springboot+redis实现发布订阅功能,实现redis的消息队列的功能

    springboot+redis实现发布订阅功能,实现redis的消息队列的功能 参考:https://www.cnblogs.com/cx987514451/p/9529611.html 思考一个问 ...

  8. redis的发布订阅、持久化存储、redis的主从复制

    redis的发布订阅 1. 创建redis配置文件 vim /opt/redis_conf/reids-6379.conf mkdir /data/6379 redis-server  redis-6 ...

  9. redis 实现发布订阅的功能

    redis 除了作为缓存的功能外还可以用作消息中间件的功能,这片博客主要是介绍一下 redis 整合spring 实现消息的发布和订阅功能: 1:redis依赖,依赖两个包,redis 包, spri ...

  10. Redis之发布订阅

    一 什么是发布订阅 发布订阅模式又叫观察者模式,它定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖它的对象都将得到通知 Redis 发布订阅(pub/sub)是一种消息通信模式: ...

随机推荐

  1. POJ - 1251A - Jungle Roads 利用最小生成树

    The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was s ...

  2. Beginning and Ending the Speech

    Beginning and Ending the Speech Just as musical plays need appropriate beginnings and endings, so do ...

  3. leetcode-682-Baseball Game

    题目描述: You're now a baseball game point recorder. Given a list of strings, each string can be one of ...

  4. springcloud(二)-最简单的实战

    技术储备 Spring cloud并不是面向零基础开发人员,它有一定的学习曲线. 语言基础:spring cloud是一个基于Java语言的工具套件,所以学习它需要一定的Java基础.当然,sprin ...

  5. 【中间件】Struts2系列漏洞POC小结

    #Struts2-045 ''' CVE-2017-5638 影响范围:Struts 2.3.5 – Struts 2.3.31,Struts 2.5 – Struts 2.5.10 触发条件:基于J ...

  6. AWS S3

    Amazon Simple Storage Service (Amazon S3) Amazon S3 提供了一个简单 Web 服务接口,可用于随时在 Web 上的任何位置存储和检索任何数量的数据.此 ...

  7. Restful API学习笔记

    之前关于这个概念在网上看了一些,看完似懂非懂,模模糊糊,发现专业术语或者说书面表达的形式对于理解这种十分抽象的概念还是低效了点. 书面文档方面看了以下几个: 理解本真的REST架构风格 1. 要深入理 ...

  8. Docker安装及常用操作

    Docker简介: Docker是一个轻量级容器技术,类似于虚拟机技术,但性能远远高于虚拟机,Docker支持将软件编译成一个镜像(image),在这个镜像中做好对软件的各种配置,然后可以运行这个镜像 ...

  9. CentOS 6.5中配置RabbitMQ

    先配置erlang依赖环境 yum -y install make gcc gcc-c++ kernel-devel m4 ncurses-devel openssl-devel 安装erlang 1 ...

  10. JDBC(2)-使用statment接口实现增删改操作

    1.Statement接口引入 作用:用于执行静态SQL语句并返回它所生成结果的对象. int executeUpdate(String sql) :执行给定SQL语句,该语句可能为insert.up ...