redis-py 模块是否在集群模式下与 Redis 一起工作?

Does the redis-py module work with Redis in Cluster mode?

我正在尝试使用 redis-py with redis in cluster mode, but I can't get it to work. I see that redis-py-cluster works, but I have a preference for redis-py as I have been using it and it's a recommended client

redis-py 不支持集群模式。集群具有完全不同的体系结构来服务于水平可扩展性的目的。 HA(高可用性)在其设计中并不是优先考虑的。因此,您不能将一个客户端用于另一个客户端。

redis-py-cluster 似乎有持续的开发/支持,它基于 redis.py。您链接的客户端页面不适用于 redis 集群。 "redis-py-cluster" 在 redis 集群页面上提到(寻找 "Playing with the cluster"):https://redis.io/topics/cluster-tutorial

除了集群之外,Redis 还支持 sentinel 设置以提供 HA,redis-py 支持。

你可以在redis集群中使用redis-py,但是由于不同的键被分区到不同的节点,你需要计算(通过crc16/crc32散列函数)哪个集群处理哪些键。

为了充分利用"cluster mode",在你不关心键位置的地方,你需要实现redis-py-cluster提供的"client side partitioning"和"query routing"。 (https://redis.io/topics/partitioning)

redis-py-cluster的一个主要缺点是它没有为"pipeline + transaction"

中的原子操作提供解决方案

录制到 redis-py documentation:

redis-py is now supports cluster mode and provides a client for Redis Cluster.

请注意,redis-py 在目前没有稳定版本的 4.1.0 版本中添加了此功能。如果你想安装它,你应该使用下面的命令:

pip install redis==4.1.0-rc1

也许你在看这个答案的时候,它是稳定的!因此,只需在没有 -rc1 后缀的情况下安装即可。

您可以像下面这样连接到您的 redis 集群:

>>> 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>>>]]