python 操作redis集群

一、连接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='123456')return redisconnexcept 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集群成功”)

View Code

执行输出:

连接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='123456')return redisconnexcept Exception as e:print(e)print("错误,连接redis 集群失败")return Falsedef get\_state(self):"""获取状态:return:"""res \= RedisCluster(self.conn\_list).connect()# print("连接集群对象",res,type(res),res.\_\_dict\_\_)if not res:return Falsedic \= res.cluster\_info()  # 查看info信息, 返回dictfor i in dic:  # 遍历dictip = 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()

View Code

执行输出:

节点状态, 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='123456')return redisconnexcept Exception as e:print(e)print("错误,连接redis 集群失败")return Falsedef get\_info(self):"""获取redis集群info信息:return: dict"""res \= RedisCluster(self.conn\_list).connect()# print("连接集群对象",res,type(res),res.\_\_dict\_\_)if not res:return Falsedic \= res.cluster\_info()  # 查看info信息, 返回dictif not dic:return Falsereturn dicdef get\_state(self):"""获取状态:return:"""dic \= self.get\_info()  # type:dictif not dic:return dicfor i in dic:  # 遍历dictip = 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 Falsedic \= res.config\_get('appendonly')  # 从config配置项中查询appendonlyfor 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()

View Code

执行输出:

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='123456')return redisconnexcept 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’)) # 查询值

View Code

执行输出:

name is: b’admin’

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

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

这里就不一一演示了

posted @ 2019-05-23 11:46 肖祥 阅读( …) 评论( …) 编辑 收藏

)

python 操作redis集群相关推荐

  1. python操作redis集群_python 连接管理作redis集群

    python的redis库是不支持集群操作的,推荐库:redis-py-cluster. 安装 pip3 install redis-py-cluster 连接redis集群 #!/usr/bin/e ...

  2. python操作redis集群_python操作redis集群

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

  3. Redis数据库搭建集群(集群概念、redis集群、搭建集群(配置机器1、2、创建集群、数据操作验证)、Python与redis集群交互)

    1. 集群的概念 集群是一组相互独立的.通过高速网络互联的计算机,它们构成了一个组,并以单一系统的模式加以管理.一个客户与集群相互作用时,集群像是一个独立的服务器.集群配置是用于提高可用性和可缩放性. ...

  4. python连接redis集群如何释放内存_python 连接 redis cluster 集群

    一. redis集群模式有多种, cluster模式只是其中的一种实现方式, 其原理请自行谷歌或者百度, 这里只举例如何使用Python操作 redis cluster 集群 二. python 连接 ...

  5. 代码操作redis集群报错:(error) MOVED 解决方法

    记录一下今天搭建完本地redis集群以后,使用C++代码测试redis集群搭建是否成功. 在初始化.链接等一系列成功后,我开开心心进行写操作: 这时候报错: Run 382 Redis Set Err ...

  6. 使用predis操作redis集群

    先搭建好redis集群,我的搭建好了,给你们看下 我的是3主3从. 接下来我们用predis来测试集群.predis是php生态里面的一个composer包 <?phprequire __DIR ...

  7. python连接redis集群如何释放内存_python 连接redis集群

    连接单个节点 单个的直接导入redis模块,设置ip,密码,端口,直接连就OK了 #python 2.7.x #redis (2.10.6) import redis def connRedis(): ...

  8. Python与redis集群进行交互操作

    安装包如下 pip install redis-py-cluster redis-py-cluster源码地址https://github.com/Grokzen/redis-py-cluster 创 ...

  9. 使用Python连接Redis集群

    1.使用pip search查看可安装的Redis模块版本 PS D:\code>pip install pip-search #安装pip-search模块 PS D:\code> pi ...

最新文章

  1. 百度搜索技巧语法大全
  2. 动态导航与动态路由绑定
  3. Python的系统管理_12_rrdtool
  4. C++ Ouput Exactly 2 Digits After Decimal Point 小数点后保留三位数字
  5. pymysql.err.InternalError: (1054, Unknown column '27D24A3B' in 'where clause')之错误解决
  6. 【大型网站运维之道 天道 人道 运维之道】
  7. 141.3. 单机安装 CentOS 5 + hadoop-0.20.0
  8. Linux客户机上安装VMware tools工具方法
  9. 研磨设计模式学习笔记1--简单工厂(SimpleFactory)
  10. 3D旋转相册代码及详细使用教程
  11. 3dmax su 简单_3DMAX转SU逆天神器!一键转换,无脑操作,你值得拥有!
  12. Python-Data-Science-Toolbox-Part-1
  13. 五子棋java毕业设计论文_Java五子棋毕业设计论文
  14. 英文邮件寻求帮助的礼貌用语
  15. web开发中移动端适配
  16. 蓝鸽服务器崩溃怎样从装系统,系统崩溃如何重装系统?
  17. Jenkin权限控制——基于角色授权策略
  18. 【嵌入式C语言】内存分配 malloc 和 free
  19. 期权基础篇 | 什么是期权
  20. 回到过去看未来(1)

热门文章

  1. Hadoop入门案例WordCount
  2. WLAN AP安全策略中WPA认证与WPA2认证的差异
  3. 寒假每日一题——贝茜的报复
  4. TC2.0库函数清单
  5. mysql存储过程中光标的使用
  6. win10服务器权限修改时间,大师为你解答win10系统时间无法修改没有权限的处理方案...
  7. Android10.0 Binder通信原理(十)-AIDL原理分析-Proxy-Stub设计模式
  8. 嵌入式主板Linux的adb命令adb有线调试使用说明
  9. 使用jsoup入门java爬虫 案例
  10. java 单链表一元多项式_java单链表实现一元多项式加法和乘法运算