springboot 使用rocketmq实现简单的TCC

商品微服务

商品扣减库存请求体

java 复制代码
@Data
public class GoodsTccRequest {

    private List<Integer> skuIds;//商品售卖id

    private List<Integer> quantitys;//数量

    private String orderId;//订单号
}

商品TCC service

java 复制代码
public interface GoodsTccService {

    /**
     * 订单feign调用扣减库存时使用
     * */
    Integer tryStock(GoodsTccRequest request);//生成冻结库存记录,商品库存=商品库存-冻结库存,冻结库存状态=try

    /**
     * 监听MQ调用该方法,修改冻结状态
     * */
    Integer commitStock(String orderId);//冻结库存状态=commit


    /**
     * 监听MQ调用该方法,把冻结的库存加上去
     * */
    Integer cancelStock(String orderId);//冻结库存状态=cancle,商品库存=商品库存+冻结库存
}

tcc相关的监听器

java 复制代码
@Slf4j
@Service
@RocketMQMessageListener(consumerGroup = "goods_service_stock", topic = "order_create",messageModel= CLUSTERING,consumeThreadMax = 1)//"goods_change"
public class GoodsTccStockListener implements RocketMQListener<String>{

    @Resource
    private GoodsTccService goodsTccService;


    /*
    *监听商品tcc,执行tcc commit或者cancel
    * **/
    @Override
    public void onMessage(String body) {
        log.info("商品库存监听MQ消费 body {} ",body);

        JSONObject jsonObject = JSON.parseObject(body);
        String orderId = jsonObject.getString("orderId");
        String result = jsonObject.getString("result");
        if(result.equals("commit")){
            goodsTccService.commitStock(orderId);
        }else  if(result.equals("cancel")){
            goodsTccService.cancelStock(orderId);
        }
    }
}

订单服务

商品微服务feign

java 复制代码
public interface GoodsTccFeign {

    /**
     * 订单feign调用扣减库存时使用
     * */
    @PostMapping("/goods/stock/reduce")
    Integer tryStock(GoodsTccRequest request);//生成冻结库存记录,商品库存=商品库存-冻结库存,冻结库存状态=try
}

mq发送,用于最终一致性,要么cancel,要么commit

java 复制代码
@Slf4j
@Component
public class MessageGoodsTccSendProducer {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    /**
     * 异步发送消息
     * @param orderId 订单id
     * @param result 请求结果 ("commit","cancel")
     */
    public void asyncSend(String orderId,String result){
        JSONObject obj = new JSONObject();
        obj.put("orderId",orderId);
        obj.put("result",result);
        Message<String> message = MessageBuilder.withPayload(obj.toJSONString()).build();
        // 异步发送消息
        rocketMQTemplate.asyncSend("order_create", message, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                log.info("消息发送Producer, message为:{}, 成功。{}", message.getPayload(), JSONObject.toJSONString(sendResult));
            }

            @Override
            public void onException(Throwable throwable) {
                log.error("消息发送Producer失败,messageId为:{}, 异常:{}", message.getPayload(), throwable.getMessage());
            }
        }, 5000, 0);
    }
}

下单controller

java 复制代码
@RestController
@RequestMapping("/order")
public class OrderCreateController {

    GoodsTccFeign goodsTccFeign;

    MessageGoodsTccSendProducer messageGoodsTccSendProducer;

    @RequestMapping("/create")
    public Map getCanGotoActivityTag(@RequestParam("skuIds") List<Integer> skuids, @RequestParam("quantitys")List<Integer> quantitys){
        GoodsTccRequest goodsTccRequest = new GoodsTccRequest();
        goodsTccRequest.setSkuIds(skuids);
        goodsTccRequest.setSkuIds(quantitys);
        String orderId = UUID.randomUUID().toString().replace("-","");
        goodsTccRequest.setOrderId(orderId);
        //冻结库存
        goodsTccFeign.tryStock(goodsTccRequest);

        //生成订单

        //如果一切正常
        String result = "commit";

        //如果异常
        //result = "cancel";

        //发送消息
        messageGoodsTccSendProducer.asyncSend(orderId,result);
        Map orderMap = new HashMap<>();
        return orderMap;
    }

}
相关推荐
ps酷教程6 小时前
Jackson 解决没有无参构造函数的反序列化问题
java
NiceCloud喜云6 小时前
Opus 4.8 的 Effort Control 怎么选:Low 到 Max 五档策略
android·java·大数据·前端·c++·python·spring
Dfreedom.7 小时前
Windows、虚拟机、开发板组网通信原理及调试通联步骤
人工智能·windows·部署·边缘计算·开发板·模型加速
AI玫瑰助手7 小时前
Python函数:默认参数的定义与注意事项
开发语言·python·信息可视化
油炸自行车7 小时前
Claude Code 错误:API Error: 400 Failed to deserialize the JSON body into the
开发语言·javascript·json·trae·claude code·api error 400
肩上风骋7 小时前
C++14特性
开发语言·c++·c++14特性
_日拱一卒7 小时前
LeetCode:994腐烂的橘子
java·数据结构·算法·leetcode·深度优先
隔窗听雨眠8 小时前
Nginx网关响应慢排查手记
java·服务器·nginx
智慧物业老杨8 小时前
智慧物业合同周期管理系统:从风险预警到智能交接的全流程数智化落地方案
java·人工智能·python
源码宝8 小时前
MES系统源码:Java8 + SpringBoot2.7 + MySQL8 + Redis,后端源码清爽易扩展
java·后端·源码·springboot·mes系统·源码二开·mes源码