首先创建一个seata的springboot模块并引入seata的起步依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
</dependency>
模块的目录结构如下
seata.yaml中的内容为:
seata:
tx-service-group: seata-toutiao
service:
vgroup-mapping: # 事务组与cluster的映射关系
seata-toutiao: DEFAULT
grouplist:
DEFAULT: 192.168.211.136:8091
spring.factories中的内容为:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.jjw.core.seata.SeataHeimaAutoConfiguration
org.springframework.boot.env.EnvironmentPostProcessor=\
com.jjw.core.seata.MyEnvironmentPostProcessor
MyEnvironmentPostProcessor中的内容为:
package com.jjw.core.seata;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.boot.env.YamlPropertySourceLoader;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import java.io.IOException;
import java.util.List;
/**
* 自定义环境处理,在运行SpringApplication之前加载任意配置文件到Environment环境中
*/
public class MyEnvironmentPostProcessor implements EnvironmentPostProcessor {
//Properties对象
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
//自定义配置文件
String[] profiles = {
"seata.yaml"
};
//循环添加
for (String profile : profiles) {
//从classpath路径下面查找文件
Resource resource = new ClassPathResource(profile);
//加载成PropertySource对象,并添加到Environment环境中
environment.getPropertySources().addFirst(loadProfiles(resource));
}
}
//加载单个配置文件
private PropertySource<?> loadProfiles(Resource resource) {
if (!resource.exists()) {
throw new IllegalArgumentException("资源" + resource + "不存在");
}
try {
YamlPropertySourceLoader yamlPropertySourceLoader = new YamlPropertySourceLoader();
List<PropertySource<?>> resources = yamlPropertySourceLoader.load(resource.getFilename(), resource);
return resources.get(0);
} catch (IOException ex) {
throw new IllegalStateException("加载配置文件失败" + resource, ex);
}
}
}
SeataHeimaAutoConfiguration中的内容为:
package com.jjw.core.seata;
import io.seata.rm.datasource.DataSourceProxy;
import io.seata.spring.boot.autoconfigure.properties.SeataProperties;
import io.seata.spring.boot.autoconfigure.properties.client.ServiceProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.HashMap;
import java.util.Map;
/**
* --加载文件properties
* https://www.cnblogs.com/huanzi-qch/p/11122107.html
* --加载yaml
* https://blog.csdn.net/baidu_28523317/article/details/108701391
*
* --顺序
* https://blog.csdn.net/f641385712/article/details/105596178
*
* --可能存在的问题
* https://segmentfault.com/q/1010000040364236
*/
@Configuration
@ConditionalOnClass(DataSourceProxy.class)
public class SeataHeimaAutoConfiguration {
//仅仅用作测试
@Bean
public Map<String,String> seatax(SeataProperties seataProperties,
ServiceProperties serviceProperties){
String txServiceGroup = seataProperties.getTxServiceGroup();
System.out.println(txServiceGroup);
System.out.println(serviceProperties.getGrouplist());
return new HashMap<>();
}
}
在需要用到分布式事务的微服务中添加依赖(用到几个微服务就给相应的微服务都添加分布式事务依赖)、且对这几个微服务所在的库都添加一个undolog表:
-- auto-generated definition
create table undo_log
(
id bigint auto_increment
primary key,
branch_id bigint not null,
xid varchar(100) not null,
context varchar(128) not null,
rollback_info longblob not null,
log_status int not null,
log_created datetime not null,
log_modified datetime not null,
ext varchar(100) null,
constraint ux_undo_log
unique (xid, branch_id)
)
charset = utf8;
在最开始使用分布式事务的那个微服务上的方法上添加事务注解
@GlobalTransactional // 全局事务
@Transactional // 本地事务
@Override
public void pass(Integer id) {