一、SEATA CLIENT
2022-05-12 20:59:23 6 举报
AI智能生成
seata客户端源码解析--seata全局事务处理 1、首先,通过SpringBoot的自动配置,加载配置类SeataAutoConfiguration,在该类中创建bean GlobalTransactionScanner ; 2、GlobalTransactionScanner 类实现了接口AbstractAutoProxyCreator,AbstractAutoProxyCreator接口实现了方法SmartInstantiationAwareBeanPostProcessor,在方法postProcessAfterInitialization调用了方法wrapIfNecessary(Object bean, String beanName, Object cacheKey),而在类GlobalTransactionScanner 中重写了方法wrapIfNecessary(Object bean, String beanName, Object cacheKey),从而给容器中相关的bean进行代理。 3、GlobalTransactionScanner 实现了InitializingBean接口,实现了方法afterPropertiesSet(),在该方法中初始化TM和RM客户端,从而与TC进行通信,进行事务的发起,提交与回滚。 4、在GlobalTransactionScanner 类中的wrapIfNecessary()方法中,生成一个拦截器GlobalTransactionalInterceptor(); 5、在GlobalTransactionScanner 类的invoke(final MethodInvocation methodInvocation)方法中,会先判断执行的代理类的方法上是否存在全局事务注解@GlobalTransactional没存在则处理全局事务handleGlobalTransaction(methodInvocation, globalTransactionalAnnotation);,否则是否存@GlobalLock,则处理全局锁,否则,则为普通方法,直接执行方法内容。 6、在方法handleGlobalTransaction(methodInvocation, globalTransactionalAnnotation);中,会开启全局事务,执行业务逻辑,回滚事务,提交事务等,最终会通过第三步中的初始化的客户端,与TC进行全局事务的开启,提交,回滚等。
作者其他创作
大纲/内容
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=io.seata.spring.boot.autoconfigure.SeataAutoConfiguration
org.springframework.boot.autoconfigure.EnableAutoConfiguration=io.seata.spring.boot.autoconfigure.SeataAutoConfiguration
进入类:SeataAutoConfiguration,SeataAutoConfiguration配置类,创建bean:GlobalTransactionScanner
@Bean
@DependsOn({BEAN_NAME_SPRING_APPLICATION_CONTEXT_PROVIDER})
@ConditionalOnMissingBean(GlobalTransactionScanner.class)
public GlobalTransactionScanner globalTransactionScanner(SeataProperties seataProperties) {
if (LOGGER.isInfoEnabled()) {
LOGGER.info("Automatically configure Seata");
}
return new GlobalTransactionScanner(seataProperties.getApplicationId(), seataProperties.getTxServiceGroup());
}
@Bean
@DependsOn({BEAN_NAME_SPRING_APPLICATION_CONTEXT_PROVIDER})
@ConditionalOnMissingBean(GlobalTransactionScanner.class)
public GlobalTransactionScanner globalTransactionScanner(SeataProperties seataProperties) {
if (LOGGER.isInfoEnabled()) {
LOGGER.info("Automatically configure Seata");
}
return new GlobalTransactionScanner(seataProperties.getApplicationId(), seataProperties.getTxServiceGroup());
}
- 该类继承类:AbstractAutoProxyCreator
- 重写方法:wrapIfNecessary(Object bean, String beanName, Object cacheKey)
1、校验是否是TCC模式代理,若不是进行下一步
2、获取一个全局事务的拦截器:interceptor = new GlobalTransactionalInterceptor(failureHandlerHook);
进入类:GlobalTransactionalInterceptor 该类实现接口MethodInterceptor,执行invoke(final MethodInvocation methodInvocation) 方法
1、获取@GlobalTransactional注解:GlobalTransactional globalTransactionalAnnotation = getAnnotation(method, GlobalTransactional.class);
- 2、如果方法上存在@GlobalTransactional注解注解,则开始全局事务处理:if (!disable && globalTransactionalAnnotation != null) { return handleGlobalTransaction(methodInvocation, globalTransactionalAnnotation); }
进入方法handleGlobalTransaction(final MethodInvocation methodInvocation,
final GlobalTransactional globalTrxAnno)
final GlobalTransactional globalTrxAnno)
进入核心类TransactionalTemplate transactionalTemplate = new TransactionalTemplate() 的execute()核心方法
1、入参为:new TransactionalExecutor() {
@Override
public Object execute() throws Throwable {
return methodInvocation.proceed();
}
public String name() {
String name = globalTrxAnno.name();
if (!StringUtils.isNullOrEmpty(name)) {
return name;
}
return formatMethod(methodInvocation.getMethod());
}
@Override
public TransactionInfo getTransactionInfo() {
TransactionInfo transactionInfo = new TransactionInfo();
transactionInfo.setTimeOut(globalTrxAnno.timeoutMills());
transactionInfo.setName(name());
Set<RollbackRule> rollbackRules = new LinkedHashSet<>();
for (Class<?> rbRule : globalTrxAnno.rollbackFor()) {
rollbackRules.add(new RollbackRule(rbRule));
}
for (String rbRule : globalTrxAnno.rollbackForClassName()) {
rollbackRules.add(new RollbackRule(rbRule));
}
for (Class<?> rbRule : globalTrxAnno.noRollbackFor()) {
rollbackRules.add(new NoRollbackRule(rbRule));
}
for (String rbRule : globalTrxAnno.noRollbackForClassName()) {
rollbackRules.add(new NoRollbackRule(rbRule));
}
transactionInfo.setRollbackRules(rollbackRules);
return transactionInfo;
}
}
@Override
public Object execute() throws Throwable {
return methodInvocation.proceed();
}
public String name() {
String name = globalTrxAnno.name();
if (!StringUtils.isNullOrEmpty(name)) {
return name;
}
return formatMethod(methodInvocation.getMethod());
}
@Override
public TransactionInfo getTransactionInfo() {
TransactionInfo transactionInfo = new TransactionInfo();
transactionInfo.setTimeOut(globalTrxAnno.timeoutMills());
transactionInfo.setName(name());
Set<RollbackRule> rollbackRules = new LinkedHashSet<>();
for (Class<?> rbRule : globalTrxAnno.rollbackFor()) {
rollbackRules.add(new RollbackRule(rbRule));
}
for (String rbRule : globalTrxAnno.rollbackForClassName()) {
rollbackRules.add(new RollbackRule(rbRule));
}
for (Class<?> rbRule : globalTrxAnno.noRollbackFor()) {
rollbackRules.add(new NoRollbackRule(rbRule));
}
for (String rbRule : globalTrxAnno.noRollbackForClassName()) {
rollbackRules.add(new NoRollbackRule(rbRule));
}
transactionInfo.setRollbackRules(rollbackRules);
return transactionInfo;
}
}
2、public Object execute(TransactionalExecutor business)
1、 get or create a transaction 获取或者创建一个全局事务 GlobalTransaction tx = GlobalTransactionContext.getCurrentOrCreate();
getCurrentOrCreate()
2、// 2. get transactionInfo 获取事务信息 TransactionInfo txInfo = business.getTransactionInfo();
getTransactionInfo()
3、begin transaction 开启事务 beginTransaction(txInfo, tx);
private void beginTransaction(TransactionInfo txInfo, GlobalTransaction tx)
4、Do Your Business,执行业务逻辑 rs = business.execute();
execute()
5、the needed business exception to rollback. 回滚事 completeTransactionAfterThrowing(txInfo,tx,ex);
TransactionalTemplate#completeTransactionAfterThrowing
6、everything is fine, commit. 无异常方式,提交事务 commitTransaction(tx);
TransactionalTemplate#commitTransaction
添加注解GlobalLock
未开启事务,即两个注解都不存在,直接执行业务方法
- 实现Spring接口InitializingBean,实现方法afterPropertiesSet,该方法负责初始化Netty客户端,实现RPC通信
初始化netty客户端
初始TM客户端
初始化RM客户端
注册钩子程序registerSpringShutdownHook();
收藏
收藏
0 条评论
下一页