python 操做redis集羣

1、鏈接redis集羣

python的redis庫是不支持集羣操做的,推薦庫:redis-py-cluster,一直在維護。還有一個rediscluster庫,看GitHub上已經好久沒更新了。
node

安裝

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集羣成功")
View Code

 

執行輸出:python

鏈接redis集羣成功

 

2、操做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()
View Code

 

執行輸出:redis

節點狀態, 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()
View Code

 

執行輸出:app

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'))  # 查詢值
View Code

 

執行輸出:ide

name is:  b'admin'

 

注意:get出來的值,是bytes類型的。spa

 

其餘redis操做,好比hget,hgetall... 和redis單例模式,是同樣的。code

這裏就不一一演示了對象

相關文章
相關標籤/搜索