库更新后,要用新的方法链接

安装库

pip install redis-py-cluster

连接代码

from rediscluster import RedisCluster


class RedisManager(object):  # 连接redis集群
    def __init__(self, conn_list):
        self.conn_list = conn_list  # 连接列表

    def connect(self):
        """
        连接redis集群
        :return: object
        """
        try:
            redisconn = RedisCluster(startup_nodes=self.conn_list, decode_responses=True, password='basc')
            return redisconn
        except Exception as e:
            print(e)
            print("错误,连接redis 集群失败")
            return False


redis_basis_conn = [

    {'host': '192.168.20.200', 'port': 4444},
    {'host': '192.168.20.200', 'port': 4445},

    {'host': '192.168.20.201', 'port': 2222},
    {'host': '192.168.20.201', 'port': 2223},

    {'host': '192.168.20.203', 'port': 1111},
    {'host': '192.168.20.204', 'port': 1112}

]
res = RedisManager(redis_basis_conn).connect()
res.set("foo", "bar")
print(res.get("foo"))
Last modification:April 25, 2021
如果觉得我的文章对你有用,请随意赞赏