一、连接redis集群

python的redis库是不支持集群操作的,推荐库:redis-py-cluster,一直在维护。还有一个rediscluster库,看GitHub上已经很久没更新了。

安装

pip3 install redis-py-cluster

连接redis集群

#!/usr/bin/env python
# coding: utf-8 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='')
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}] res = RedisCluster(redis_basis_conn).connect()
if not res:
print("连接redis集群失败")
else:
print("连接redis集群成功")

执行输出:

连接redis集群成功

二、操作redis集群

查看节点状态

#!/usr/bin/env python
# coding: utf-8 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='')
return redisconn
except Exception as e:
print(e)
print("错误,连接redis 集群失败")
return False def get_state(self):
"""
获取状态
:return:
"""
res = RedisCluster(self.conn_list).connect()
# print("连接集群对象",res,type(res),res.__dict__)
if not res:
return False dic = res.cluster_info() # 查看info信息, 返回dict for i in dic: # 遍历dict
ip = i.split(":")[0]
if dic[i].get('cluster_state'): # 获取状态
print("节点状态, ip: ", ip, "value: ", dic[i].get('cluster_state')) 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}] RedisCluster(redis_basis_conn).get_state()

执行输出:

节点状态, ip:  192.168.10.171 value:  ok
节点状态, ip: 192.168.10.169 value: ok
节点状态, ip: 192.168.10.143 value: ok
节点状态, ip: 192.168.10.142 value: ok
节点状态, ip: 192.168.10.170 value: ok
节点状态, ip: 192.168.10.168 value: ok

查看aof是否开启

#!/usr/bin/env python
# coding: utf-8 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='')
return redisconn
except Exception as e:
print(e)
print("错误,连接redis 集群失败")
return False def get_info(self):
"""
获取redis集群info信息
:return: dict
"""
res = RedisCluster(self.conn_list).connect()
# print("连接集群对象",res,type(res),res.__dict__)
if not res:
return False dic = res.cluster_info() # 查看info信息, 返回dict
if not dic:
return False return dic def get_state(self):
"""
获取状态
:return:
"""
dic = self.get_info() # type:dict
if not dic:
return dic for i in dic: # 遍历dict
ip = i.split(":")[0]
if dic[i].get('cluster_state'): # 获取状态
print("节点状态, ip: ", ip, "value: ", dic[i].get('cluster_state')) def get_has_aof(self):
"""
查看aof是否打开
:return:
"""
res = RedisCluster(self.conn_list).connect()
# print("连接集群对象",res,type(res),res.__dict__)
if not res:
return False dic = res.config_get('appendonly') # 从config配置项中查询appendonly for i in dic:
ip = i.split(":")[0]
# print(dic[i])
if dic[i].get('appendonly'):
print("aof开关, ip: ", ip,"value: ",dic[i].get('appendonly')) 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}] RedisCluster(redis_basis_conn).get_has_aof()

执行输出:

aof开关, ip:  192.168.10.170 value:  no
aof开关, ip: 192.168.10.168 value: no
aof开关, ip: 192.168.10.142 value: no
aof开关, ip: 192.168.10.171 value: no
aof开关, ip: 192.168.10.169 value: no
aof开关, ip: 192.168.10.143 value: no

set和get

#!/usr/bin/env python
# coding: utf-8 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='')
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')) # 查询值

执行输出:

name is:  b'admin'

注意:get出来的值,是bytes类型的。

其他redis操作,比如hget,hgetall... 和redis单例模式,是一样的。

这里就不一一演示了

python 操作redis集群的更多相关文章

  1. python操作redis集群

    strictRedis对象方法用于连接redis 指定主机地址,port与服务器连接,默认db是0,redis默认数据库有16个,在配置文件中指定database 16 上代码 .对redis的单实例 ...

  2. 15.9,python操作redis集群

      上代码 .对redis的单实例进行连接操作 python3 >>>import redis >>>r = redis.StrictRedis(host=, db ...

  3. java操作redis集群配置[可配置密码]和工具类(比较好用)

    转: java操作redis集群配置[可配置密码]和工具类 java操作redis集群配置[可配置密码]和工具类     <dependency>   <groupId>red ...

  4. java操作redis集群配置[可配置密码]和工具类

    java操作redis集群配置[可配置密码]和工具类     <dependency>   <groupId>redis.clients</groupId>   & ...

  5. php操作redis集群哨兵模式

    前段时间项目里正好用到了redis的集群哨兵部署,因为此前并无了解过,所以一脸懵逼啊,查阅了几篇资料,特此综合总结一下,作为记录. 写在前沿:随着项目的扩张,对redis的依赖也越来越大,为了增强re ...

  6. Java操作 Redis 集群

    // 连接redis集群 @Test public void testJedisCluster() { JedisPoolConfig config = new JedisPoolConfig(); ...

  7. JedisCluster操作redis集群

    1.pom引入依赖 <dependency> <groupId>redis.clients</groupId> <artifactId>jedis< ...

  8. python 搭建redis集群

    所需依赖 redis.io/download">redis-3.0.7ruby-1.8.7:sudo apt-get install rubyrubygems:sudo apt-get ...

  9. JedisCluster操作redis集群demo

    package com.chenk; import java.util.HashMap; import java.util.HashSet; import java.util.List; import ...

随机推荐

  1. P2388 阶乘之乘

    首先感谢wxy学长之前告诉我这道题,结果今天竟然一眼切了,咕咕咕 题目链接: P2388 阶乘之乘 题目思路: 第一眼看到一定想到的是先求一下阶乘然后看最后又几个零,但是这样会TIL啊 想一下0是怎么 ...

  2. 洛谷P2744 量取牛奶

    题目 DP或者迭代加深搜索,比较考验递归的搜索. 题目第一问可以用迭代加深搜索限制层数. 第二问需要满足字典序最小,所以我们可以在搜索的时候把比当前答案字典序大的情况剪枝掉. 然后考虑怎么搜索,对于每 ...

  3. Android入门教程(五)

    关注我,每天都有优质技术文章推送,工作,学习累了的时候放松一下自己. 欢迎大家关注我的微信公众号:「醉翁猫咪」 字面量: 1.整数字面量为整型(int) 2.小数字面量为双精度浮点型(double) ...

  4. 【2019.11.27】SDN上机第5次作业

    参考资料: https://www.cnblogs.com/zzqsss/p/11924685.html 问答环节 描述官方教程实现了一个什么样的交换机功能? Ryu是一个基于组件的软件定义的网络框架 ...

  5. compile install deploy;

    如果compile的话,也会打包在target里面: 如果有问题的话就找到本地仓库把它删掉: /Users/yinfuqing/.m2/repository/com/sankuai/qcs/qcs-r ...

  6. 共线性图 | Alluvial Diagrams | Parallel plot | Parallel Coordinates Plot

    最近有个需求需要画如下的图: 这些图的核心意思是一样的,就是connection,把不同的数据连到一起. 文章里把这图叫做共线性图,是按功能命名的,Google里搜不到. 搜到类似的,这个图叫 Par ...

  7. CORS-跨域问题:Access-Control-Allow-Origin Header and the ASP.NET Web API

    代码控制跨域: 如何使用:在 Global.asax 对应的控制类中: protected void Application_BeginRequest() { if (CorsFilter.IsOpt ...

  8. 环境变量path的值大于1024的解决办法

    原文传送门:https://blog.csdn.net/jytxj111/article/details/43916421 1.打开Path,点击默认文本(WIN 10),将所有路径备份下来 2.新建 ...

  9. Mysql按日、周、月进行分组统计

    我们在用 Mysql 制作数据可视化图表时候,经常需要按照天.周.月等不同的粒度对数据进行分组统计.而我们的时间可能是 “2017/12/5 0:0:0” 这种准确的时间. 所以在进行分组之前我们需要 ...

  10. jquery click 与原生 click 的区别

    $.click() 触发的事件中没有 event.originalEvent , 不同触发 href="" 中的内容 $[0].click() 可以 <script type ...