日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python操作redis集群_python 连接管理作redis集群

發布時間:2024/7/23 python 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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集群的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。