Python 操作Redis集群

布满荆棘的人生 2022-11-29 13:05 250阅读 0赞

Python 的 Redis 库是不支持集群操作的,推荐库:redis-py-cluster

安装

  1. pip install redis-py-cluster

连接集群并操作set

  1. from rediscluster import StrictRedisCluster
  2. class RedisCluster(object): # 连接redis集群
  3. def __init__(self, conn_list):
  4. self.conn_list = conn_list # 连接列表
  5. def connect(self):
  6. """ 连接redis集群 :return: object """
  7. try:
  8. # 非密码连接redis集群
  9. # redisconn = StrictRedisCluster(startup_nodes=self.conn_list)
  10. # 使用密码连接redis集群
  11. redisconn = StrictRedisCluster(startup_nodes=self.conn_list, password='8Mbh8Ykz')
  12. return redisconn
  13. except Exception as e:
  14. print(e)
  15. print("错误,连接redis 集群失败")
  16. return False
  17. if __name__ == '__main__':
  18. redis_basis_conn = [{ 'host': '192.17.210.84', 'port': 7001}, { 'host': '192.17.210.85', 'port': 7003},
  19. { 'host': '192.17.210.86', 'port': 7005}, { 'host': '192.17.210.84', 'port': 7002},
  20. { 'host': '192.17.210.84', 'port': 7004}, { 'host': '192.17.210.84', 'port': 7006}]
  21. res = RedisCluster(redis_basis_conn).connect()
  22. if not res:
  23. print("连接redis集群失败")
  24. else:
  25. print("连接redis集群成功")
  26. redis_conn = RedisCluster(redis_basis_conn).connect() # redis连接对象
  27. parames = { 500059684,500059713,500059714,500059670,500059675,500059677,500059678}
  28. for parame in parames:
  29. redis_conn.sadd('search_uids', parame)

发表评论

表情:
评论列表 (有 0 条评论,250人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Redis常规操作

    一、故障转移 redis集群实现了高可用,当集群内少量节点出现故障时,通过故障转移可以保证集群正常对外提供服务。 当集群里某个节点出现了问题,redis集群内的节点通过