现存代码问题分析
-
decreaseStock
方法被 @Transactional
标注,并且调用 decreaseStock
的方法 createOrder
也被 @Transactional
标注,根据 Spring 的事务传播机制,默认 decreaseStock
会沿用 createOrder
的事务,也就是说和 createOrder
的事务同时成功或同时失败;
- 原先
decreaseStock
代码是 MySQL 操作,意味着,如果 decreaseStock
之后的大事务中的代码报错,decreaseStock
中对 MySQL 的更改是可以回滚的;但是现在改用 Redis 和 MQ 之后,如果之后的大事务失败(比如订单入库失败、销量增加失败),对 Redis 的更改以及对 MQ 发出的消息和造成的 MySQL 的更改是无法恢复的,返回给用户的下单失败,但是库存就损失掉了,虽然不会造成超卖,但是会造成少卖,库存莫名其妙的少了,但是又找不到对应的订单,导致货物积压;
- 问题的本质其实是分布式事务的问题,RocketMQ 是提供了事务型消息的支持的;
@Override
@Transactional
public boolean decreaseStock(Integer itemId, Integer amount) {
// int affectedRows = itemStockDOMapper.decreaseStock(itemId, amount);
long result = redisTemplate.opsForValue().increment("promo_item_stock_" + itemId, amount * -1);
if (result >= 0) {
boolean mqResult = mqProducer.asyncReduceStock(itemId, amount);
if (!mqResult) {
redisTemplate.opsForValue().increment("promo_item_stock_" + itemId, amount);
return false;
}
return true;
} else {
redisTemplate.opsForValue().increment("promo_item_stock_" + itemId, amount);
return false;
}
}
小知识:Spring 提供le在事务 Commit 成功之后再做点事情的能力
- 如果
afterCommit
方法执行失败,那么事务中已经提交成功的数据是不能回滚的;
@Transactional
public void createOrder() {
// some operation in transaction
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
// 这个方法会在最近的一个 @Transactional 标签被成功 Commit 之后执行
@Override
public void afterCommit() {
super.afterCommit();
}
});
}
RocketMQ 的事务型消息
在 Producer 的封装中,增加发送事务型消息的方法
- 事务型消息的发送逻辑为:
- 先发送 Prepared 状态的消息到 Broker 中;
- 再执行本地事务(下单),本地事务的执行在回调方法
executeLocalTransaction
中;
- 根据本地事务(下单)的成功与否决定提交 Broker 中的消息还是撤回;
package com.lixinlei.miaosha.mq;
import com.alibaba.fastjson.JSON;
import com.lixinlei.miaosha.error.BusinessException;
import com.lixinlei.miaosha.service.OrderService;
import com.lixinlei.miaosha.service.model.OrderModel;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.*;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
@Component
public class MqProducer {
private DefaultMQProducer producer;
private TransactionMQProducer transactionMQProducer;
@Value("${mq.nameserver.addr}")
private String nameAddr;
@Value("${mq.topicname}")
private String topicName;
@Autowired
private OrderService orderService;
/**
* 在 Bean 初始化完成之后调用
*/
@PostConstruct
public void init() throws MQClientException {
producer = new DefaultMQProducer("producer_group");
producer.setNamesrvAddr(nameAddr);
producer.start();
transactionMQProducer = new TransactionMQProducer("transaction_producer_group");
transactionMQProducer.setNamesrvAddr(nameAddr);
transactionMQProducer.start();
transactionMQProducer.setTransactionListener(new TransactionListener() {
/**
* 消息以 Prepared 状态被保存进 Broker 后执行
* @param message
* @param args `transactionMQProducer.sendMessageInTransaction(message, argsMap)` 中传入的 `argsMap`
* @return
*/
@Override
public LocalTransactionState executeLocalTransaction(Message message, Object args) {
// 真正要执行的操作:创建订单
Integer userId = (Integer)((Map) args).get("userId");
Integer itemId = (Integer)((Map) args).get("itemId");
Integer promoId = (Integer)((Map) args).get("promoId");
Integer amount = (Integer)((Map) args).get("amount");
try {
OrderModel orderModel = orderService.createOrder(userId, itemId, promoId, amount);
} catch (BusinessException e) {
e.printStackTrace();
return LocalTransactionState.ROLLBACK_MESSAGE;
}
return LocalTransactionState.COMMIT_MESSAGE;
}
/**
* 如果 `OrderModel orderModel = orderService.createOrder(userId, itemId, promoId, amount)` 执行成功了,但是
* Tomcat 和 MySQL 中的连接断了,既走不到 ROLLBACK_MESSAGE,也走不到 COMMIT_MESSAGE,那么这个事务型消息的状态就是
* UNKNOW,在 UNKNOW 的情况下,Broker 会定期回调本方法;
* @param msg
* @return
*/
@Override
public LocalTransactionState checkLocalTransaction(MessageExt msg) {
return null;
}
});
}
/**
* 事务型让 MySQL 同步 Redis 中库存扣减的消息
* @param itemId
* @param amount
* @return
*/
public boolean transactionAsyncReduceStock(Integer userId, Integer promoId, Integer itemId, Integer amount) {
Map bodyMap = new HashMap<>();
bodyMap.put("itemId", itemId);
bodyMap.put("amount", amount);
Map argsMap = new HashMap<>();
argsMap.put("itemId", itemId);
argsMap.put("amount", amount);
argsMap.put("userId", userId);
argsMap.put("promoId", promoId);
Message message = new Message(
topicName,
"increase",
JSON.toJSON(bodyMap).toString().getBytes(Charset.forName("UTF-8")));
TransactionSendResult sendResult = null;
try {
/**
* 事务型消息有一个二阶段提交的概念:
* 消息发出后,Broker 的确可以收到消息,但是状态是不可被消费的状态,而是 Prepared 状态;
* 消息发出后,会回调 Producer 端的 executeLocalTransaction 方法;
*/
sendResult = transactionMQProducer.sendMessageInTransaction(message, argsMap);
} catch (MQClientException e) {
e.printStackTrace();
return false;
}
if (sendResult.getLocalTransactionState() == LocalTransactionState.ROLLBACK_MESSAGE) {
return false;
} else if (sendResult.getLocalTransactionState() == LocalTransactionState.COMMIT_MESSAGE) {
return true;
} else {
return false;
}
}
}
不再是 Controller 直接调用下单的 Service
- Controller 直接发送事务型消息给 Broker,下单操作作为本地事务在回调中执行;
@RequestMapping(value = "/createorder", method = {RequestMethod.POST}, consumes = {CONTENT_TYPE_FORMED})
@ResponseBody
public CommonReturnType createOrder(@RequestParam(name = "itemId") Integer itemId,
@RequestParam(name = "amount") Integer amount,
@RequestParam(name = "promoId", required = false) Integer promoId) throws BusinessException {
String token = httpServletRequest.getParameterMap().get("token")[0];
if (StringUtils.isEmpty(token)) {
throw new BusinessException(EmBusinessError.USER_NOT_LOGIN, "用户还未登录,不能下单");
}
UserModel userModel = (UserModel) redisTemplate.opsForValue().get(token);
if (userModel == null) {
throw new BusinessException(EmBusinessError.USER_NOT_LOGIN, "用户还未登录,不能下单");
}
// OrderModel orderModel = orderService.createOrder(userModel.getId(), itemId, promoId, amount);
if(!mqProducer.transactionAsyncReduceStock(userModel.getId(), promoId, itemId, amount)) {
throw new BusinessException(EmBusinessError.UNKNOWN_ERROR, "下单失败");
}
return CommonReturnType.create(null);
}
减库存的操作中不再给 MQ 发消息
- 给 Broker 发扣减库存的消息不再跟在 Redis 操作(减 Redis 中的库存)之后,而是作为事务型消息,由 Controller 发送;
@Override
@Transactional
public boolean decreaseStock(Integer itemId, Integer amount) {
// int affectedRows = itemStockDOMapper.decreaseStock(itemId, amount);
long result = redisTemplate.opsForValue().increment("promo_item_stock_" + itemId, amount * -1);
if (result >= 0) {
// boolean mqResult = mqProducer.asyncReduceStock(itemId, amount);
// if (!mqResult) {
// redisTemplate.opsForValue().increment("promo_item_stock_" + itemId, amount);
// return false;
// }
return true;
} else {
redisTemplate.opsForValue().increment("promo_item_stock_" + itemId, amount);
return false;
}
}