项目引入pom文件
<!-- SpringCloud Seata 组件-->
<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-seata</artifactId><version>${alibaba.seata}</version><exclusions><exclusion><groupId>io.seata</groupId><artifactId>seata-all</artifactId></exclusion></exclusions>
</dependency>
<dependency><groupId>io.seata</groupId><artifactId>seata-spring-boot-starter</artifactId><version>${seata}</version>
</dependency>
yml配置文件
#====================================Seata Config===============================================
seata:enabled: truetx-service-group: my_test_tx_group # 1 事务群组(可以每个应用独立取名,也可以使用相同的名字)这个需要跟nacos的配置名字保持一致registry:type: nacosnacos:server-addr: ${nacos-addr} #nacos地址 localhost:8848application: service-seatanamespace: seata #seata在nacos中配置的命名空间group: SEATA_GROUPcluster: defaultusername: nacospassword: nacosconfig:type: nacosnacos:server-addr: ${nacos-addr} #nacos地址 localhost:8848namespace: seata #seata在nacos中配置的命名空间group: SEATA_GROUPusername: nacospassword: nacos
Seata文件配置
修改file.conf
## transaction log store, only used in seata-server
store {## store mode: file、db、redismode = "db" ##将file改为db## file store propertyfile {## store location dirdir = "sessionStore"# branch session size , if exceeded first try compress lockkey, still exceeded throws exceptionsmaxBranchSessionSize = 16384# globe session size , if exceeded throws exceptionsmaxGlobalSessionSize = 512# file buffer size , if exceeded allocate new bufferfileWriteBufferCacheSize = 16384# when recover batch read sizesessionReloadReadSize = 100# async, syncflushDiskMode = async}## database store propertydb {## the implement of javax.sql.DataSource, such as DruidDataSource(druid)/BasicDataSource(dbcp)/HikariDataSource(hikari) etc.datasource = "druid"## mysql/oracle/postgresql/h2/oceanbase etc.dbType = "mysql"driverClassName = "com.mysql.jdbc.Driver"url = "jdbc:mysql://127.0.0.1:3306/db_seata" ##修改为自己的数据库连接user = "root"password = "123456"minConn = 5maxConn = 30globalTable = "global_table"branchTable = "branch_table"lockTable = "lock_table"queryLimit = 100maxWait = 5000}## redis store propertyredis {host = "127.0.0.1"port = "6379"password = ""database = "0"minConn = 1maxConn = 10queryLimit = 100}
}
修改regitry.conf
registry {# file 、nacos 、eureka、redis、zk、consul、etcd3、sofatype = "nacos" ##我们使用的是nacos,因此将type修改为nacosnacos {application = "service-seata" #application填写注册的服务名serverAddr = "127.0.0.1:8848" #填写nacos注册中心地址group = "SEATA_GROUP" #group是服务注册的分组。可不填,注:是服务分组,不是配置分组namespace = "seata" #填写服务注册时的命名空间,可不填,不填默认的时public命名空间cluster = "default" #cluster不用改username = "nacos" password = "nacos"}file {name = "file.conf"}
}
config {# file、nacos 、apollo、zk、consul、etcd3type = "nacos"nacos {serverAddr = "127.0.0.1:8848"namespace = "seata"group = "SEATA_GROUP"username = "nacos"password = "nacos"}file {name = "file.conf"}
}
将配置导入nacos
本来我们是需要把file.conf和registry.conf放入我们的项目中的,这里直接导入到nacos。需要什么配置直接从nacos中获取。不需要放在项目中了
修改conf.txt
在conf目录下
service.vgroupMapping.my_test_tx_group=default #需要跟配置文件的保持一致
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/db_seata?useUnicode=true #自己的数据库连接地址
store.db.user=root
store.db.password=123456
store.db.minConn=5
store.db.maxConn=30
store.db.globalTable=global_table
store.db.branchTable=branch_table
store.db.queryLimit=100
store.db.lockTable=lock_table
store.db.maxWait=5000
创建命名空间
执行导入
打开git bash。
输入以下命令:
sh nacos-config.sh -h localhost -p 8848 -g SEATA_GROUP -t seata -u nacos -w nacos
注:命令解析:-h -p 指定nacos的端口地址;-g 指定配置的分组,注意,是配置的分组;-t 指定命名空间id; -u -w指定nacos的用户名和密码,同样,这里开启了nacos注册和配置认证的才需要指定。
导入成功之后如下图:
数据库配置
db_seata数据库中导入数据表
branch_table
global_table
lock_table
业务数据库中导入数据表
undo_log
启动Seata
配置成功
记得主接口增加注解 @GlobalTransactional
远程调用服务增加注解 @Transactional