python celery 中 redis集群sentinel 使用 《实践 踩坑》

本文介绍了如何在Python应用中使用Redis Sentinel集群,包括Scrapy与Celery两个场景。通过Sentinel进行高可用配置,确保数据的稳定存储和任务的可靠执行。详细配置代码已给出。
Python3.8

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

https://github.com/leffss/ScrapyRedisBloomFilterBlockCluster

https://pypi.org/project/scrapy-redis-sentinel/   scrapy  使用redis 哨兵

import redis
from redis.sentinel import Sentinel

python 中使用  redis哨兵
class RedisClient(object):
    '''初始化Redis数据库'''
    def __init__(self,TYPE, host=REDIS_HOST, port=REDIS_PORT, password=REDIS_PASSWORD,db=0):
       # self.db = redis.StrictRedis(host=host, port=port, password=password, decode_responses=True, db=db)
        self.db = Sentinel([('172.20.0.252', 26079), ('172.20.0.252', 26080),('172.20.0.252', 26081)], socket_timeout=0.5)
        if TYPE=='write':
            self.db = self.db.master_for('local-master', socket_timeout=0.5,password='redis' ,db=db)
        if TYPE=='read':
            self.db = self.db.slave_for('local-master', socket_timeout=0.5, password='redis',db=db)
 

celery 中使用  redis哨兵

# _*_ coding:utf-8 _*_
_
import redis
from redis.sentinel import Sentinel

from celery import Celery

# celery_app = Celery("ApiInterface", broker='redis://root:123@172.20.0.252:6079/9')   #单机版
# CELERYD_OPTS = "--time-limit=7200 -E --loglevel=DEBUG"
celery_app=Celery("app")     #集群哨兵版
celery_app.conf.broker_url = 'sentinel://root:redis@localhost:26079;sentinel://root:redis@localhost:26080;sentinel://root:redis@localhost:26081'

celery_app.conf.broker_transport_options = { 'master_name': "local-master" }

 

您可能感兴趣的与本文相关的镜像

Python3.8

Python3.8

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值