RabbitMQ和kafka对于消费失败处理总结
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RabbitMQ和kafka对于消费失败处理总结相关的知识,希望对你有一定的参考价值。
参考技术A 参考: https://www.jianshu.com/p/7a6deaba34d2参考: https://my.oschina.net/dengfuwei/blog/1595047
失败重发参考: https://www.cnblogs.com/xujishou/p/6288623.html
confirm模式 重发消息,生成CorrelationData,重新发送
发送消息时,消息发送成功,业务失败。业务成功消息发送失败?
保证业务处理成功后发送消息,发送失败一直重试发送消息。
kafka: https://github.com/huangxiongbiao12/kafka.git
rabbitmq: https://github.com/huangxiongbiao12/rabbitmq-demo.git
rabbitmq消费端的nack和重回队列的总结
重回队列模式,是当投递消息失败时,让该消息重新回到队列的模式,该模式需要手动签收,并需要在消费者中进行判断,调用重回队列的确认模式
消费者
package com.flying.rabbitmq.api.ack; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory; public class Consumer public static void main(String[] args) throws Exception ConnectionFactory connectionFactory = new ConnectionFactory(); connectionFactory.setHost("127.0.0.1"); connectionFactory.setPort(5672); connectionFactory.setVirtualHost("/"); Connection connection = connectionFactory.newConnection(); Channel channel = connection.createChannel(); String exchangeName = "test_ack_exchange"; String queueName = "test_ack_queue"; String routingKey = "ack.#"; channel.exchangeDeclare(exchangeName, "topic", true, false, null); channel.queueDeclare(queueName, true, false, false, null); channel.queueBind(queueName, exchangeName, routingKey); // 手工签收 必须要关闭 autoAck = false channel.basicConsume(queueName, false, new MyConsumer(channel));
自定义消费者:
package com.flying.rabbitmq.api.ack; import com.rabbitmq.client.AMQP; import com.rabbitmq.client.Channel; import com.rabbitmq.client.DefaultConsumer; import com.rabbitmq.client.Envelope; import java.io.IOException; public class MyConsumer extends DefaultConsumer private Channel channel ; public MyConsumer(Channel channel) super(channel); this.channel = channel; @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException System.err.println("-----------consume message----------"); System.err.println("body: " + new String(body)); try Thread.sleep(2000); catch (InterruptedException e) e.printStackTrace(); if((Integer)properties.getHeaders().get("num") == 0) channel.basicNack(envelope.getDeliveryTag(), false, true); else channel.basicAck(envelope.getDeliveryTag(), false);
生产者:
package com.flying.rabbitmq.api.ack; import com.rabbitmq.client.AMQP; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory; import java.util.HashMap; import java.util.Map; public class Producer public static void main(String[] args) throws Exception ConnectionFactory connectionFactory = new ConnectionFactory(); connectionFactory.setHost("127.0.0.1"); connectionFactory.setPort(5672); connectionFactory.setVirtualHost("/"); Connection connection = connectionFactory.newConnection(); Channel channel = connection.createChannel(); String exchange = "test_ack_exchange"; String routingKey = "ack.save"; for(int i =0; i<5; i ++) Map<String, Object> headers = new HashMap<String, Object>(); headers.put("num", i); AMQP.BasicProperties properties = new AMQP.BasicProperties.Builder() .deliveryMode(2) .contentEncoding("UTF-8") .headers(headers) .build(); String msg = "Hello RabbitMQ ACK Message " + i; channel.basicPublish(exchange, routingKey, true, properties, msg.getBytes());
以上是关于RabbitMQ和kafka对于消费失败处理总结的主要内容,如果未能解决你的问题,请参考以下文章
Spring Cloud Stream消费失败后的处理策略:使用DLQ队列(RabbitMQ)