尝试使用 Celery 编写时出现 RedisClusterException

RedisClusterException when trying to write using Celery

我的环境: 我有三台 Ubuntu 服务器。 一台服务器用作使用 Nginx 的负载平衡器。 其他两台服务器包含完全相同的项目(除 redis 外完全相同,其中一个是主机,另一个是从机)。

我用的Programs/Applications Python, 独角兽, 姜戈, 芹菜, Redis, 哨兵

我的项目做什么: 我有一个 URL,它接收 GET 请求,对请求进行一些逻辑处理并保存到 redis。

def save(key, value):
    conn = redis_cluster_connect()
    print conn
    conn.set(key, value)
    conn.set('bar','foo')

当我的连接是:

时这工作正常
def redis_connect():
    print 'redis connected'
    return redis.Redis(host="xxx.xx.xxx.x", port=6380, db=1) # CHANGE TO THE REDIS PORT THAT IS ACTIVE

但是当我使用集群连接时:

def redis_cluster_connect():
    startup_nodes = [{"host": "xxx.xx.xxx.x", "port": "6380"}]
    rc = StrictRedisCluster(startup_nodes=startup_nodes, decode_responses=True)

return rc

我得到的错误:

[2016-09-15 13:28:59,682: INFO/MainProcess] Received task: testapp.tasks.mobilise_stops[cc64c425-bd37-4896-b6ab-4319de5fb743]
    [2016-09-15 13:28:59,684: WARNING/Worker-1] Oh no! Task failed: RedisClusterException("ERROR sending 'cluster slots' command to redis server: {'host': 'xxx.xx.xxx.x', 'port': '6380'}",)
    [2016-09-15 13:28:59,685: ERROR/MainProcess] Task testapp.tasks.mobilise_stops[cc64c425-bd37-4896-b6ab-4319de5fb743] raised unexpected: RedisClusterException("ERROR sending 'cluster slots' command to redis server: {'host': 'xxx.xx.xxx.x', 'port': '6380'}",)
    Traceback (most recent call last):
      File "/usr/local/lib/python2.7/dist-packages/celery/app/trace.py", line 240, in trace_task
        R = retval = fun(*args, **kwargs)
      File "/usr/local/lib/python2.7/dist-packages/celery/app/trace.py", line 438, in __protected_call__
        return self.run(*args, **kwargs)
      File "/var/www/gateway/venv/gateway/testapp/tasks.py", line 50, in mobilise_stops
        save(mobilise_stops.request.id, 'Success')
      File "/var/www/gateway/venv/gateway/testapp/tasks.py", line 28, in save
        conn = redis_cluster_connect()
      File "/var/www/gateway/venv/gateway/testapp/tasks.py", line 19, in redis_cluster_connect
        rc = StrictRedisCluster(startup_nodes=startup_nodes, decode_responses=True)
      File "/usr/local/lib/python2.7/dist-packages/rediscluster/client.py", line 157, in __init__
        **kwargs
      File "/usr/local/lib/python2.7/dist-packages/rediscluster/connection.py", line 83, in __init__
        self.nodes.initialize()
      File "/usr/local/lib/python2.7/dist-packages/rediscluster/nodemanager.py", line 148, in initialize
        raise RedisClusterException("ERROR sending 'cluster slots' command to redis server: {0}".format(node))
    RedisClusterException: ERROR sending 'cluster slots' command to redis server: {'host': 'xxx.xx.xxx.x', 'port': '6380'}

当我 运行 redis-cli 并在主服务器(在服务器 1 上)上设置变量时,我可以在从服务器(在服务器 2 上)上检索它

server 1:
xxx.xx.xxx.x:6380> set test 100
OK

server 2:
xxx.xx.xxx.x:6381> get test 
"100"

当我尝试在 redis 客户端中执行 cluster slots 命令时,出现与上述相同的错误。

xxx.xx.xxx.x:6380> cluster slots
(error) ERR unknown command 'cluster'

需要考虑的一件事是我的 redis 配置文件没有 tcp 套接字,当我尝试使用 tcp redis 时将无法工作。当我尝试 运行ning redis 时它给了我一个错误。这已更改为默认 redis.conf 文件

中的默认 Unixsocket

发件人:

tcp-backlog 511

收件人:

unixsocket /var/run/redis/redis.sock
unixsocketperm 700

您没有 运行 建立 redis 集群,因此尝试 运行 redis-cluster only 命令将会失败。

就我而言,问题出在版本 redis-py-cluster (1.3.4) 上,重新安装到版本 1.3.1 后一切正常。我猜 python 包的版本有一些问题。