版本信息
nacos:2.0.3
seata:1.4.2
mysql:8.0
1.先部署nacos,以下为docker部署方式
并建立seata的命名空间,ID建议设为seata
2.下载seata服务端
选择合适版本的binary
3.下载seata全局事务会话信息表并建表
4.下载配置文件conf.txt,然后将其放在根目录下,然后修改配置内容,主要关注如下几个配置
#配置事务分组及对外地址
service.vgroupMapping.XXX_tx_group=default
service.default.grouplist=127.0.0.1:8091
#配置db类型及mysql信息
store.mode=db
store.db.datasource=druid
store.db.dbType=mysql
store.db.driverClassName=com.mysql.jdbc.Driver
store.db.url=jdbc:mysql://127.0.0.1:3306/seata?useUnicode=true&rewriteBatchedStatements=true&serverTimezone=GMT%2B8
store.db.user=root
store.db.password=123456
5.下载nacos-config.sh脚本,使用git bash执行命令将conf.txt上传到nacos
sh nacos-config.sh -h localhost -p 8848 -g SEATA_GROUP -t seata -u nacos -w nacos
其中-h -p -g是nacos的host、port和group,此处均为默认值,可以不输入,-t是第1步中新建的命令空间id,-u -w是nacos的账号和密码
6.进入conf目录下的registry.conf,按需修改配置
registry {
type = "nacos"
nacos {
application = seata-server
serverAddr = "localhost:8848"
namespace = ""
group = "SEATA_GROUP"
cluster = "default"
username = nacos
password = nacos
}
}
config {
type = "nacos"
nacos {
serverAddr = "localhost:8848"
namespace = ""
group = "SEATA_GROUP"
cluster = "default"
username = nacos
password = nacos
}
}
7.启动bin目录下的seata-server.bat
8.spring cloud项目添加依赖,注意seata的版本要和服务端相同
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
<version>2021.1</version>
</dependency>
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-all</artifactId>
<version>1.4.2</version>
</dependency>
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
<version>1.4.2</version>
</dependency>
9.配置yml
spring:
cloud:
nacos:
discovery:
server-addr: localhost:8848
group: "SEATA_GROUP"
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/database?characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false&nullCatalogMeansCurrent=true&allowPublicKeyRetrieval=true
username: root
password: 123456
#type: com.alibaba.druid.pool.DruidDataSource#不要配置datasource的type
autoconfigure:
exclude: [ org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure ]#排除自动配置
seata:
application-id: order#服务名称
tx-service-group: XXX_tx_group#服务端配置的分组
enable-auto-data-source-proxy: false#关闭自动配置datasource
config:
type: nacos
nacos:
server-addr: 127.0.0.1:8848
namespace: "seata"
group: "SEATA_GROUP"
username: "nacos"
password: "nacos"
registry:
nacos:
application: seata-server#服务端配置的服务名称
server-addr: 127.0.0.1:8848
namespace: "seata"
group: "SEATA_GROUP"
username: "nacos"
password: "nacos"
10.配置数据源
@Configuration
public class DataSourceProxyConfig {
@Value("${mybatis.mapperLocations}")
private String mapperLocations;
@Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource() {
return new DruidDataSource();
}
//开启本地事务,若不需要可以不加
@Bean("txManager")
public DataSourceTransactionManager txManager(DataSourceProxy dataSourceProxy) {
return new DataSourceTransactionManager(dataSourceProxy);
}
@Bean
public DataSourceProxy dataSourceProxy(DataSource dataSource) {
return new DataSourceProxy(dataSource);
}
@Bean
public SqlSessionFactory sqlSessionFactoryBean(DataSourceProxy dataSourceProxy) throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSourceProxy);
sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapperLocations));
sqlSessionFactoryBean.setTransactionFactory(new SpringManagedTransactionFactory());
return sqlSessionFactoryBean.getObject();
}
}
11.若需要开启本地事务,则启动类增加@EnableTransactionManagement
12.下载undo_log数据表并建表到业务数据库
13.在需要增加全局事务的方法入口增加@GlobalTransactional
14.启动服务并测试
根据日志在AbstractUndoLogManager中打断点可以看到undo_log确实写入了数据,并且在事务执行完毕后删除