# encoding=utf8
# pip install redis
from redis.sentinel import Sentinel
from redis import WatchError
from itertools import izip_longest
# from itertools import zip_longest
MYSETINEL = None
MASTER = None
SLAVE = None
SENTINEADDRESS = [('192.161.0.89', 26379), ('192.161.0.90', 26379), ('192.161.0.91', 26379)]
def get_redis_conn():
global MYSETINEL
global MASTER
global SLAVE
# 如果哨兵连接实例已存在, 不重复连接, 当连接失效时, 重新连接
if not MYSETINEL: # 连接哨兵
MYSETINEL = Sentinel(SENTINEADDRESS, socket_timeout=2000) # 尝试连接最长时间单位毫秒, 1000毫秒为1秒
# 通过哨兵获取主数据库连接实例 参数1: 主数据库的名字(集群部署时在配置文件里指明)
MASTER = MYSETINEL.master_for('mymaster', socket_timeout=2000, db=11, password="dattttt2l")
# 通过哨兵获取从数据库连接实例 参数1: 从数据的名字(集群部署时在配置文件里指
python3 redis哨兵模式 删除固定前缀的 key
最新推荐文章于 2025-01-09 17:22:25 发布