zoukankan      html  css  js  c++  java
  • RabbitMQ持久化

    我们知道,如果消息接收端挂了,消息会保存在队列里。下次接收端启动就会接收到消息。

    如果RabbitMQ挂了怎么办呢?这时候需要将消息持久化到硬盘

    消息发送端:producer

    ...........
    # 建立管道
    channel = connection.channel()
    # 声明队列 1、加上durable=True进行队列持久化。两边都要加(当RabbitMQ服务down了之后)
    channel.queue_declare(queue="q1",durable=True)
    # 发消息
    channel.basic_publish(exchange='',
                          routing_key='q1',
                          body='everything is just beginning!',
                          # 3、加上这句话,对消息进行持久化,只需要在发送方写(RabbitMQ服务down了之后)
                          properties=pika.BasicProperties(delivery_mode=2))
    .......

    消息接收端:consumer

    .........
    # 建管道
    channel = connection.channel()
    # 声明队列  2、加上durable=True进行队列持久化
    channel.queue_declare(queue='q1', durable=True)
    
    def callback(ch, method, properties, body):
        print("--->:",ch,properties)
        time.sleep(10)
        print("received: ", body)
        ch.basic_ack(delivery_tag=method.delivery_tag)
    ............

    这样改动,当RabbitMQ挂了之后可以对消息队列和里面的消息进行持久化。

  • 相关阅读:
    瀑布流事件
    js 面向对象 模拟日历
    leetcode 戳气球
    leetcode 地下城游戏
    laravel服务容器
    lru缓存策略
    php实现7种常见排序
    curl请求中http头的几种格式
    wireshark过滤规则(两年前记录在qq空间的日志)
    screen和nohub及&用法
  • 原文地址:https://www.cnblogs.com/staff/p/9927850.html
Copyright © 2011-2022 走看看