本文主要是介绍python3连接redis sentinel集群,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
安装
pip install redis
示例代码
# import redis
from redis.sentinel import Sentinel"""
1、通过访问Sentinel服务的方式,获取redis的master、slave节点信息
2、向master redis写入数据
3、从slave redis读取数据
"""# 连接哨兵服务器(主机名也可以用域名)
sentinel = Sentinel([('192.168.196.129', 26379),('192.168.196.132', 26379)],socket_timeout=0.5)# 获取主服务器地址
master = sentinel.discover_master('mymaster')
print(master)
# 输出:('192.168.196.132', 6379)# 获取从服务器地址
slave = sentinel.discover_slaves('mymaster')
print(slave)
# 输出:[('192.168.196.129', 6379)]# 获取主服务器进行写入
master = sentinel.master_for('mymaster', socket_timeout=0.5, password='newpwd', db=0)
w_ret = master.set('foo', 'bar')
# 输出:True# 获取从服务器进行读取(默认是round-roubin,随机从多个slave服务中读取数据)
slave = sentinel.slave_for('mymaster', socket_timeout=0.5, password='newpwd', db=0)
r_ret = slave.get('foo')
print(r_ret)
# 输出:bar
封装工具类方法
from redis.sentinel import Sentinelclass redisSentinelHelper():def __init__(self,sentinel_list,service_name,password,db):self.sentinel = Sentinel(sentinel_list,socket_timeout=0.5)self.service_name = service_nameself.password = passwordself.db = dbdef get_master_redis(self):return self.sentinel.discover_master(self.service_name)def get_slave_redis(self):return self.sentinel.discover_slaves(self.service_name)def set_key(self,key,value):master = self.sentinel.master_for(service_name=self.service_name,socket_timeout=0.5,password=self.password,db=self.db)return master.set(key,value)def get_key(self,key):slave = self.sentinel.slave_for(service_name=self.service_name,socket_timeout=0.5,password=self.password,db=self.db)return slave.get(key)def _test():# redis infosentinel_list = [('192.168.196.129', 26379),('192.168.196.132', 26379)]password = 'newpwd'db = 0service_name = 'mymaster'# create redis linkrsh = redisSentinelHelper(sentinel_list=sentinel_list,password=password,service_name=service_name,db=db)# test set key : key1 test-insert-key1rsh.set_key('key1','test-insert-key1')# get key1print(rsh.get_key('key1'))if __name__ == '__main__':_test()
运行如下:
D:\Python37\python3.exe D:/pythonProject/redis-test/test7.py
b'test-insert-key1'
其中,我没有把设置master节点写在初始化,而是在set key操作的时候才创建连接,主要是后续想要测试master节点变化的情况下,写入能够继续。
当然这样的话性能肯定不会很好,有很多可以根据实际情况修改的地方。
测试:当master节点切换,能否自动连续写入
当然,在sentinel执行master切点切换的过程,肯定会有些丢失,但是主要是要看切换之后,是否可以自动继续写入数据。
首先编写一个循环写入的示例
from redis.sentinel import Sentinelclass redisSentinelHelper():def __init__(self,sentinel_list,service_name,password,db):self.sentinel = Sentinel(sentinel_list,socket_timeout=0.5)self.service_name = service_nameself.password = passwordself.db = dbdef get_master_redis(self):return self.sentinel.discover_master(self.service_name)def get_slave_redis(self):return self.sentinel.discover_slaves(self.service_name)def set_key(self,key,value):master = self.sentinel.master_for(service_name=self.service_name,socket_timeout=0.5,password=self.password,db=self.db)return master.set(key,value)def get_key(self,key):slave = self.sentinel.slave_for(service_name=self.service_name,socket_timeout=0.5,password=self.password,db=self.db)return slave.get(key)def _test():# redis infosentinel_list = [('192.168.196.129', 26379),('192.168.196.132', 26379)]password = 'newpwd'db = 0service_name = 'mymaster'# create redis linkrsh = redisSentinelHelper(sentinel_list=sentinel_list,password=password,service_name=service_name,db=db)# test set key : key1 test-insert-key1# rsh.set_key('key1','test-insert-key1')# get key1# print(rsh.get_key('key1'))# loop set keyfor i in range(0,1000000):rsh.set_key('key' + str(i), i)print(rsh.get_key('key' + str(i)))if __name__ == '__main__':_test()
运行如下:
在写入的过程停止master服务
脚本由于socket连接异常,直接报终止了。
查看sentinel的日志如下:
1825:X 17 Apr 17:44:51.101 # +sdown master mymaster 192.168.196.132 6379
1825:X 17 Apr 17:44:51.194 # +new-epoch 3
1825:X 17 Apr 17:44:51.195 # +vote-for-leader 3607cde178c7ac4255d5f862e8f3892f5599100f 3
1825:X 17 Apr 17:44:52.230 # +odown master mymaster 192.168.196.132 6379 #quorum 2/2
1825:X 17 Apr 17:44:52.230 # Next failover delay: I will not start a failover before Wed Apr 17 17:50:52 2019
1825:X 17 Apr 17:44:52.274 # +config-update-from sentinel 3607cde178c7ac4255d5f862e8f3892f5599100f 192.168.196.129 26379 @ mymaster 192.168.196.132 6379
1825:X 17 Apr 17:44:52.274 # +switch-master mymaster 192.168.196.132 6379 192.168.196.129 6379
1825:X 17 Apr 17:44:52.274 * +slave slave 192.168.196.131:6379 192.168.196.131 6379 @ mymaster 192.168.196.129 6379
1825:X 17 Apr 17:44:52.274 * +slave slave 192.168.196.132:6379 192.168.196.132 6379 @ mymaster 192.168.196.129 6379
1825:X 17 Apr 17:45:22.298 # +sdown slave 192.168.196.132:6379 192.168.196.132 6379 @ mymaster 192.168.196.129 6379
1825:X 17 Apr 17:45:22.298 # +sdown slave 192.168.196.131:6379 192.168.196.131 6379 @ mymaster 192.168.196.129 6379
[root@centos7-server02 redis]#
可以看到已经切换了master节点,这时候抛开刚才的异常问题,再执行一下脚本看看。
在master节点切换之后,再次运行脚本
直接就可以访问处理了。
这里就体现了通过sentinel集群来获取master节点的优势。
这篇关于python3连接redis sentinel集群的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!