导入配置
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId><version>2.7.7</version></dependency>
配置文件配置
JavaConfig配置
@Component@Slf4jpublicclassRabbitmqConfigimplementsInitializingBean{@ResourceprivateRabbitTemplate rabbitTemplate;@OverridepublicvoidafterPropertiesSet()throwsException{
log.info("初始化配置rabbitmq配置");// rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter(new ObjectMapper()));
rabbitTemplate.setConfirmCallback(newRabbitTemplate.ConfirmCallback(){@Overridepublicvoidconfirm(CorrelationData correlationData,boolean b,String s){if(!b){
log.error("发送消息到mq失败,原因:{}",s);}}});
rabbitTemplate.setReturnCallback(newRabbitTemplate.ReturnCallback(){@OverridepublicvoidreturnedMessage(Message message,int replyCode,String replyText,String exchange,String routingKey){
log.error("消息返回回调触发,交换机:{},路由:{},消息内容:{},原因:{}",exchange,routingKey,message,replyText);}});}}
通过@Bean注入MessageConverter,保证消息的正确传输
@BeanpublicMessageConverterjsonMessageConverter(ObjectMapper objectMapper){returnnewJackson2JsonMessageConverter(objectMapper);}
修改后的代码
@Override//关于事务注解publicResultseckillVoucher(Long voucherId){// 1,执行lua脚本,lua脚本用于判断库存是否充足,扣库存操作Long userId=UserHolder.getUser().getId();long orderId=redisIDworker.nextId("order");//执行此操作前保证,用户查看优惠券的请求,和添加优惠券时 已经将秒杀优惠圈的库存数量添加到了redis中Long result=stringRedisTemplate.execute(SECKILL_SCRIPT,Collections.emptyList(),
voucherId.toString(),
userId.toString(),String.valueOf(orderId));//获取lua脚本返回值int r=result.intValue();if(r!=0){returnResult.fail(r==1?"库存不足":"不能重复下单");}// //获取代理对象// IVoucherOrderService proxy= (IVoucherOrderService) AopContext.currentProxy();//异步写数据库VoucherOrder voucherOrder =newVoucherOrder();
voucherOrder.setId(orderId);
voucherOrder.setUserId(userId);
voucherOrder.setVoucherId(voucherId);
rabbitTemplate.convertAndSend(SaveVoucherConstants.SECKILL_VOUCHER_SAVE_QUEUE,voucherOrder);
log.info("发送保存秒杀券订单信息成功:{}",orderId);returnResult.ok("seckSuccess");}
监听器
@Component@Slf4jpublicclassAsyncSaveVoucherListener{@ResourceprivateIVoucherOrderService voucherOrderService;@ResourceprivateISeckillVoucherService seckillVoucherService;@RabbitListener(queuesToDeclare ={@Queue(name=SaveVoucherConstants.SECKILL_VOUCHER_SAVE_QUEUE)})publicvoidAsyncSave(VoucherOrder voucherOrder){
log.info("接收到存储订单信息的消息,{}",JSON.toJSON(voucherOrder).toString());boolean success = seckillVoucherService.update().setSql("stock=stock-1").eq("voucher_id", voucherOrder.getVoucherId()).gt("stock",0).update();
voucherOrderService.save(voucherOrder);
log.info("订单信息存储完成?{}",success);}}
本文转载自: https://blog.csdn.net/qq_51050526/article/details/134448473
版权归原作者 Script kiddies 所有, 如有侵权,请联系我们删除。
版权归原作者 Script kiddies 所有, 如有侵权,请联系我们删除。