Python 的 Redis 库是不支持集群操作的,推荐库:redis-py-cluster
pip install redis-py-cluster
from rediscluster import StrictRedisCluster
class RedisCluster(object): # 连接redis集群
def __init__(self, conn_list):
self.conn_list = conn_list # 连接列表
def connect(self):
"""
连接redis集群
:return: object
"""
try:
# 非密码连接redis集群
# redisconn = StrictRedisCluster(startup_nodes=self.conn_list)
# 使用密码连接redis集群
redisconn = StrictRedisCluster(startup_nodes=self.conn_list, password='8Mbh8Ykz')
return redisconn
except Exception as e:
print(e)
print("错误,连接redis 集群失败")
return False
if __name__ == '__main__':
redis_basis_conn = [{'host': '192.17.210.84', 'port': 7001}, {'host': '192.17.210.85', 'port': 7003},
{'host': '192.17.210.86', 'port': 7005}, {'host': '192.17.210.84', 'port': 7002},
{'host': '192.17.210.84', 'port': 7004}, {'host': '192.17.210.84', 'port': 7006}]
res = RedisCluster(redis_basis_conn).connect()
if not res:
print("连接redis集群失败")
else:
print("连接redis集群成功")
redis_conn = RedisCluster(redis_basis_conn).connect() # redis连接对象
parames = {500059684,500059713,500059714,500059670,500059675,500059677,500059678}
for parame in parames:
redis_conn.sadd('search_uids', parame)