我有AWS ElastiCache Redis集群,有2个节点。我正在使用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
型
任何想法为什么它不与连接池工作?
1条答案
按热度按时间nhjlsmyf1#
Redis集群将数据分片到集群中的所有节点上。
ConnectionPool
不支持集群,只能连接到一个节点。当数据驻留在另一个节点时,它会返回该节点的引用。如果你提供了ConnectionPool
,我怀疑RedisCluster
只使用该节点,即使集群中还有其他节点。RedisCluster
创建一个集群感知的客户端,并在后台执行重定向,因此可以从集群中的所有节点返回数据。如果您创建一个没有ConnectionPool
的RedisCluster
,它会自动创建所需的连接池。为了避免建立新的连接,请在代码中保持客户端处于活动状态。以下是
Redis-py
中池化内部工作方式的quote:连接池:RedisCluster在内部保存一个Redis客户端示例,用于与集群中的每个分片进行通信。每个Redis客户端示例都维护一个连接池到其分片,这允许它在与分片通信时重用连接。这节省了为每个命令建立新连接的性能损失。