如何使用python在redis集群模式下执行get命令?

时间:2017-06-12 10:06:19

标签: python redis

我想使用python在redis上执行以下场景。

使用命令行: 1)redis-cli -c 2)redis_prompt>>得到some_string

我尝试使用redis和rediscluster模块,但没有运气。以下是我尝试过的代码:

1)

r = redis.Redis(host='123.123.123.123', port=6379, db=0)
r.get('some srting')

出现以下错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\site-packages\redis\client.py", line 880, in get
    return self.execute_command('GET', name)
  File "C:\Python27\lib\site-packages\redis\client.py", line 573, in execute_command
    return self.parse_response(connection, command_name, **options)
  File "C:\Python27\lib\site-packages\redis\client.py", line 585, in parse_response
    response = connection.read_response()
  File "C:\Python27\lib\site-packages\redis\connection.py", line 582, in read_response
    raise response
redis.exceptions.ResponseError: MOVED 9442 172.16.176.36:6380

2)

from rediscluster import StrictRedisCluster
startup_nodes = [{"host": "123.123.123.123", "port": "7000"}]
rc_readonly = StrictRedisCluster(startup_nodes=startup_nodes, decode_responses=True, readonly_mode=True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\site-packages\rediscluster\client.py", line 181, in __init__
    **kwargs
  File "C:\Python27\lib\site-packages\rediscluster\connection.py", line 353, in __init__
    **connection_kwargs)
  File "C:\Python27\lib\site-packages\rediscluster\connection.py", line 141, in __init__
    self.nodes.initialize()
  File "C:\Python27\lib\site-packages\rediscluster\nodemanager.py", line 240, in initialize
    raise RedisClusterException("Redis Cluster cannot be connected. Please provide at least one reachable node.")
rediscluster.exceptions.RedisClusterException: Redis Cluster cannot be connected. Please provide at least one reachable node.

任何帮助都会很棒。

1 个答案:

答案 0 :(得分:0)

好的,所以这个错误意味着以下之一:

  • 在这些代码行中(1):

    r = redis.Redis(host='123.123.123.123', port=6379, db=0)
    
    r.get('some srting')
    

    您尝试从主机123.123.123.123获取密钥“some sting” 端口6379。错误redis.exceptions.ResponseError: MOVED 9442 172.16.176.36:6380表示此密钥位于172.16.176.36:6380。因此,尝试在此节点中与。/redis-cli -c -p 6380连接 执行CLUSTER GETKEYSINSLOT 6380 3以查看此密钥是否确实存在 这里。

  • 从(2)出现此错误:rediscluster.exceptions.RedisClusterException: Redis Cluster cannot be connected. Please provide at least one reachable node表示群集出现问题。也许,你没有正确的初始化。

    首先在/home/{{user}}/redis-yourversion/utils/create-cluster你 会找到create-cluster.sh。然后设置端口和主机和节点 执行./create-cluster start ./create-cluster-create。但首先是你 必须以群集模式启动redis服务器。转到redis.conf并进入 您将看到cluster-enabled no的配置参数。建立 是的在此之后你必须运行./redis-server ../redis.conf(给 redis.conf的路径。如果群集启用正确,您将看到Running in cluster mode

要记住的重要事项:

终端中的参数-c表示redis节点可以执行重定向并找到密钥所属的正确节点,而不会出现MOVED错误。如果你有一个驱动程序假设在python中操作节点和获取密钥或你自己必须管理这些重定向的任何东西。