消息幂等性 为了防止重复消费 需要进行幂等性操作实现方法有很多 幂等表式其中一种解决方案 保存消息的uuid 如果不存在则代表没有重复消费 有就是重复消费

main
夜灬瞬 2 years ago
parent 8c327d2bd0
commit 9ffc84f5dc

@ -1,5 +1,7 @@
package com.shun.integral.controller; package com.shun.integral.controller;
import org.springframework.amqp.core.Message;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
@ -19,4 +21,31 @@ public class IntegralController {
Thread.sleep(400); Thread.sleep(400);
System.out.println("扣除用户积分成功!!"); System.out.println("扣除用户积分成功!!");
} }
private final String ID_NAME = "spring_returned_message_correlation";
// @Transactional
// public void consume(Message message) {
// // 获取生产者提供的CorrelationId要基于header去获取。
// String id = message.getMessageProperties().getHeader(ID_NAME);
// //1、查询幂等表是否存在当前消息标识
// int count = userPointsIdempotentMapper.findById(id);
// //2、如果存在直接return结束
// if(count == 1){
// log.info("消息已经被消费!!!无需重复消费!");
// return;
// }
// //3、如果不存在插入消息标识到幂等表
// userPointsIdempotentMapper.save(id);
// //4、执行消费逻辑
// // 预扣除用户积分
// try {
// Thread.sleep(400);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// System.out.println("扣除用户积分成功!!");
// }
} }

Loading…
Cancel
Save