我在使用 Celery 和 Redis 时遇到问题。出于某种原因,Celery 不会连接到远程 Redis 服务器。我已经阅读了这里的 celery + redis 帖子,但还没有找到解决方案。
到目前为止,我已经尝试了以下方法:
redis.conf
从服务器中删除绑定并重新启动- 仔细检查 Celery 是否具有我在
celeryconfig.py
文件中指定的正确设置。 - 已验证
BROKER_TRANSPORT
设置为'redis'
celery.broker_connection().as_uri()
使用该方法验证我的配置文件正在加载- 验证我可以使用连接到我的 redis 服务器
redis-cli -h w2214.area1.company.com:6379
并且redis ping
命令返回+PONG
- 验证我可以使用连接到我的 redis 服务器
telnet w2214.area1.company.com 6379
但是当我运行我的工作脚本时我仍然遇到连接失败 111 并且我很难过。这是追溯。我已经替换了下面显示的 URL 中的公司名称,以保护我工作的公司的隐私。
initializing celery
celery.broker_connection() = redis://w2214.area1.company.com:6379/0
BROKER_URL: redis://w2214.area1.company.com:6379/0
CELERY_RESULT_BACKEND: redis://w2214.area1.company.com:6379/0
Traceback (most recent call last):
File "test_worker_standalone.py", line 66, in <module>
result = tasks.get_schema.apply_async((), queue=options.queue)
File "/nfs/site/home/rodney/projects/mos-env/lib/python2.7/site-packages/celery/app/task.py", line 474, in apply_async
**options)
File "/nfs/site/home/rodney/projects/mos-env/lib/python2.7/site-packages/celery/app/amqp.py", line 250, in publish_task
**kwargs
File "/nfs/site/home/rodney/projects/mos-env/lib/python2.7/site-packages/kombu/messaging.py", line 164, in publish
routing_key, mandatory, immediate, exchange, declare)
File "/nfs/site/home/rodney/projects/mos-env/lib/python2.7/site-packages/kombu/connection.py", line 470, in _ensured
interval_max)
File "/nfs/site/home/rodney/projects/mos-env/lib/python2.7/site-packages/kombu/connection.py", line 396, in ensure_connection
interval_start, interval_step, interval_max, callback)
File "/nfs/site/home/rodney/projects/mos-env/lib/python2.7/site-packages/kombu/utils/__init__.py", line 217, in retry_over_time
return fun(*args, **kwargs)
File "/nfs/site/home/rodney/projects/mos-env/lib/python2.7/site-packages/kombu/connection.py", line 246, in connect
return self.connection
File "/nfs/site/home/rodney/projects/mos-env/lib/python2.7/site-packages/kombu/connection.py", line 761, in connection
self._connection = self._establish_connection()
File "/nfs/site/home/rodney/projects/mos-env/lib/python2.7/site-packages/kombu/connection.py", line 720, in _establish_connection
conn = self.transport.establish_connection()
File "/nfs/site/home/rodney/projects/mos-env/lib/python2.7/site-packages/kombu/transport/virtual/__init__.py", line 742, in establish_connection
self._avail_channels.append(self.create_channel(self))
File "/nfs/site/home/rodney/projects/mos-env/lib/python2.7/site-packages/kombu/transport/virtual/__init__.py", line 725, in create_channel
channel = self.Channel(connection)
File "/nfs/site/home/rodney/projects/mos-env/lib/python2.7/site-packages/kombu/transport/redis.py", line 365, in __init__
self.client.info()
File "/nfs/site/home/rodney/projects/mos-env/lib/python2.7/site-packages/redis/client.py", line 480, in info
return self.execute_command('INFO')
File "/nfs/site/home/rodney/projects/mos-env/lib/python2.7/site-packages/redis/client.py", line 397, in execute_command
connection.send_command(*args)
File "/nfs/site/home/rodney/projects/mos-env/lib/python2.7/site-packages/redis/connection.py", line 306, in send_command
self.send_packed_command(self.pack_command(*args))
File "/nfs/site/home/rodney/projects/mos-env/lib/python2.7/site-packages/redis/connection.py", line 288, in send_packed_command
self.connect()
File "/nfs/site/home/rodney/projects/mos-env/lib/python2.7/site-packages/redis/connection.py", line 235, in connect
raise ConnectionError(self._error_message(e))
redis.exceptions.ConnectionError: Error 111 connecting localhost:6379. Connection refused.
最后,我正在使用以下软件版本:
amqp (1.0.13)
anyjson (0.3.3)
autoenv (1.0.0)
billiard (2.7.3.34)
celery (3.0.24)
celery-with-redis (3.0)
Cython (0.19.1)
distribute (0.7.3)
Django (1.5.1)
kombu (2.5.16)
pbr (0.5.21)
pip (1.4.1)
python-dateutil (2.2)
redis (2.8.0)
setuptools (0.9.8)
six (1.4.1)
stevedore (0.12)
virtualenv (1.10.1)
virtualenv-clone (0.2.4)
virtualenvwrapper (4.1.1)
wsgiref (0.1.2)
我的redis版本是:Redis server v=2.6.16
我注意到一件有趣的事情,我不确定它是否相关,但回溯显示以下错误,即使我指定了要使用的远程服务器:
Error 111 connecting localhost:6379. Connection refused.
感谢您抽出宝贵的时间以及您能给我的任何帮助。
更新:我修改了 redis 库Connection
对象__init__() (redis/connecton.py:211)
并硬编码了我的 redis 服务器的主机名,一切正常。所以我将继续尝试确定为什么 Connection 对象没有收到我在 Celery 中指定的主机名。
更新#2:这里要求的是我的芹菜设置(celeryconfig.py):
BROKER_TRANSPORT = 'redis'
CELERY_BROKER_URL = 'redis://w2214.area1.company.com:6379/0'
CELERY_RESULT_BACKEND = 'redis://w2214.area1.company.com:6379/0'
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = 'America/Chicago'
CELERY_ENABLE_UTC = True
CELERYD_LOG_COLOR = False
CELERY_IMPORTS = ("tasks", )
CELERY_TASK_RESULT_EXPIRES = 300
我的 redis.conf 设置是'bind'
删除语句的默认设置。