Python连接redis集群方法
1. 安装redis、rediscluster模块
pip install redis
pip install redis-py-cluster
2. 示例代码
# -*- coding: UTF-8 -*-
from rediscluster import RedisCluster
#构建所有的节点
startup_nodes = [
{"host": "192.168.2.50", "port": 6379}, # 主
{"host": "192.168.2.50", "port": 6381}, # 6379的从数据库
{"host": "192.168.2.50", "port": 6383}, # 主
{"host": "192.168.2.50", "port": 6385}, # 6380的从数据库
{"host": "192.168.2.50", "port": 6387}, # 主
{"host": "192.168.2.50", "port": 6389} # 6381的从数据库
]
#构建StrictRedisCluster对象
redis_store = RedisCluster(startup_nodes=startup_nodes, decode_responses=True)
redis_store.set("name", "helloworld")
print("My name is: ", redis_store.get('name'))
结果: