项目介绍
LCN并不生产事务,LCN只是本地事务的搬运工
整体框架
场景1
调用流程如下:
正常调用时序图如下:
参与方B出现异常调用时序图如下:
场景2
调用流程如下:
参与方B出现异常调用时序图如下:
快速入门
源码阅读部分
源码目录结构
txlcn-tc:TXLCN分布式事务客户端
txlcn-common:公共模块
txlcn-logger:日志模块。(默认提供日志持久化到MySQL的支持)
txlcn-tm:TXLCN事务管理器
txlcn-txmsg:事务消息扩展接口
txlcn-txmsg-netty:事务消息接口的Netty实现
txlcn-tracing:分布式事务追踪工具
txlcn-tc部分
1、@LcnTransaction
拦截LcnTransaction注解
开启事务
注意groupId的生成是雪花算法
2、LcnConnectionProxy
public class LcnConnectionProxy implements Connection {
private Connection connection;
public LcnConnectionProxy(Connection connection) {
this.connection = connection;
}
/**
* notify connection
*
* @param state transactionState
* @return RpcResponseState RpcResponseState
*/
public RpcResponseState notify(int state) {
try {
if (state == 1) {
log.debug("commit transaction type[lcn] proxy connection:{}.", this);
connection.commit();
} else {
log.debug("rollback transaction type[lcn] proxy connection:{}.", this);
connection.rollback();
}
connection.close();
log.debug("transaction type[lcn] proxy connection:{} closed.", this);
return RpcResponseState.success;
} catch (Exception e) {
log.error(e.getLocalizedMessage(), e);
return RpcResponseState.fail;
}
}
@Override
public void setAutoCommit(boolean autoCommit) throws SQLException {
connection.setAutoCommit(false);
}
@Override
public void commit() throws SQLException {
//connection.commit();
}
@Override
public void rollback() throws SQLException {
//connection.rollback();
}
@Override
public void close() throws SQLException {
//connection.close();
}
复制代码
3、com.codingapi.txlcn.tc.core.checking.SimpleDTXChecking#startDelayCheckingAsync
txlcn-tm部分
1、com.codingapi.txlcn.txmsg.netty.handler.RpcAnswerHandler
2、com.codingapi.txlcn.tm.core.TransactionManager
3、com.codingapi.txlcn.tm.support.service.TxExceptionService#writeTxException 事务补偿
4、com.codingapi.txlcn.tm.txmsg.transaction.InitClientService
5、com.codingapi.txlcn.tm.support.service.impl.TxExceptionServiceImpl#getTransactionInfo
txlcn-tracing事务跟踪
代码很简单
com.codingapi.txlcn.tracing.http.RestTemplateRequestInterceptor和
com.codingapi.txlcn.tracing.http.FeignRequestInterceptor对于feign和restTemplate进行拦截 向header里面放入groupId和X-App-Map信息;
com.codingapi.txlcn.tracing.http.TracingHandlerInterceptor进行拦截接收数据,将上述数据初始化到TracingContext之中。
另外,作者写了一个 TxlcnZoneAvoidanceRule 负载均衡算法
com.codingapi.txlcn.tc.core.transaction.lcn.resource.LcnTransactionResourceProxy 不赘述
重要断点时候变量值
com.codingapi.txlcn.tc.txmsg.LoopMessenger#request(com.codingapi.txlcn.txmsg.dto.MessageDto, long, java.lang.String)
参考文章