redis的发布订阅模式pubsub
前言
redis支持发布订阅模式,在这个实现中,发送者(发送信息的客户端)不是将信息直接发送给特定的接收者(接收信息的客户端),而是将信息发送给频道(channel),然后由频道将信息转发给所有对这个频道感兴趣的订阅者。
发送者无须知道任何关于订阅者的信息,而订阅者也无须知道是那个客户端给它发送信息,它只要关注自己感兴趣的频道即可。
对发布者和订阅者进行解构(decoupling),可以极大地提高系统的扩展性(scalability),并得到一个更动态的网络拓扑(network topology)。
redis 发布订阅主要由三个entity组成:channel/subscriber/publisher。
channel:
频道有两种类型
明确型,news.sport,体育类新闻
模糊型,news.*,各种新闻
下面实现对于这两种是透明的。
# -*- coding:utf-8 -*-
class Channel(object):
def __init__(self, channel=''):
self.channel = channel
def __str__(self):
return self.channel
class ChannelFactory(object):
def __init__(self, *channels):
if isinstance(channels[0], (tuple, list)):
self.channel_list = [Channel(channel) for channel in channels[0]]
self.channel_list = [Channel(channel) for channel in channels]
def get_channels(self):
return self.channel_list
user:
主要有两类,订阅者subscriber和发布者publisher,他们都继承自Pubsub,由继承关系实现:
# -*- coding:utf-8 -*-
import redis class Pubsub(object): def __init__(self, redis_config):
pool = redis.ConnectionPool(
host=redis_config.get('host'),
port=redis_config.get('port'),
db=redis_config.get('db')
)
self.redis = redis.StrictRedis(connection_pool=pool)
class Subscriber(Pubsub): def __init__(self, redis_config):
Pubsub.__init__(self, redis_config=redis_config)
self.pubsub = self.redis.pubsub() def subscribe(self, *channel):
self.pubsub.subscribe(*channel) def psubscribe(self, *channel_pattern):
self.pubsub.psubscribe(*channel_pattern) def listen(self):
for item in self.pubsub.listen():
yield item def unsubscribe(self, *channel):
self.pubsub.unsubscribe(*channel) def punsubscribe(self, *channel_pattern):
self.pubsub.unsubscribe(*channel_pattern) class Publisher(Pubsub):
def __init__(self, redis_config):
Pubsub.__init__(self, redis_config=redis_config) def publish(self, channel, message):
self.redis.publish(channel, message)
测试
分两部分,订阅进程和发布进程
订阅进程:
from config import redis as redis_config
from subscriber import Subscriber
from channel import ChannelFactory if __name__ == '__main__':
channel = ChannelFactory('news.*', 'movie.*').get_channels()
sub = Subscriber(redis_config)
sub.psubscribe(channel)
for item in sub.listen():
print item
发布进程:
from config import redis as redis_config
from publisher import Publisher
from channel import Channel if __name__ == '__main__':
channel = Channel('news.abc')
pub = Publisher(redis_config)
pub.publish(channel, 'aaaaaaaa')
redis的发布订阅模式pubsub的更多相关文章
- redis的发布订阅模式
概要 redis的每个server实例都维护着一个保存服务器状态的redisServer结构 struct redisServer { /* Pubsub */ // 字典,键为频道, ...
- 13、Redis的发布订阅模式
写在前面的话:读书破万卷,编码如有神 -------------------------------------------------------------------------------- ...
- Springboot+Redis(发布订阅模式)跨多服务器实战
一:redis中发布订阅功能(http://www.redis.cn/commands.html#pubsub) PSUBSCRIBE pattern [pattern -]:订阅一个或者多个符合pa ...
- 【转】Redis之发布 订阅模式
本例包括 jedis_demo:入口类 jedis_control:jedis控制器(jedis的连接池) jedis_pub_sub_listener:订阅的监听器 singleton_agent: ...
- 使用redis的发布订阅模式实现消息队列
配置文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://w ...
- Spring Boot中使用redis的发布/订阅模式
原文:https://www.cnblogs.com/meetzy/p/7986956.html redis不仅是一个非常强大的非关系型数据库,它同时还拥有消息中间件的pub/sub功能,在sprin ...
- 15天玩转redis —— 第九篇 发布/订阅模式
本系列已经过半了,这一篇我们来看看redis好玩的发布订阅模式,其实在很多的MQ产品中都存在这样的一个模式,我们常听到的一个例子 就是邮件订阅的场景,什么意思呢,也就是说100个人订阅了你的博客,如果 ...
- redis发布/订阅模式
其实在很多的MQ产品中都存在这样的一个模式,我们常听到的一个例子 就是邮件订阅的场景,什么意思呢,也就是说100个人订阅了你的博客,如果博主发表了文章,那么100个人就会同时收到通知邮件,除了这个 场 ...
- 使用EventBus + Redis发布订阅模式提升业务执行性能
前言 最近一直奔波于面试,面了几家公司的研发.有让我受益颇多的面试经验,也有让我感觉浪费时间的面试经历~因为疫情原因,最近宅在家里也没事,就想着使用Redis配合事件总线去实现下具体的业务. 需求 一 ...
随机推荐
- 使用JavaScript循环嵌套解决各种图形
[循环嵌套的规律] 1.外层循环控制行数,内层循环控制每行中元素的个数. [图形题思路] 1.确定图形有几行,行数即为外层循环次数: 2.确定每行中有几种元素组成,有几种元素表示有几 ...
- 浏览器console的用法
Leo_wlCnBlogs 自由.创新.研究.探索 Linux/Windows Mono/DotNet [ Open Source .NET Development/ 使用开源工具进行DotNet软件 ...
- Web in Linux小笔记001
Linux灾难恢复: Root密码修复 Centos single Filesystem是硬盘文件根目录,无法再cd ..就像macitosh 硬盘图标 Pwd:显示绝对路径 MBR修复 模拟MBR被 ...
- [转载]python 详解re模块
原文地址:python 详解re模块作者:Rocky 正则表达式的元字符有. ^ $ * ? { [ ] | ( ) .表示任意字符 []用来匹配一个指定的字符类别,所谓的字符类别就是你想匹配的一个字 ...
- 转:【Java并发编程】之二十二:并发新特性—障碍器CyclicBarrier(含代码)
转载请注明出处:http://blog.csdn.net/ns_code/article/details/17512983 CyclicBarrier(又叫障碍器)同样是Java5中加入的新特性,使用 ...
- 九度OJ 1011 最长子串
#include <iostream> #include <string> #include <sstream> #include <math.h> u ...
- 团队作业4——第一次项目冲刺(Alpha版本)2017.4.22
昨天来不及编写,这是4月22日的日志,现在补上. 1.开完站立式会议后的合照 2.任务分解图 3.开会讨论的结果,任务分派 队员 今日进展 明日安排 陈鑫龙 原型设计图分析,设计登陆界面原稿 实现登陆 ...
- 201521123121 《Java程序设计》第4周学习总结
1. 本周学习总结 1.1 尝试使用思维导图总结有关继承的知识点. 1.2 使用常规方法总结其他上课内容. 对象的封装:将类的某些信息隐藏在类内部,不允许外部程序直接访问,而是通过该类提供的方法来实现 ...
- 201521123037 《Java程序设计》第3周学习总结
1. 本周学习总结 链接:http://naotu.baidu.com/file/026a646bb4031d4238accc69cdf53272 2. 书面作业 1. 代码阅读 public cla ...
- 201521123054《Java程序设计》第1周学习总结
#1. 本章学习总结 你对于本章知识的学习总结 本章我们学习了各种java相关文件的使用,能够进行基本的程序操作: 学会使用博客.码云与PTA管理java: #2. 书面作业 1.为什么java程序可 ...