如何保证业务是幂等的?
方案一:
生产者、消费者都配置上 setCreateMessageIds(true)。配好后,生产者给MQ发消息,消息就会带上自动生成的 id。将来消费者拿到消息后,就可以获取到 id,把 id保存起来 做唯一匹配
代码示例:
package com.itheima;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class ErpApplication {
public static void main(String[] args) {
SpringApplication.run(ErpApplication.class, args);
}
@Bean
public MessageConverter jacksonMessageConvertor() {
Jackson2JsonMessageConverter jjmc = new Jackson2JsonMessageConverter();
jjmc.setCreateMessageIds(true);
return jjmc;
}
}
方案二:
标签:可靠性,消费者,Jackson2JsonMessageConverter,springframework,jjmc,RabbitMQ,org,import,id From: https://www.cnblogs.com/gagaya2/p/17874815.html