嗨,我使用官方的 docker-compose 文件来设置我的 redis 和 sentinel,如下所示:
version: '2'
networks:
app-tier:
driver: bridge
services:
redis:
image: 'bitnami/redis:latest'
environment:
- REDIS_REPLICATION_MODE=master
- REDIS_PASSWORD=str0ng_passw0rd
networks:
- app-tier
ports:
- '6379'
redis-slave:
image: 'bitnami/redis:latest'
environment:
- REDIS_REPLICATION_MODE=slave
- REDIS_MASTER_HOST=redis
- REDIS_MASTER_PASSWORD=str0ng_passw0rd
- REDIS_PASSWORD=str0ng_passw0rd
ports:
- '6379'
depends_on:
- redis
networks:
- app-tier
redis-sentinel:
image: 'bitnami/redis-sentinel:latest'
environment:
- REDIS_MASTER_PASSWORD=str0ng_passw0rd
depends_on:
- redis
- redis-slave
ports:
- '26379-26381:26379'
networks:
- app-tier
然后我在 celery 中设置我的 sentinel URL 如下:
REDIS_URL = "sentinel://192.168.5.80:26381"
print("REDIS_URL", REDIS_URL)
app = celery.Celery('tasker')
app.conf.broker_url = REDIS_URL
app.conf.broker_transport_options = { 'master_name': 'mymaster'}}
但不知何故我在一段时间后遇到错误:
[2023-05-11 21:03:35,301: CRITICAL/MainProcess] Unrecoverable error: TypeError('Channel._connparams.<locals>.Connection.disconnect() takes 1 positional argument but 2 were given')
Traceback (most recent call last):
File "/home/worker/.local/lib/python3.11/site-packages/kombu/transport/virtual/base.py", line 925, in create_channel
return self._avail_channels.pop()
^^^^^^^^^^^^^^^^^^^^^^^^^^
IndexError: pop from empty list
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/worker/.local/lib/python3.11/site-packages/redis/retry.py", line 46, in call_with_retry
return do()
^^^^
File "/home/worker/.local/lib/python3.11/site-packages/redis/connection.py", line 610, in <lambda>
lambda: self._connect(), lambda error: self.disconnect(error)
^^^^^^^^^^^^^^^
File "/home/worker/.local/lib/python3.11/site-packages/redis/connection.py", line 675, in _connect
raise err
File "/home/worker/.local/lib/python3.11/site-packages/redis/connection.py", line 663, in _connect
sock.connect(socket_address)
TimeoutError: [Errno 110] Connection timed out
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/worker/.local/lib/python3.11/site-packages/celery/worker/worker.py", line 203, in start
self.blueprint.start(self)
File "/home/worker/.local/lib/python3.11/site-packages/celery/bootsteps.py", line 116, in start
step.start(parent)
File "/home/worker/.local/lib/python3.11/site-packages/celery/bootsteps.py", line 365, in start
return self.obj.start()
^^^^^^^^^^^^^^^^
File "/home/worker/.local/lib/python3.11/site-packages/celery/worker/consumer/consumer.py", line 332, in start
blueprint.start(self)
File "/home/worker/.local/lib/python3.11/site-packages/celery/bootsteps.py", line 116, in start
step.start(parent)
File "/home/worker/.local/lib/python3.11/site-packages/celery/worker/consumer/connection.py", line 21, in start
c.connection = c.connect()
^^^^^^^^^^^
File "/home/worker/.local/lib/python3.11/site-packages/celery/worker/consumer/consumer.py", line 428, in connect
conn = self.connection_for_read(heartbeat=self.amqheartbeat)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/worker/.local/lib/python3.11/site-packages/celery/worker/consumer/consumer.py", line 434, in connection_for_read
return self.ensure_connected(
^^^^^^^^^^^^^^^^^^^^^^
File "/home/worker/.local/lib/python3.11/site-packages/celery/worker/consumer/consumer.py", line 460, in ensure_connected
conn = conn.ensure_connection(
^^^^^^^^^^^^^^^^^^^^^^^
File "/home/worker/.local/lib/python3.11/site-packages/kombu/connection.py", line 381, in ensure_connection
self._ensure_connection(*args, **kwargs)
File "/home/worker/.local/lib/python3.11/site-packages/kombu/connection.py", line 433, in _ensure_connection
return retry_over_time(
^^^^^^^^^^^^^^^^
File "/home/worker/.local/lib/python3.11/site-packages/kombu/utils/functional.py", line 312, in retry_over_time
return fun(*args, **kwargs)
File "/home/worker/.local/lib/python3.11/site-packages/redis/retry.py", line 49, in call_with_retry
fail(error)
File "/home/worker/.local/lib/python3.11/site-packages/redis/connection.py", line 610, in <lambda>
lambda: self._connect(), lambda error: self.disconnect(error)
^^^^^^^^^^^^^^^^^^^^^^
TypeError: Channel._connparams.<locals>.Connection.disconnect() takes 1 positional argument but 2 were given
你能告诉我哪里出错了吗?我什至尝试在 URL 或下面传递密码,但它没有用。
app.conf.broker_transport_options = { 'master_name': 'mymaster' , 'sentinel_kwargs': {'password': redis_password}}
仅供参考:我在需求中使用最新版本的
celery
和redis
,我不得不删除一些回溯,因为stackoverflow将其检测为垃圾邮件
连接详细信息:确保您的 Flask 应用程序中有正确的主机和端口详细信息。请记住,Redis Sentinel 运行在与普通 Redis 实例(运行在 6379 上)不同的端口(根据您的 Docker Compose 文件,通常为 26379)。确保您的 Flask 应用程序正在尝试连接到正确的端口。
网络访问:如果您的 Flask 应用程序在 Docker 容器内运行,请确保它与您的 Redis Sentinel 服务位于同一 Docker 网络上。如果他们在不同的网络上,他们将无法通信。根据您的 Docker Compose 文件,您的 Redis 服务正在“应用层”网络上运行。你的 Flask 应用程序也应该在这个网络上。
防火墙规则:如果您的 Flask 应用程序在不同的机器上运行,请确保没有防火墙规则阻止它连接到 Redis Sentinel 端口。
使用正确的客户端:确保您在支持 Sentinel 的 Flask 应用程序中使用 Redis 客户端。 redis-py 是一个流行的支持 Sentinel 的 Redis Python 客户端。下面是使用 redis-py 通过 Sentinel 连接到 Redis 的示例代码:
在此示例中,将“localhost”替换为您的 Redis Sentinel 主机,将“mymaster”替换为您的 Redis 主机名。
检查 Redis Sentinel 日志:最后,检查 Redis Sentinel 容器的日志是否有任何错误或警告。您可以通过在终端中运行 docker logs
记得根据您的设置更新主机、端口和密码等敏感信息。希望这对您有所帮助,如果您还有其他问题,请告诉我!