Redis cluster 连接 (python)

Redis cluster 连接 (python)

安装

pip install redis-py-cluster

官方 Demo

>>> from rediscluster import RedisCluster

>>> # Requires at least one node for cluster discovery. Multiple nodes is recommended.
>>> startup_nodes = [{"host": "127.0.0.1", "port": "7000"}]

>>> # Note: See note on Python 3 for decode_responses behaviour
>>> rc = RedisCluster(startup_nodes=startup_nodes, decode_responses=True)

>>> rc.set("foo", "bar")
True
>>> print(rc.get("foo"))
'bar'

官方demo 没有运行过,然后我按照官网写了一个示例代码,但是,连不上,报如下错误

redis 连接集群

import sys
from rediscluster import RedisCluster
def init_redis():
    startup_nodes = [
        {'host': '10.90.116.153', 'port': 6379},
        {'host': '10.90.117.154', 'port': 6379},
        {'host': '10.90.128.155', 'port': 6379},
    ]
    try:
        conn = RedisCluster(startup_nodes=startup_nodes, 
                            # 有密码要加上密码哦
                            decode_responses=True, password='123456')
        print('连接成功!!!!!1', conn)
        return conn
    except Exception as e:
        print("connect error ", str(e))
        sys.exit(1)

init_redis()

报错

connect error  ERROR sending 'config get cluster-require-full-coverage' command to redis server: {'host': '10.90.122.155', 'port': 6379, 'name': '10.90.122.155:6379', 'server_type': 'master'}

然后我在网上一顿搜索,找不到答案,弄了几小时了吧,找各种操作,直接用命令就可以连上,代码也没啥问题。应该是参数配置不对吧,但是对 redis cluster 又不熟悉,于是无奈。

后来同事发给我一篇文章,我看了下觉得可能没希望,但是我还是加上了那个参数,最终尝试,连接成功。

参考: python 连接redis集群 ,常见报错解决。

加了个参数 skip_full_coverage_check = True,最后显示连接成功,但是跟参考中讲的报错完全不一样,我也很懵。

conn = RedisCluster(startup_nodes=startup_nodes, 
                            # 有密码要加上密码哦
                            skip_full_coverage_check = True,
                            decode_responses=True, password='123456')

这个只是作为一次 bug 记录,以便下次碰到能快速找到。

发布了30 篇原创文章 · 获赞 12 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/WBST5/article/details/103212686