Seata1.4配置及使用
一、服务器使用docker配置seata
1.拉取镜像
sudo docker pull seataio/seata-server
2.运行镜像
sudo docker run --name seata-server -p 8091:8091 -d seataio/seata-server
3.复制镜像中的配置文件到本机
sudo docker cp seata-server:/seata-server /home/dxusr/seata
4.停止服务
sudo docker stop [CONTAINER ID]
5.删除服务
sudo docker rm [CONTAINER ID]
6.切换到seata配置目录
sudo cd /home/dxusr/seata/resources
7.重新运行镜像
sudo docker run --name seata-server -p 8091:8091 -v /home/dxusr/seata[配置路径]:/seata-server -e SEATA_PORT=8091 seataio/seata-server
#其中 -e 用于配置环境变量, -v 用于挂载宿主机的目录
8.修改registry.conf
#服务,将type从file改成nacos,将seata服务配置进nacos
registry {
type = "nacos"
nacos {
application = "seata-server"
serverAddr = "ip:8848"
group = "DEFAULT_GROUP"
namespace = ""
cluster = "default"
username = "nacos"
password = "dx12345@"
}
}
#配置,将type从file改成nacos,这样不需要每个项目都放file.conf
config {
type = "nacos"
nacos {
serverAddr = "ip:8848"
namespace = ""
group = "SEATA_GROUP"
username = "username"
password = "password"
dataId = ""
}
}
8.导入Seata相应的配置项到Nacos的配置中心(非注册中心)
修改config.txt
service.vgroupMapping.yg_tx_group=default
service.default.grouplist=ip:8091 #seata服务地址
store.mode=db
store.lock.mode=db
store.session.mode=db
store.db.driverClassName=com.mysql.cj.jdbc.Driver #mysql8使用
store.db.url=jdbc:mysql://数据库地址/seata?useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=false&serverTimezone=Asia/Shanghai
store.db.user=数据库用户
store.db.password=数据库密码
导入nacos(进入git bash)
sh [nacos-config.sh文件路径] -h [nacos-ip地址] -p 8848 -g SEATA_GROUP[导入的组] -u [用户名] -w [密码]
9.创建相关的数据库(在回滚或提交前会将日志保存在数据库中,成功后会删除)
创建seata数据库:数据库脚本:数据库名称seata
-- 在业务数据库中创建undo_log(与上面的脚本不在一个数据库中)
CREATE TABLE IF NOT EXISTS `undo_log`
(
`branch_id` BIGINT NOT NULL COMMENT 'branch transaction id',
`xid` VARCHAR(128) NOT NULL COMMENT 'global transaction id',
`context` VARCHAR(128) NOT NULL COMMENT 'undo_log context,such as serialization',
`rollback_info` LONGBLOB NOT NULL COMMENT 'rollback info',
`log_status` INT(11) NOT NULL COMMENT '0:normal status,1:defense status',
`log_created` DATETIME(6) NOT NULL COMMENT 'create datetime',
`log_modified` DATETIME(6) NOT NULL COMMENT 'modify datetime',
UNIQUE KEY `ux_undo_log` (`xid`, `branch_id`)
) ENGINE = InnoDB
AUTO_INCREMENT = 1
DEFAULT CHARSET = utf8 COMMENT ='AT transaction mode undo table';
二.编写代码
nacos2.0.0+openfeign2.0.4+seata1.4.0
1.pom引入
1.依赖seata-spring-boot-starter,支持yml、properties配置(.conf可删除),内部已依赖seata-all
2.依赖spring-cloud-alibaba-seata,内部集成了seata,并实现了xid传递
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-seata</artifactId>
<version>2.2.0.RELEASE</version>
<exclusions>
<exclusion>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
<version>1.4.0</version>
<exclusions>
<exclusion>
<artifactId>druid</artifactId>
<groupId>com.alibaba</groupId>
</exclusion>
</exclusions>
</dependency>
2.yml配置
seata:
tx-service-group: yg_tx_group #与config.txt 中的service.vgroupMapping.yg_tx_group=default一致
service:
vgroup-mapping:
yg_tx_group: default #与config.txt 中的service.vgroupMapping.yg_tx_group=default一致
grouplist:
default: ip:8091 #seata 服务地址
3.代码编写
1.主事务:方法上使用@GlobalTransactional+@Transactional,分支事务:方法上使用@Transactional