【Python之旅】第七篇(三):使用Redis订阅服务

本文涉及的产品
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
简介:

 在C/S架构中,可以充分地利用Redis订阅服务,实现服务器端和客户端的信息收发,下面说说在Python中如何使用Redistribute的订阅服务。

    这里要举的例子是,Server端进行服务的订阅,而Client端进行消息的广播发送。


1.方法一:Server端使用Redis操作+Client端使用Python交互器

(1)Server端

    进入Redis操作界面:

1
2
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day7/redis- 2.8 . 9 /src$ redis-cli
127.0 . 0.1 : 6379 >

    开始监听频道chan_107:

1
2
3
4
5
127.0 . 0.1 : 6379 > SUBSCRIBE chan_107
Reading messages... (press Ctrl-C to quit)
1 "subscribe"
2 "chan_107"
3 ) (integer)  1


(2)Client端

    创建redis_connector.py文件,用以连接Server端Redis:

1
2
3
#!/usr/bin/env python
import  redis
r = redis.Redis(host= '192.168.1.112' ,port= 6379 ,db= 0 )

    在Python交互器中导入redis_connector,并发布消息:

1
2
>>> redis.r.publish( 'chan_107' , 'hello my name is xpleaf' )
1L

    

    此时在Server端中就能看到Client端发布的消息了:

1
2
3
4
5
6
7
8
127.0 . 0.1 : 6379 > SUBSCRIBE chan_107
Reading messages... (press Ctrl-C to quit)
1 "subscribe"
2 "chan_107"
3 ) (integer)  1
1 "message"
2 "chan_107"
3 "hello my name is xpleaf"


2.方法二:Server端使用Python程序+Client端使用Python交互器

(1)Server端

    程序代码如下:

1
2
3
4
5
6
7
8
9
10
import  redis_connector  as  redis
 
channel =  'chan_107'     #频道应该要和发布者的一样,否则将无法订阅到发布者发布的消息
 
msg_queue = redis.r.pubsub() #bind listen instance
msg_queue.subscribe(channel)
 
while  True:
     data = msg_queue.parse_response() #waiting  for  the publisher
     print data

    可以看到这里也导入了redis_connector模块用来连接本地的Redis数据库,跟Client端的类似,只是IP地址改为'localhost',如下:

1
2
3
#!/usr/bin/env python
import  redis
r = redis.Redis(host= 'localhost' ,port= 6379 ,db= 0 )

    msg_queue.parse_response()即是用来响应Client端发布的消息,执行该程序,开始监听消息:

1
2
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day7/monitor/m_server/core$ python redis_sub.py 
===>光标停在此处,开始监听Client端发布的消息


(2)Client端

    创建redis_connector.py文件,用以连接Server端Redis:

1
2
3
#!/usr/bin/env python
import  redis
r = redis.Redis(host= '192.168.1.112' ,port= 6379 ,db= 0 )

    在Python交互器中导入redis_connector,并发布消息:

1
2
3
4
>>> redis.r.publish( 'chan_107' , 'hello my name is yonghaoye' )
2L
>>> redis.r.publish( 'chan_107' , 'second msg' )
2L

    上面两步其实和方法一是一样的。


    此时在Server端中也可以订阅到Client端发布的消息了:

1
2
3
4
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day7/monitor/m_server/core$ python redis_sub.py 
[ 'message' 'chan_107' 'hello my name is yonghaoye' ]
[ 'message' 'chan_107' 'second msg' ]
===>光标停在此处,继续监听Client端发布的消息


3.方法三:Server端使用Python程序+Client端使用Python程序

    直接给出Client端的程序代码:

1
2
3
4
import  redis_connector  as  redis
 
for  in  range( 10 ):
         redis.r.publish( 'chan_107' , 'This is the NO.%s msg I send to you'  % i)

    Server端开始监听:

1
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day7/monitor/m_server/core$ python redis_sub.py

    Client端发布消息:

1
[root@moban ~]# python publish.py

    

    在Server端中很快就可以监听到Client端发布的消息:

1
2
3
4
5
6
7
8
9
10
11
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day7/monitor/m_server/core$ python redis_sub.py 
[ 'message' 'chan_107' 'This is the NO.0 msg I send to you' ]
[ 'message' 'chan_107' 'This is the NO.1 msg I send to you' ]
[ 'message' 'chan_107' 'This is the NO.2 msg I send to you' ]
[ 'message' 'chan_107' 'This is the NO.3 msg I send to you' ]
[ 'message' 'chan_107' 'This is the NO.4 msg I send to you' ]
[ 'message' 'chan_107' 'This is the NO.5 msg I send to you' ]
[ 'message' 'chan_107' 'This is the NO.6 msg I send to you' ]
[ 'message' 'chan_107' 'This is the NO.7 msg I send to you' ]
[ 'message' 'chan_107' 'This is the NO.8 msg I send to you' ]
[ 'message' 'chan_107' 'This is the NO.9 msg I send to you' ]
相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore     ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库 ECS 实例和一台目标数据库 RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
相关文章
|
16天前
|
NoSQL Linux Redis
06- 你们使用Redis是单点还是集群 ? 哪种集群 ?
**Redis配置:** 使用哨兵集群,结构为1主2从,加上3个哨兵节点,总计分布在3台Linux服务器上,提供高可用性。
229 0
|
24天前
|
负载均衡 监控 NoSQL
Redis的集群方案有哪些?
Redis集群包括主从复制(基础,手动故障恢复)、哨兵模式(自动高可用)和Redis Cluster(官方分布式解决方案,自动分片和容错)。此外,还有如Codis、Redisson和Twemproxy等第三方工具用于代理和负载均衡。选择方案需考虑应用场景、数据规模和并发需求。
191 2
|
30天前
|
NoSQL Redis
Redis集群(六):集群常用命令及说明
Redis集群(六):集群常用命令及说明
187 0
|
2月前
|
运维 NoSQL 算法
Redis-Cluster 与 Redis 集群的技术大比拼
Redis-Cluster 与 Redis 集群的技术大比拼
82 0
|
1天前
|
存储 NoSQL 算法
Redis 搭建分片集群
Redis 搭建分片集群
|
24天前
|
NoSQL Java 测试技术
面试官:如何搭建Redis集群?
**Redis Cluster** 是从 Redis 3.0 开始引入的集群解决方案,它分散数据以减少对单个主节点的依赖,提升读写性能。16384 个槽位分配给节点,客户端通过槽位信息直接路由请求。集群是无代理、去中心化的,多数命令直接由节点处理,保持高性能。通过 `create-cluster` 工具快速搭建集群,但适用于测试环境。在生产环境,需手动配置文件,启动节点,然后使用 `redis-cli --cluster create` 分配槽位和从节点。集群动态添加删除节点、数据重新分片及故障转移涉及复杂操作,包括主从切换和槽位迁移。
32 0
面试官:如何搭建Redis集群?
|
28天前
|
存储 缓存 NoSQL
【Redis深度专题】「核心技术提升」探究Redis服务启动的过程机制的技术原理和流程分析的指南(集群功能分析)(一)
【Redis深度专题】「核心技术提升」探究Redis服务启动的过程机制的技术原理和流程分析的指南(集群功能分析)
322 0
|
1月前
|
NoSQL Redis Docker
使用Docker搭建一个“一主两从”的 Redis 集群(超详细步骤)
使用Docker搭建一个“一主两从”的 Redis 集群(超详细步骤)
68 0
|
1月前
|
存储 监控 NoSQL
Redis 架构深入:主从复制、哨兵到集群
大家好,我是小康,今天我们来聊下 Redis 的几种架构模式,包括主从复制、哨兵和集群模式。
Redis 架构深入:主从复制、哨兵到集群
|
1月前
|
运维 负载均衡 NoSQL
【大厂面试官】知道Redis集群和Redis主从有什么区别吗
集群节点之间的故障检测和Redis主从中的哨兵检测很类似,都是通过PING消息来检测的。。。面试官抓抓脑袋,继续看你的简历…得想想考点你不懂的😰。
67 1

热门文章

最新文章