分布式事务的章节中,讲解了可靠消息最终一致性实现方案https://www.jianshu.com/p/3186a0a76c1d,本篇以具体实现代码举例。
简介RocketMq事务消息
消息队列RocketMQ提供的分布式事务消息适用于所有对数据最终一致性有强需求的场景。
可靠消息最终一致性方案是指当事务发起方执行完成本地事务后发出一条消息到消息中间件,事务参与方(消息消费者)一定能够接收到消息并处理事务成功,此方案强调的是只要消息发给事务参与方,则最终事务要达到一致。
可靠消息最终一致性方案原理
两个主要过程
半事务消息:暂不能投递的消息,发送方已经成功地将消息发送到了消息队列RocketMQ版服务端,但是服务端未收到生产者对该消息的二次确认,此时该消息被标记成“暂不能投递”状态,处于该种状态下的消息即半事务消息。
消息回查:由于网络闪断、生产者应用重启等原因,导致某条事务消息的二次确认丢失,消息队列RocketMQ版服务端通过扫描发现某条消息长期处于“半事务消息”时,需要主动向消息生产者询问该消息的最终状态(Commit或是Rollback),该询问过程即消息回查。
代码实现
三种状态:
RocketMQLocalTransactionState.COMMIT:提交事务,允许订阅方消费该消息。
RocketMQLocalTransactionState.ROLLBACK:回滚事务,消息将被丢弃不允许消费。
RocketMQLocalTransactionState.Unknow:暂时无法判断状态,等待固定时间以后消息队列RocketMQ版服务端向发送方进行消息回查。
控制器:
/**
* 事务消息发送
*/
@RequestMapping("/send/transaction")
public void transaction() {
rocketMqProducer.sendTransaction("test_transaction", "事务消息");
}
生产者:
/**
* 发送事务消息
* @date: 2020/11/30
* @param topic
* @param msgBody
* @return void
* @author weirx
* @version 3.0
*/
public void sendTransaction(String topic, String msgBody) {
//发送事务消息
rocketMQTemplate.sendMessageInTransaction(topic, MessageBuilder.withPayload(msgBody).build(), null);
}
生产者监听:
即文章前面提到的两个主要过程。
1)用于接收发送的半事务消息回调,然后执行本地事务,并返回本地事务状态。
2)当本地事务执行没有进行commit或rollback时,由mq进行发起本地事务回查。
package com.cloud.bssp.message.rocketmq.producer;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQTransactionListener;
import org.apache.rocketmq.spring.core.RocketMQLocalTransactionListener;
import org.apache.rocketmq.spring.core.RocketMQLocalTransactionState;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
/**
* 监听生产者发送消息后的回调,以及本地事务回查
*
* 注意:在rocketmq 2.1.0 版本重构了事务消息api,@RocketMQTransactionListener注解额txProducerGroup属性去掉了
* 可参考:https://blog.csdn.net/z69183787/article/details/109958380
* @date: 2020/11/30
* @author weirx
* @version 3.0
*/
@Slf4j
@Component
@RocketMQTransactionListener
public class ProducerTransactionMessageListener implements RocketMQLocalTransactionListener {
/**
* mq收到半事务消息后的回调方法,此处执行本地事务
* @date: 2020/11/30
* @param message
* @param o
* @return org.apache.rocketmq.spring.core.RocketMQLocalTransactionState
* @author weirx
* @version 3.0
*/
@SneakyThrows
@Override
@Transactional(rollbackFor = Exception.class)
public RocketMQLocalTransactionState executeLocalTransaction(Message message, Object o) {
log.info("half message send success,msg = {}" , message);
try {
//模拟一秒业务处理时间
Thread.sleep(1000);
}catch (Exception e) {
return RocketMQLocalTransactionState.ROLLBACK;
}
log.info("local transaction success, tell mq to commit");
return RocketMQLocalTransactionState.COMMIT;
}
/**
* mq回查本地事务的状态
* @date: 2020/11/30
* @param message
* @return org.apache.rocketmq.spring.core.RocketMQLocalTransactionState
* @author weirx
* @version 3.0
*/
@Override
public RocketMQLocalTransactionState checkLocalTransaction(Message message) {
log.info("check local transaction state");
//模拟本地事务的回查业务
if (System.currentTimeMillis() % 3 == 0){
log.info("local transaction execute success,transaction commit");
return RocketMQLocalTransactionState.COMMIT;
}else {
log.info("local transaction execute failed,transaction rollback");
return RocketMQLocalTransactionState.ROLLBACK;
}
}
}
消费者:
/**
* RocketMqProducer
* @date: 2020/11/26
* @author weirx
* @version 3.0
*/
@Slf4j
@Component
@RocketMQMessageListener(topic = "test_transaction", selectorExpression = "*", consumerGroup = "test_transaction")
public class TransactionMessageListener implements RocketMQListener {
@Override
public void onMessage(MessageExt messageExt) {
byte[] body = messageExt.getBody();
String msg = new String(body);
log.info("receive async message:{}", msg);
}
}
结果:上面的代码模拟的一定成功的过程
2020-11-30 10:27:09.175 INFO 10200 --- [nio-8085-exec-4] m.r.p.ProducerTransactionMessageListener : half message send success,msg = GenericMessage [payload=byte[12], headers={rocketmq_TOPIC=test_transaction, rocketmq_FLAG=0, id=b0987012-7b89-f5a9-f539-00435c10bcf5, contentType=text/plain;charset=UTF-8, rocketmq_TRANSACTION_ID=AC10020827D818B4AAC29796731C0000, timestamp=1606703220762}]
2020-11-30 10:27:25.799 INFO 10200 --- [nio-8085-exec-4] m.r.p.ProducerTransactionMessageListener : local transaction success, tell mq to commit
2020-11-30 10:27:48.923 INFO 10200 --- [MessageThread_1] c.c.b.m.r.c.TransactionMessageListener : receive async message:事务消息
模拟本地事务处理失败
修改生产者监听代码:
/**
* mq收到半事务消息后的回调方法,此处执行本地事务
* @date: 2020/11/30
* @param message
* @param o
* @return org.apache.rocketmq.spring.core.RocketMQLocalTransactionState
* @author weirx
* @version 3.0
*/
@SneakyThrows
@Override
@Transactional(rollbackFor = Exception.class)
public RocketMQLocalTransactionState executeLocalTransaction(Message message, Object o) {
log.info("half message send success,msg = {}" , message);
try {
//模拟一秒业务处理时间
Thread.sleep(1000);
//增加异常抛出
throw new Exception("模拟本地事务处理失败");
}catch (Exception e) {
log.info("local transaction failed, msg = {}" , e.getMessage());
return RocketMQLocalTransactionState.ROLLBACK;
}
// log.info("local transaction success, tell mq to commit");
// return RocketMQLocalTransactionState.COMMIT;
}
执行结果,上面代码返回给mq消费端rollback,消息并没有被consumer消费:
2020-11-30 10:53:55.290 INFO 60932 --- [nio-8085-exec-1] m.r.p.ProducerTransactionMessageListener : half message send success,msg = GenericMessage [payload=byte[12], headers={rocketmq_TOPIC=test_transaction, rocketmq_FLAG=0, id=eb1817a9-3b09-896f-6862-25f717ec75b7, contentType=text/plain;charset=UTF-8, rocketmq_TRANSACTION_ID=AC100208EE0418B4AAC297AF13A00000, timestamp=1606704834320}]
2020-11-30 10:53:56.291 INFO 60932 --- [nio-8085-exec-1] m.r.p.ProducerTransactionMessageListener : local transaction failed, msg = 模拟本地事务处理失败
模拟事务回查
同样修改生产者监听这里,将状态设置为NUKNOWN(不确定是否成功):
/**
* mq收到半事务消息后的回调方法,此处执行本地事务
* @date: 2020/11/30
* @param message
* @param o
* @return org.apache.rocketmq.spring.core.RocketMQLocalTransactionState
* @author weirx
* @version 3.0
*/
@SneakyThrows
@Override
@Transactional(rollbackFor = Exception.class)
public RocketMQLocalTransactionState executeLocalTransaction(Message message, Object o) {
log.info("half message send success,msg = {}", message);
try {
//模拟一秒业务处理时间
Thread.sleep(1000);
//增加异常抛出
// throw new Exception("模拟本地事务处理失败");
} catch (Exception e) {
log.info("local transaction failed, msg = {}", e.getMessage());
return RocketMQLocalTransactionState.ROLLBACK;
}
// log.info("local transaction success, tell mq to commit");
// return RocketMQLocalTransactionState.COMMIT;
log.info("local transaction unknown");
return RocketMQLocalTransactionState.UNKNOWN;
}
结果,UNKNOWN状态下,执行了回查方法:
2020-11-30 13:25:02.878 INFO 24904 --- [nio-8085-exec-3] m.r.p.ProducerTransactionMessageListener : half message send success,msg = GenericMessage [payload=byte[12], headers={rocketmq_TOPIC=test_transaction, rocketmq_FLAG=0, id=558db1b3-2495-4309-996b-1463f6b2c938, contentType=text/plain;charset=UTF-8, rocketmq_TRANSACTION_ID=AC100208614818B4AAC2983971720003, timestamp=1606713901460}]
2020-11-30 13:25:03.879 INFO 24904 --- [nio-8085-exec-3] m.r.p.ProducerTransactionMessageListener : local transaction unknown
2020-11-30 13:25:44.230 INFO 24904 --- [pool-2-thread-1] m.r.p.ProducerTransactionMessageListener : check local transaction state
2020-11-30 13:25:44.230 INFO 24904 --- [pool-2-thread-1] m.r.p.ProducerTransactionMessageListener : local transaction execute failed,transaction rollback
修改生产者监听,阻塞60s:
/**
* mq收到半事务消息后的回调方法,此处执行本地事务
* @date: 2020/11/30
* @param message
* @param o
* @return org.apache.rocketmq.spring.core.RocketMQLocalTransactionState
* @author weirx
* @version 3.0
*/
@SneakyThrows
@Override
@Transactional(rollbackFor = Exception.class)
public RocketMQLocalTransactionState executeLocalTransaction(Message message, Object o) {
log.info("half message send success,msg = {}", message);
try {
//休眠60s
Thread.sleep(60000);
//增加异常抛出
// throw new Exception("模拟本地事务处理失败");
} catch (Exception e) {
log.info("local transaction failed, msg = {}", e.getMessage());
return RocketMQLocalTransactionState.ROLLBACK;
}
// log.info("local transaction success, tell mq to commit");
// return RocketMQLocalTransactionState.COMMIT;
log.info("local transaction unknown");
return RocketMQLocalTransactionState.UNKNOWN;
}
在线程阻塞期间,成功调用了回查方法,成功提交了事务,并被消费:
2020-11-30 13:37:46.398 INFO 53084 --- [nio-8085-exec-1] m.r.p.ProducerTransactionMessageListener : half message send success,msg = GenericMessage [payload=byte[12], headers={rocketmq_TOPIC=test_transaction, rocketmq_FLAG=0, id=74c0bb1c-801c-03be-ca19-aa36815ea64d, contentType=text/plain;charset=UTF-8, rocketmq_TRANSACTION_ID=AC100208CF5C18B4AAC2984517580000, timestamp=1606714665623}]
2020-11-30 13:38:36.361 INFO 53084 --- [pool-2-thread-1] m.r.p.ProducerTransactionMessageListener : check local transaction state
2020-11-30 13:38:38.015 INFO 53084 --- [pool-2-thread-1] m.r.p.ProducerTransactionMessageListener : local transaction execute success,transaction commit
2020-11-30 13:38:41.855 INFO 53084 --- [MessageThread_1] c.c.b.m.r.c.TransactionMessageListener : receive async message:事务消息