消费峰值不对齐问题

原队列峰值约为 15000 qps,现在调整后,单队列峰值约为 80 qps,远低于原队列,因此需要分析问题原因。

在这里插入图片描述

2、解决方案

  1. 队列配置:

    • Durable:对于不需要持久化消息的场景,可将消息持久性设置为 Transient
    • Auto-delete:对于不需要持久化队列的场景,可将队列自动删除属性设置为 True
  2. 更换消费库:

    • 更换消费库,如使用 Pika 代替 RabbitMQ-C,以排除是否是消费库引起的性能问题。
  3. 消费者数量:

    • 增加消费者数量,以提高处理消息的速度。
  4. 均衡负载:

    • 使用多个消费者来均衡负载,以避免单点故障。
  5. 预取消息:

    • 调整每个消费者的预取消息数量,以提高吞吐量。
  6. 多节点部署:

    • 在多台机器上部署 RabbitMQ,以增加吞吐量。
  7. 队列长度限制:

    • 调整队列长度限制,以防止队列过长导致性能下降。
  8. 消息大小:

    • 减少消息大小,以提高吞吐量。
  9. ACK机制:

    • 使用批量ACK机制,以减少网络开销。
  10. 性能监控:

    • 使用性能监控工具,以识别性能瓶颈。

代码例子

import pika

# 建立连接
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()

# 定义队列
queue_name = 'test_queue'
channel.queue_declare(queue=queue_name, durable=True)

# 定义回调函数
def callback(ch, method, properties, body):
    print(f"Received message: {body}")
    ch.basic_ack(delivery_tag=method.delivery_tag)

# 开始消费
channel.basic_consume(queue=queue_name, on_message_callback=callback)
channel.start_consuming()
#include <stdio.h>
#include <stdlib.h>
#include <amqp.h>
#include <amqp_framing.h>

int main(int argc, char **argv) {
    amqp_connection_state_t conn;

    // 建立连接
    conn = amqp_new_connection();
    if (!conn) {
        fprintf(stderr, "amqp_new_connection error: %s\n", amqp_error_string2(amqp_get_error(conn)));
        return 1;
    }

    // 设置连接参数
    amqp_socket_t *socket = amqp_tcp_socket_new(conn);
    if (!socket) {
        fprintf(stderr, "amqp_tcp_socket_new error: %s\n", amqp_error_string2(amqp_get_error(conn)));
        return 1;
    }
    amqp_socket_open(socket, "localhost", 5672);

    // 登录
    amqp_login(conn, "/", 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, "guest", "guest");

    // 创建信道
    amqp_channel_t channel = 1;
    amqp_channel_open(conn, channel);

    // 定义队列
    amqp_queue_declare_ok_t *r = amqp_queue_declare(conn, channel, amqp_cstring_bytes("test_queue"), 0, 0, 0, 0, amqp_empty_table);
    if (r->reply_code != 200) {
        fprintf(stderr, "amqp_queue_declare error: %s\n", amqp_error_string2(amqp_get_error(conn)));
        return 1;
    }

    // 开始消费
    amqp_basic_consume(conn, channel, amqp_cstring_bytes("test_queue"), amqp_empty_bytes, 0, 0, 0, amqp_empty_table);

    // 接收消息
    while (1) {
        amqp_frame_t frame;
        amqp_envelope_t envelope;

        amqp_simple_wait_frame(conn, &frame);
        amqp_decode_envelope(&envelope, &frame);

        if (AMQP_BASIC_DELIVER == frame.payload.deliver.name) {
            printf("Received message: %.*s\n", (int)frame.payload.deliver.message.len, (char *)frame.payload.deliver.message.bytes);
            amqp_basic_ack(conn, channel, envelope.delivery_tag, 0);
        }
    }

    // 关闭信道
    amqp_channel_close(conn, channel, AMQP_REPLY_SUCCESS);

    // 关闭连接
    amqp_connection_close(conn, AMQP_REPLY_SUCCESS);
    amqp_destroy_connection(conn);

    return 0;
}
  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值