私の環境: 3 つの Ubuntu サーバーがあります。1 台のサーバーは、Nginx を使用したロード バランサーとして使用されます。他の 2 つのサーバーには、まったく同じプロジェクトが含まれています (一方のボックスがマスターで、もう一方がスレーブである redis を除いて同一です)。
私が使用しているプログラム/アプリケーション Python、Gunicorn、Django、Celery、Redis、Sentinel
私のプロジェクトの内容: GET リクエストを受け取り、リクエストに関するいくつかのロジックを実行し、redis に保存する URL があります。
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 クライアントでクラスター スロット コマンドを実行しようとすると、上記と同じエラーが発生します。
xxx.xx.xxx.x:6380> cluster slots
(error) ERR unknown command 'cluster'
考慮すべきことの 1 つは、tcp redis を使用しようとしたときに、私の redis 構成ファイルに tcp ソケットがないことです。redisを実行しようとすると、エラーが発生しました。これは、デフォルトの redis.conf ファイルに含まれるデフォルトの Unixsocket に変更されました。
から:
tcp-backlog 511
に:
unixsocket /var/run/redis/redis.sock
unixsocketperm 700