使用Python连接Redis集群

程鸿煊
2023-12-01

1、使用pip search查看可安装的Redis模块版本

PS D:\code>pip install pip-search #安装pip-search模块
PS D:\code> pip_search redis #利用pip-search查找模块可安装的版本信息
                                                                          https://pypi.org/search/?q=redis 
┏━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Package              ┃ Version  ┃ Released     ┃ Description                                                                                                                         ┃
┡━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━╇━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│  redis2            │ 2.10.6.3 │ Jan 1, 2022  │ Python client for Redis key-value store                                                                                             │
│  redis3            │ 3.5.2.3  │ Jan 1, 2022  │ Python client for Redis key-value store                                                                                             │
│  redis             │ 4.1.2 == │ Jan 27, 2022 │ Python client for Redis database and key-value store                                                                                │
│  django-redis      │ 5.2.0    │ Dec 22, 2021 │ Full featured redis cache backend for Django.                                                                                       │
│  redis-ipc         │ 0.0.1    │ Jun 27, 2021 │ A minimal multi producer single consumer IPC using redis pub/sub                                                                    │
│  bottle-redis      │ 0.2.3    │ Jun 3, 2015  │ Redis integration for Bottle.                                                                                                       │
│  rivescript-redis  │ 0.1.0    │ Feb 21, 2017 │ Redis driver for RiveScript                                                                                                         │
│  flypper-redis     │ 0.1.2    │ Jan 23, 2022 │ Feature flags, with a GUI - Redis backend                                                                                           │
│  JCoder-redis      │ 0.0.5    │ May 1, 2021  │ This Redis is a Python library based on redis.                                                                                      │
│  openid-redis      │ 1.1      │ Mar 3, 2014  │ A Redis storage backend for the python-openid package                                                                               │
│  serialized-redis  │ 0.1.5    │ Oct 12, 2014 │ A solution to have lists and dictionaries serialized using redis-py.                                                                │
│  redis-trib        │ 0.6.2    │ Nov 1, 2019  │ Redis Cluster tools in Python                                                                                                       │
│  redis_proxy       │ 0.0.2    │ Sep 11, 2013 │ a redis proxy that will seperate read and write                                                                                     │
│  redis-index       │ 0.1.11   │ Nov 8, 2019  │ Inverted Index using efficient Redis set                                                                                            │
│  redis-tornado     │ 0.2      │ Aug 28, 2012 │ Async redis client built on the Tornado IOLoop.                                                                                     │
│  sdh.redis         │ 0.1.0    │ Oct 31, 2020 │ Django redis wrapper                                                                                                                │
│  redis-collections │ 0.11.0   │ Jan 19, 2022 │ Set of basic Python collections backed by Redis.                                                                                    │
│  restful-redis     │ 0.0.2    │ Feb 24, 2018 │ Redis queues for synchronous server2server ops                                                                                      │
│  redis-persistence │ 0.0.4    │ Jan 5, 2021  │ A python module to make your Telegram bot persistent using Redis                                                                    │
│  cp-redis          │ 0.0.1    │ Jan 2, 2020  │ This is Carpool Project subpackage: cp cp-redis                                                                                     │
│  disposable-redis  │ 0.2      │ Aug 4, 2015  │ UNKNOWN                                                                                                                             │
│  stati-redis       │ 0.0.7    │ May 12, 2013 │ Python client with redis transport for GottWall statistics aggregator                                                               │
│  exec-redis        │ 0.0.10   │ Dec 2, 2021  │ Redis handler.                                                                                                                      │
│  redis_triggers    │ 0.1      │ Jul 30, 2016 │ Redis Key events that executes a function written by you such as on expiry                                                          │
│  namekox-redis     │ 0.0.26   │ Apr 29, 2021 │ namekox redis                                                                                                                       │
│  Mask-Redis        │ 1.0.0a1  │ Jun 29, 2021 │ Redis extension for Mask.                                                                                                           │
│  redis-pal         │ 0.1.4    │ Jun 15, 2021 │ Store things in Redis without worrying about types or anything, just do it!                                                         │
│  redis-schematics  │ 0.3.1    │ Feb 14, 2020 │ Redis storage backend for schematics.                                                                                               │
│  celerybeat-redis  │ 0.1.5    │ Apr 4, 2016  │ A Celery Beat Scheduler that uses Redis to store both schedule definitions and status information                                   │
│  redis-funnel      │ 0.0.1    │ Mar 26, 2019 │ A distributed funnel system based on redis, management system included.                                                             │
│  redis-cirrus      │ 2.10.5   │ Apr 20, 2016 │ Python client for Redis key-value store                                                                                             │
│  redis-sentinel    │ 1.0.1    │ Sep 11, 2020 │ Redis connection fully managed by sentinel                                                                                          │
│  redis-completion  │ 0.5.0    │ Apr 20, 2013 │ autocomplete with redis                                                                                                             │
│  redis_drs         │ 0.0.3    │ Mar 4, 2015  │ A Distributed Resource Scheduling system that uses Redis.                                                                           │
│  kinto-redis       │ 2.1.0    │ Nov 16, 2021 │ Kinto Redis                                                                                                                         │
│  redis-memslider   │ 0.1.2    │ Feb 29, 2016 │ Gradually reduce redis maxmemory.REDIS (redis.io) will block while ensuring maxmemory is honored, so when reducing it               │
│                      │          │              │ significantlyit is helpful to do so over a period of time in smaller steps if necessary. For example an AWS EC2m2.2xlarge instance  │
│                      │          │              │ takes in the vicinity of 1-5 seconds to evict 256MB of keys.                                                                        │
│  redis-relay       │ 0.0.1    │ Jul 25, 2018 │ redis relay for interprocess communication                                                                                          │
│  py-redis          │ 1.1.1    │ Dec 13, 2021 │ A convenience wrapper for the official Python redis package                                                                         │
│  joker-redis       │ 0.0.3    │ Jan 21, 2022 │ redis based utilities                                                                                                               │
│  redis_tryton      │ 0.4      │ Mar 5, 2018  │ Redis Cache for the Tryton Framework                                                                                                │
└──────────────────────┴──────────┴──────────────┴─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
PS D:\code> 

2、利用pip安装指定版本的Redis模块

PS D:\code> pip install redis==4.1.2           #安装指定版本的Redis                                                                                                                                         Requirement already satisfied: redis==4.1.2 in d:\appsoft\python\python310\lib\site-packages (4.1.2)
Requirement already satisfied: packaging>=20.4 in d:\appsoft\python\python310\lib\site-packages (from redis==4.1.2) (21.3)
Requirement already satisfied: deprecated>=1.2.3 in d:\appsoft\python\python310\lib\site-packages (from redis==4.1.2) (1.2.13)
Requirement already satisfied: wrapt<2,>=1.10 in d:\appsoft\python\python310\lib\site-packages (from deprecated>=1.2.3->redis==4.1.2) (1.13.3)
Requirement already satisfied: pyparsing!=3.0.5,>=2.0.2 in d:\appsoft\python\python310\lib\site-packages (from packaging>=20.4->redis==4.1.2) (3.0.7)
PS D:\code> 

3、利用redis模块连接Redis集群

import logging
from redis import RedisCluster

logging.basicConfig()
logger=logging.getLogger('redis')
logger.setLevel(logging.DEBUG)
logger.propagate=True

r=RedisCluster(host='192.168.102.30',port=7001,password='Aa1111')

print(r.get('test:2')) #nil
print(r.get('BB'))   #33
print(r.set('sdlk2','asfdkl'))
print(r.get('sdlk2'))

运行之后输出信息如下:

 PS D:\code>  d:; cd 'd:\code'; & 'D:\appsoft\Python\Python310\python.exe' 'c:\Users\27178\.vscode\extensions\ms-python.python-2021.12.1559732655\pythonFiles\lib\python\debugpy\launcher' '61541' '--' 'd:\code\redistest.py' INFO:redis.cluster:Creating a new instance of RedisCluster clientDEBUG:redis.cluster:startup_nodes : [[host=192.168.102.30,port=7001,name=192.168.102.30:7001,server_type=None,redis_connection=None]]DEBUG:redis.cluster:Initializing the nodes' topology of the clusterDEBUG:redis.cluster:Target node/s for COMMAND: default-nodeDEBUG:redis.cluster:Executing command COMMAND on target node: primary 192.168.102.31:7002DEBUG:redis.cluster:Target for ('GET', 'test:2'): slot 6296DEBUG:redis.cluster:Executing command GET on target node: primary 192.168.102.31:7001NoneDEBUG:redis.cluster:Target for ('GET', 'BB'): slot 808DEBUG:redis.cluster:Executing command GET on target node: primary 192.168.102.31:7002b'33'DEBUG:redis.cluster:Target for ('SET', 'sdlk2', 'asfdkl'): slot 4028DEBUG:redis.cluster:Executing command SET on target node: primary 192.168.102.31:7002TrueDEBUG:redis.cluster:Target for ('GET', 'sdlk2'): slot 4028DEBUG:redis.cluster:Executing command GET on target node: primary 192.168.102.31:7002b'asfdkl'PS D:\code> 

参考文档:

https://github.com/scredis/scredis/issues/50

scredis {
  redis {
    # Redis server address
    host = "hostname"
    # Redis server port
    #port = "7387"
    password = "redispassword"
    # List of hosts or host:port tuples, which serve as seed nodes for a Redis cluster connection.
    # If port is missing, the configured port is used as default.
    #cluster-nodes = [${scredis.redis.host}":"${scredis.redis.port}]
    cluster-nodes = ["hostname1:7385","hostname2:7386","hostname3:7387"]
    #cluster-nodes = ${?REDIS_CLUSTER_NODES}
  }
}

https://aws.amazon.com/blogs/opensource/new-cluster-mode-support-in-redis-py/

https://docs.redis.com/latest/rs/references/client_references/client_python/

 类似资料: