python操作redis集群_python 连接管理作redis集群
python的redis庫是不支持集群操作的,推薦庫:redis-py-cluster。
安裝
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','weijing') # 插入一個值
print("name is: ", redis_conn.get('name')) # 查詢值
執行輸出:
name is: b'weijing'
總結
以上是生活随笔為你收集整理的python操作redis集群_python 连接管理作redis集群的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: procreate 笔刷_Procrea
- 下一篇: python基础文档_python基本文