烧瓶的redis哨兵集成
Flask-Redis-Sentinel的Python项目详细描述
Flask Redis Sentinel支持使用Sentinel连接到Redis,也支持连接到Redis 没有它。
- 支持Python2.7和3.3+
- 使用apache license 2.0授权
安装
使用pip安装:
pip install Flask-Redis-Sentinel
基本用法
fromflask_redis_sentinelimportSentinelExtensionredis_sentinel=SentinelExtension()redis_connection=redis_sentinel.default_connection# Later when you create applicationapp=Flask(...)redis_sentinel.init_app(app)
可以使用redis+sentinelflask配置变量配置redis连接参数 URL方案:
redis+sentinel://localhost:26379[,otherhost:26379,...]/mymaster/0 redis+sentinel://localhost:26379[,otherhost:26379,...]/mymaster/0?socket_timeout=0.1 redis+sentinel://localhost:26379[,otherhost:26379,...]/mymaster/0?sentinel_socket_timeout=0.1 redis+sentinel://:sentinel-secret-password@localhost:26379[,otherhost:26379,...]/mymaster/0?sentinel_socket_timeout=0.1
该扩展还支持redis py支持的url方案,用于直接连接到实例而无需sentinel:
redis://[:password]@localhost:6379/0 rediss://[:password]@localhost:6379/0 unix://[:password]@/path/to/socket.sock?db=0
也支持flask和redis样式的配置变量,以便于迁移,但是扩展将 记录不推荐警告:
REDIS_HOST = 'localhost' REDIS_PORT = 6379 REDIS_DB = 0
如果同时存在redis_url和其他变量,则使用url。
使用单个sentinel群集创建多个连接池
fromflask_redis_sentinelimportSentinelExtensionredis_sentinel=SentinelExtension()master1=redis_sentinel.master_for('service1')master2=redis_sentinel.master_for('service2')slave1=redis_sentinel.slave_for('service1')
访问redis py的sentinel实例
fromflask_redis_sentinelimportSentinelExtensionfromflaskimportjsonify,Flaskapp=Flask('test')redis_sentinel=SentinelExtension(app=app)@app.route('/')defindex():slaves=redis_sentinel.sentinel.discover_slaves('service1')returnjsonify(slaves=slaves)
更改日志
v2.0.0
- 连接现在是线程本地的,以避免redis主故障转移后出现争用情况
- 删除了对redis{host,port,db}配置变量的支持
v1.0.0
将URL处理代码移动到单独的库中,redis\u sentinel\u url
向后不兼容更改:
# Old redis+sentinel://host:port/service?slave=true
现在应该写为:
# New redis+sentinel://host:port/service?client_type=slave
v0.2.0
- 使用配置变量而不是redis{host,port,db}即使使用了redis url
- 小重构
v0.1.0
- 初始版本