安装Redis集群库
pip install redis-py-cluster
第一种方法
python连接redis cluster集群
from rediscluster import StrictRedisCluster
#构建所有的节点
startup_nodes = [
{"host":"192.168.3.25", "port":6379}, # 主
{"host":"192.168.3.25", "port":7001}, # 6379的从数据库
{"host":"192.168.3.25", "port":6380}, # 主
{"host":"192.168.3.25", "port":7002}, # 6380的从数据库
{"host":"192.168.3.25", "port":6381}, # 主
{"host":"192.168.3.25", "port":7003} # 6381的从数据库
]
#构建StrictRedisCluster对象
redis_store= StrictRedisCluster(startup_nodes=startup_nodes, decode_responses=True)
# 设置key键为name、money; value值为 '老鹰'、'10亿'
redis_store.set('name', '老鹰')
redis_store.set('money', '10亿')
# 获取键为name,money
print("My name is: ", redis_store.get('name'))
print("I have money: ", redis_store.get('money'))
第二种方法
python连接阿里云Redis集合案例
from rediscluster import StrictRedisCluster
#主从阿里云Redis
startup_nodes = [{"host": "raaabbbccc.redis.rds.aliyuncs.com", "port": 6379}]
redis_store = StrictRedisCluster(startup_nodes=startup_nodes, password="你的redis密码", decode_responses=True, max_connections=200)