我有带有 2 个节点的 AWS ElastiCache Redis 集群。 我正在使用 python 的 redis-py lib(版本 5.0.1) 在 pyspark 应用程序中看到以下错误:
│ File "/usr/local/lib/python3.9/dist-packages/redis/commands/core.py", line 4946, in hget │
│ return self.execute_command("HGET", name, key) │
│ File "/usr/local/lib/python3.9/dist-packages/redis/client.py", line 536, in execute_command │
│ return conn.retry.call_with_retry( │
│ File "/usr/local/lib/python3.9/dist-packages/redis/retry.py", line 46, in call_with_retry │
│ return do() │
│ File "/usr/local/lib/python3.9/dist-packages/redis/client.py", line 537, in <lambda> │
│ lambda: self._send_command_parse_response( │
│ File "/usr/local/lib/python3.9/dist-packages/redis/client.py", line 513, in _send_command_parse_response │
│ return self.parse_response(conn, command_name, **options) │
│ File "/usr/local/lib/python3.9/dist-packages/redis/client.py", line 553, in parse_response │
│ response = connection.read_response() │
│ File "/usr/local/lib/python3.9/dist-packages/redis/connection.py", line 524, in read_response │
│ raise response │
│ redis.exceptions.ResponseError: MOVED 393 redis-replication-0001-001.xxx.amazonaws.com:6379
代码,给出 MOVED 错误:
import redis
class redis_conn_pool:
def __init__(self, host, password, username):
self.host_ = host
self.pd_ = password
self.user_ = username
def connect(self):
pool = redis.ConnectionPool(host=self.host_,
port=6379,
password=self.pd_,
username=self.user_,
connection_class=redis.SSLConnection,
decode_responses = True,
)
conn = redis.RedisCluster(connection_pool=pool, host = self.host_, reinitialize_steps=1)
self.conn = conn
尝试了不同的 reinitialize_steps 值。
以下代码正在运行:
import redis
class redis_conn:
def __init__(self, host, password, username):
self.host_ = host
self.port_ = 6379
self.pd_ = password
self.user_ = username
def connect(self):
conn = redis.RedisCluster(host=self.host_,
port=self.port_,
password=self.pd_,
username=self.user_,
ssl=True,
decode_responses = True,
skip_full_coverage_check=True)
self.conn = conn
有什么想法为什么它不能与 ConnectionPool 一起使用吗?
Redis 集群将数据分片到集群中的所有节点上。
ConnectionPool
不支持集群,只能连接到一个节点。当数据驻留在另一个节点时,它会返回对该节点的引用。如果您提供 ConnectionPool
,我怀疑 RedisCluster
只使用该节点,即使集群中还有其他节点。
RedisCluster
创建一个集群感知客户端并在后台执行重定向,因此可以从集群中的所有节点返回数据。如果您创建一个没有 RedisCluster
的 ConnectionPool
,它会自动创建所需的连接池。