前言

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的更多相关文章

  1. redis的发布订阅模式

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

  2. 13、Redis的发布订阅模式

     写在前面的话:读书破万卷,编码如有神 -------------------------------------------------------------------------------- ...

  3. Springboot+Redis(发布订阅模式)跨多服务器实战

    一:redis中发布订阅功能(http://www.redis.cn/commands.html#pubsub) PSUBSCRIBE pattern [pattern -]:订阅一个或者多个符合pa ...

  4. 【转】Redis之发布 订阅模式

    本例包括 jedis_demo:入口类 jedis_control:jedis控制器(jedis的连接池) jedis_pub_sub_listener:订阅的监听器 singleton_agent: ...

  5. 使用redis的发布订阅模式实现消息队列

    配置文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://w ...

  6. Spring Boot中使用redis的发布/订阅模式

    原文:https://www.cnblogs.com/meetzy/p/7986956.html redis不仅是一个非常强大的非关系型数据库,它同时还拥有消息中间件的pub/sub功能,在sprin ...

  7. 15天玩转redis —— 第九篇 发布/订阅模式

    本系列已经过半了,这一篇我们来看看redis好玩的发布订阅模式,其实在很多的MQ产品中都存在这样的一个模式,我们常听到的一个例子 就是邮件订阅的场景,什么意思呢,也就是说100个人订阅了你的博客,如果 ...

  8. redis发布/订阅模式

    其实在很多的MQ产品中都存在这样的一个模式,我们常听到的一个例子 就是邮件订阅的场景,什么意思呢,也就是说100个人订阅了你的博客,如果博主发表了文章,那么100个人就会同时收到通知邮件,除了这个 场 ...

  9. 使用EventBus + Redis发布订阅模式提升业务执行性能

    前言 最近一直奔波于面试,面了几家公司的研发.有让我受益颇多的面试经验,也有让我感觉浪费时间的面试经历~因为疫情原因,最近宅在家里也没事,就想着使用Redis配合事件总线去实现下具体的业务. 需求 一 ...

随机推荐

  1. 数据库学习任务一:使用vs2010建立数据库

    数据库应用程序的开发流程一般主要分为以下几个步骤: 创建数据库 使用Connection对象连接数据库 使用Command对象对数据源执行SQL命令并返回数据 使用DataReader和DataSet ...

  2. 团队作业1 团队展示&选题

    团队展示&选题 Coding项目地址:https://git.coding.net/wjunren/running.git 一.团队展示 1.队名:Runing Guys 2.队员: 组长:骆 ...

  3. 如何设置Cookie 的值为中文的内容

    默认情况下,cookie的值是不允许中文内容的.可以借助于java.net.URLEncoder先对中文字符串进行编码,将编码后的结果设为cookie值.当程序要读取cookie值时,先读取,然后使用 ...

  4. 201521123016 《Java程序设计》第8周学习总结

    1. 本周学习总结 2. 书面作业 本次作业题集集合 1.List中指定元素的删除(题目4-1) 1.1 实验总结 1.删除元素的时候从最后一个元素开始,避免删除元素后位置发生变化而导致有些元素没有删 ...

  5. 201521123064 《Java程序设计》第5周学习总结

    1. 本章学习总结 1.1 尝试使用思维导图总结有关多态与接口的知识点. 1.2 可选:使用常规方法总结其他上课内容. 对匿名内部类的印象很深(内部类类部类内部内--).总结一下,匿名内部类也就是没有 ...

  6. 201521123122 《java程序设计》第十三周学习总结

    ## 201521123122 <java程序设计>第十三周实验总结 ## 1. 本周学习总结 以你喜欢的方式(思维导图.OneNote或其他)归纳总结多网络相关内容. 2. 书面作业 1 ...

  7. Flex布局介绍

    Flex 是 Flexible Box 的缩写,意为"弹性布局",用来为盒状模型提供最大的灵活性 任何一个容器都可以指定为 Flex 布局. .box{ display: -web ...

  8. 新旧apache HttpClient 获取httpClient方法

    在apache httpclient 4.3版本中对很多旧的类进行了deprecated标注,通常比较常用的就是下面两个类了. DefaultHttpClient -> CloseableHtt ...

  9. 【Kafka】

    KafkaProducer Kafka消息发布客户端. 线程安全,跨线程共享单个生产者实例通常比拥有多个实例的速度更快. 例子,使用生产者发送包含序列号的字符串作为键/值对的记录: Propertie ...

  10. 51 nod 1097 拼成最小的数 思路:字符串排序

    题目: 思路:1.以字符串输入这些整数. 2.对这些字符串排序,排序规则为尽量让能让结果变小的靠前. 代码中有注释,不懂的欢迎在博客中评论问我. 代码: #include <bits\stdc+ ...