一 运行Python程序与redis交互报错提示:ImportError: cannot import name 'StrictRedisCluster'
from rediscluster import strictRedisCluster
if __name__ == "__main__":
'''python与redis server集群交互'''
try:
# 构建所有节点,redis会根据CRC16算法,将键和值分配到某个节点上
startup_nodes = [
{'host':'192.168.1.7','port':'7000'},
{'host':'192.168.1.7','port':'7001'},
{'host':'192168.1.7','port':'7002'},
]
# 创建StrictRedisCluster对象
src = strictRedisCluster(startup_nodes=startup_nodes,decode_response=True)
# 设置键值对 alis et
result = src.set('alis','et')
print(result)
# 获取键值对
ret = src.get('alis')
print(ret)
except Exception as e:
print(e)
找问题:pip list 命令查看redis 和redis_py_cluster版本
qiang@ubuntu:~/redis$ pip3 list
redis 3.5.3
redis-py-cluster 2.1.0
了解到redis5.0修改了方法不支持该StrictRedisCluster方法,使用RedisCluster方法,把代码中StrictRedisCluster替换为RedisCluster,且decode_responses(不是decode_response),否则也会报错:__init__() got an unexpected keyword argument 'decode_response',以上都修改好后,重新运行python3 xxx.py,成功!