python操作Redis集群
2023-09-11 14:14:26 时间
集群介绍
https://www.cnblogs.com/theboy/p/10690838.html
方法一、第三方包实现
参考文档:
https://redis-py-cluster.readthedocs.io/en/master/
参考rep:
https://github.com/Grokzen/redis-py-cluster
>>> from rediscluster import RedisCluster
>>> # Requires at least one node for cluster discovery. Multiple nodes is recommended.
>>> startup_nodes = [{"host": "127.0.0.1", "port": "7000"}, {"host": "127.0.0.1", "port": "7001"}]
>>> rc = RedisCluster(startup_nodes=startup_nodes, decode_responses=True)
# Or you can use the simpler format of providing one node same way as with a Redis() instance
<<< rc = RedisCluster(host="127.0.0.1", port=7000, decode_responses=True)
>>> rc.set("foo", "bar")
True
>>> print(rc.get("foo"))
'bar'
方法二、自带官方包已经支持
参考repo:https://github.com/andymccurdy/redis-py
参考文档:https://redis.io/topics/cluster-tutorial
Using 'host' and 'port' arguments:
>>> from redis.cluster import RedisCluster as Redis
>>> rc = Redis(host='localhost', port=6379)
>>> print(rc.get_nodes())
[[host=127.0.0.1,port=6379,name=127.0.0.1:6379,server_type=primary,redis_connection=Redis<ConnectionPool<Connection<host=127.0.0.1,port=6379,db=0>>>], [host=127.0.0.1,port=6378,name=127.0.0.1:6378,server_type=primary,redis_connection=Redis<ConnectionPool<Connection<host=127.0.0.1,port=6378,db=0>>>], [host=127.0.0.1,port=6377,name=127.0.0.1:6377,server_type=replica,redis_connection=Redis<ConnectionPool<Connection<host=127.0.0.1,port=6377,db=0>>>]]
Using the Redis URL specification:
>>> from redis.cluster import RedisCluster as Redis
>>> rc = Redis.from_url("redis://localhost:6379/0")
Directly, via the ClusterNode class:
>>> from redis.cluster import RedisCluster as Redis
>>> from redis.cluster import ClusterNode
>>> nodes = [ClusterNode('localhost', 6379), ClusterNode('localhost', 6378)]
>>> rc = Redis(startup_nodes=nodes)
相关文章
- redis实战笔记(3)-第3章 Redis命令
- Python脚本写端口扫描器(socket,python-nmap)
- Python中数据类型的转换
- How to run python interactive in current file's directory in Visual Studio Code? Python路径问题
- Python--Matplotlib库与数据可视化③--Figures、Axes对象与多图绘制
- Python 字符串、时间、日期、时间戳的相互转换(时间戳转换)
- 【零基础学python】:清华官方出品的《看漫画学Python》全彩PDF,495页资源分享
- 为什么 Python 会如此火爆?Python都可以做哪些副业?
- Redis——Lettuce连接redis集群
- 【Python】【PyPI】twine模块打包python项目上传pypi
- Python 导入数据(Pandas)
- Redis超详细入门(图文结合)原来redis这么简单
- Linux下安装OpenCV+Python支持
- python字符串(string)方法整理
- Nvidia GPU 入门教程之 04 如何在 Ubuntu 上安装 Anaconda Python 发行版
- Golang 实现 Redis(6): 实现 pipeline 模式的 redis 客户端
- Python 常见问题 之 python 安装包下载安装速度慢 的 快速解决方法(之一)
- Python 基础 之 python 进程知识点整理,实现一个简单使用进程池的多进程文件夹文件copy器
- 【数据结构】:树的先序,中序,后序遍历Python实现
- Redis(1.17)redis客户端管理
- Golang中通过go-redis操作Redis
- 【爬虫系列】【Python】python with as的用法
- 曹工说Redis源码(7)-- redis server 的周期执行任务,到底要做些啥
- 曹工说Redis源码(6)-- redis server 主循环大体流程解析
- 【Linux学习笔记】解决:yum install python-dev 报错(CentOS安装Python-dev库)
- python的第一副图
- python读取某一个文件夹下的所有图片