# 官方参考 https://github.com/Grokzen/redis-py-cluster
# pip install redis-py-cluster
from rediscluster import RedisCluster
def redis_init():
startup_nodes = [
{"host": "192.168.188.32", "port": "7101"},
{"host": "192.168.188.52", "port": "7102"},
{"host": "192.168.188.62", "port": "7103"}
]
try:
# 单节点
# rs = redis.Redis(host='192.168.188.32', port=7101, password='123456', db=0, decode_responses=True)
# 集群
conn = RedisCluster(startup_nodes=startup_nodes, decode_responses=True, password='123456')
print("连接成功!", conn)
return conn
except Exception as e:
print("connect error ", str(e))
sys.exit(1)
if __name__ == '__main__':
rs = redis_init()
print(rs.get("name"))
win10在PyCharm中安装 redis-py-cluster如图: