#!/usr/bin/env python
# coding: utf-8
from rediscluster import RedisCluster
#from rediscluster import StrictRedisCluster会报错
#StrictRedisCluster名字换成了RedisCluster
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 = RedisCluster(startup_nodes=self.conn_list, password='123456')
return redisconn
except Exception as e:
print(e)
print("错误,连接redis 集群失败")
return False
# 连接列表,注意:必须严格按照此格式来!
redis_basis_conn = [{'host': '192.168.10.168', 'port': 7201}, {'host': '192.168.10.169', 'port': 7201}, {'host': '192.168.10.170', 'port': 7201}, {'host': '192.168.10.171', 'port': 7201}, {'host': '192.168.10.142', 'port': 7201}, {'host': '192.168.10.143', 'port': 7201}]
redis_conn = RedisCluster(redis_basis_conn).connect() # redis连接对象
redis_conn.set('name','admin') # 插入一个值
print("name is: ", redis_conn.get('name')) # 查询值
python连接redis集群
最新推荐文章于 2024-07-05 16:34:27 发布