我正在尝试将redis-py与Redis集群模式一起使用,但无法使其正常工作。我发现redis-py-cluster可以工作,但我更喜欢redis-py,因为我一直在使用它并且它是推荐的客户端。
redis-py
的文档:
请注意,
redis-py
现已支持集群模式,并提供 Redis 集群的客户端。
redis-py
在版本 4.1.0
中添加了此功能,目前还没有稳定版。如果要安装,请使用以下命令:pip install redis==4.1.0-rc1
也许当你阅读这个答案时,已经稳定了!所以只需安装而不需要使用后缀 -rc1
。您可以按照以下方式连接到redis-cluster:>>> from redis.cluster import RedisCluster as Redis
>>> rc = Redis(host='localhost', port=6379)
>>> print(rc.get_nodes())
[[host=127.0.0.1,port=6379,name=127.0.0.1:6379,server_type=primary,redis_connection=Redis<ConnectionPool<Connection<host=127.0.0.1,port=6379,db=0>>>], [host=127.0.0.1,port=6378,name=127.0.0.1:6378,server_type=primary,redis_connection=Redis<ConnectionPool<Connection<host=127.0.0.1,port=6378,db=0>>>], [host=127.0.0.1,port=6377,name=127.0.0.1:6377,server_type=replica,redis_connection=Redis<ConnectionPool<Connection<host=127.0.0.1,port=6377,db=0>>>]]