python 操作redis集群
一、连接redis集群
python的redis库是不支持集群操作的,推荐库:redis-py-cluster,一直在维护。还有一个rediscluster库,看GitHub上已经很久没更新了。
安装
pip3 install redis-py-cluster
连接redis集群

#!/usr/bin/env python # coding: utf-8 from rediscluster import StrictRedisCluster class RedisCluster(object): # 连接redis集群 def __init__(self,conn_list): self.conn_list = conn_list # 连接列表 def connect(self): """ 连接redis集群 :return: object """ try: # 非密码连接redis集群 # redisconn = StrictRedisCluster(startup_nodes=self.conn_list) # 使用密码连接redis集群 redisconn = StrictRedisCluster(startup_nodes=self.conn_list, password='123456') return redisconn except Exception as e: print(e) print("错误,连接redis 集群失败") return False redis_basis_conn = [{'host': '192.168.10.168', 'port': 7201}, {'host': '192.168.10.169', 'port': 7201}, {'host': '192.168.10.170', 'port': 7201}, {'host': '192.168.10.171', 'port': 7201}, {'host': '192.168.10.142', 'port': 7201}, {'host': '192.168.10.143', 'port': 7201}] res = RedisCluster(redis_basis_conn).connect() if not res: print("连接redis集群失败") else: print("连接redis集群成功")
执行输出:
连接redis集群成功
二、操作redis集群
查看节点状态

#!/usr/bin/env python # coding: utf-8 from rediscluster import StrictRedisCluster class RedisCluster(object): # 连接redis集群 def __init__(self,conn_list): self.conn_list = conn_list # 连接列表 def connect(self): """ 连接redis集群 :return: object """ try: # 非密码连接redis集群 # redisconn = StrictRedisCluster(startup_nodes=self.conn_list) # 使用密码连接redis集群 redisconn = StrictRedisCluster(startup_nodes=self.conn_list, password='123456') return redisconn except Exception as e: print(e) print("错误,连接redis 集群失败") return False def get_state(self): """ 获取状态 :return: """ res = RedisCluster(self.conn_list).connect() # print("连接集群对象",res,type(res),res.__dict__) if not res: return False dic = res.cluster_info() # 查看info信息, 返回dict for i in dic: # 遍历dict ip = i.split(":")[0] if dic[i].get('cluster_state'): # 获取状态 print("节点状态, ip: ", ip, "value: ", dic[i].get('cluster_state')) redis_basis_conn = [{'host': '192.168.10.168', 'port': 7201}, {'host': '192.168.10.169', 'port': 7201}, {'host': '192.168.10.170', 'port': 7201}, {'host': '192.168.10.171', 'port': 7201}, {'host': '192.168.10.142', 'port': 7201}, {'host': '192.168.10.143', 'port': 7201}] RedisCluster(redis_basis_conn).get_state()
执行输出:
节点状态, ip: 192.168.10.171 value: ok 节点状态, ip: 192.168.10.169 value: ok 节点状态, ip: 192.168.10.143 value: ok 节点状态, ip: 192.168.10.142 value: ok 节点状态, ip: 192.168.10.170 value: ok 节点状态, ip: 192.168.10.168 value: ok
查看aof是否开启

#!/usr/bin/env python # coding: utf-8 from rediscluster import StrictRedisCluster class RedisCluster(object): # 连接redis集群 def __init__(self,conn_list): self.conn_list = conn_list # 连接列表 def connect(self): """ 连接redis集群 :return: object """ try: # 非密码连接redis集群 # redisconn = StrictRedisCluster(startup_nodes=self.conn_list) # 使用密码连接redis集群 redisconn = StrictRedisCluster(startup_nodes=self.conn_list, password='123456') return redisconn except Exception as e: print(e) print("错误,连接redis 集群失败") return False def get_info(self): """ 获取redis集群info信息 :return: dict """ res = RedisCluster(self.conn_list).connect() # print("连接集群对象",res,type(res),res.__dict__) if not res: return False dic = res.cluster_info() # 查看info信息, 返回dict if not dic: return False return dic def get_state(self): """ 获取状态 :return: """ dic = self.get_info() # type:dict if not dic: return dic for i in dic: # 遍历dict ip = i.split(":")[0] if dic[i].get('cluster_state'): # 获取状态 print("节点状态, ip: ", ip, "value: ", dic[i].get('cluster_state')) def get_has_aof(self): """ 查看aof是否打开 :return: """ res = RedisCluster(self.conn_list).connect() # print("连接集群对象",res,type(res),res.__dict__) if not res: return False dic = res.config_get('appendonly') # 从config配置项中查询appendonly for i in dic: ip = i.split(":")[0] # print(dic[i]) if dic[i].get('appendonly'): print("aof开关, ip: ", ip,"value: ",dic[i].get('appendonly')) redis_basis_conn = [{'host': '192.168.10.168', 'port': 7201}, {'host': '192.168.10.169', 'port': 7201}, {'host': '192.168.10.170', 'port': 7201}, {'host': '192.168.10.171', 'port': 7201}, {'host': '192.168.10.142', 'port': 7201}, {'host': '192.168.10.143', 'port': 7201}] RedisCluster(redis_basis_conn).get_has_aof()
执行输出:
aof开关, ip: 192.168.10.170 value: no aof开关, ip: 192.168.10.168 value: no aof开关, ip: 192.168.10.142 value: no aof开关, ip: 192.168.10.171 value: no aof开关, ip: 192.168.10.169 value: no aof开关, ip: 192.168.10.143 value: no
set和get

#!/usr/bin/env python # coding: utf-8 from rediscluster import StrictRedisCluster class RedisCluster(object): # 连接redis集群 def __init__(self,conn_list): self.conn_list = conn_list # 连接列表 def connect(self): """ 连接redis集群 :return: object """ try: # 非密码连接redis集群 # redisconn = StrictRedisCluster(startup_nodes=self.conn_list) # 使用密码连接redis集群 redisconn = StrictRedisCluster(startup_nodes=self.conn_list, password='123456') return redisconn except Exception as e: print(e) print("错误,连接redis 集群失败") return False # 连接列表,注意:必须严格按照此格式来! redis_basis_conn = [{'host': '192.168.10.168', 'port': 7201}, {'host': '192.168.10.169', 'port': 7201}, {'host': '192.168.10.170', 'port': 7201}, {'host': '192.168.10.171', 'port': 7201}, {'host': '192.168.10.142', 'port': 7201}, {'host': '192.168.10.143', 'port': 7201}] redis_conn = RedisCluster(redis_basis_conn).connect() # redis连接对象 redis_conn.set('name','admin') # 插入一个值 print("name is: ", redis_conn.get('name')) # 查询值
执行输出:
name is: b'admin'
注意:get出来的值,是bytes类型的。
其他redis操作,比如hget,hgetall... 和redis单例模式,是一样的。
这里就不一一演示了
分类:
python 运维开发
标签:
运维开发
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
2018-05-23 python 全栈开发,Day47(行级块级标签,高级选择器,属性选择器,伪类选择器,伪元素选择器,css的继承性和层叠性,层叠性权重相同处理,盒模型,padding,border,margin)