seata的架构
seata是无状态,低耦合的微服务,为了达到回滚的状态需要建立seata的服务
1、下载seata 服务
http://seata.io/zh-cn/blog/download.html
本文下载的是 1.4.2
下载后解压的目录
2、修改seata配置文件
2.1、配置注册配置文件
本文由于没有架设集群采用file 模式
registry.conf
registry {
# file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
type = "eureka" # 改为 eureka
eureka { # 修改eureka 数据块中的数据
serviceUrl = "http://192.168.1.2:9876/eureka" # eureka服务的地址
application = "seata-server" # seata 的显示名
weight = "1"
}
}
file.conf
将 file.conf 和 file.conf.example 互换名称
并在 新 的 file.conf 中加入
service {
#transaction service group mapping
# 注意:1.2.0 版本(或更早的版本) 已经将 ‘vgroup_mapping’ 改为 ‘vgroupMapping’
vgroupMapping.fsp_tx_group = "default"
#only support when registry.type=file, please don't set multiple addresses
default.grouplist = "127.0.0.1:8091"
#degrade, current not support 降级处理
enableDegrade = false
#disable seata 是否开启本地事务
disableGlobalTransaction = false
}
此代码块和 server 同级
此时 启动 seata server 即可
启动后可以在 eureka中看到
3、更新业务代码配置
3.1 业务代码中引入依赖
pom.xml
<!-- Seata -->
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</exclusion>
</exclusions>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-seata</artifactId>
<exclusions>
<exclusion>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
</exclusion>
</exclusions>
<version>2.2.0.RELEASE</version>
</dependency>
3.2 修改 yml文件
# seata的配置
seata:
enabled: true
application-id: seata-server
tx-service-group: default # 此时不能变化,否则会出现io.seata.common.exception.FrameworkException: No available service
enable-auto-data-source-proxy: true
use-jdk-proxy: false
service:
vgroup-mapping:
default: seata-server
enable-degrade: false
disable-global-transaction: false
registry:
type: eureka
eureka:
weight: 1
service-url: http://192.168.1.2:9876/eureka/
# eureka
eureka:
client:
service-url:
defaultZone: http://192.168.1.2:9876/eureka/
enabled: true
在serviceImp上加入 注解
@GlobalTransactional
在 application中加入
@EnableAutoDataSourceProxy
@MapperScan("com.test.usr.mapper")
@SpringBootApplication()
@EnableEurekaClient
@EnableFeignClients
@EnableAutoDataSourceProxy
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
}
3.3、在数据库中加入 undo_log
如果你需要用 seata 这个表必须加入
mysql
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';
启动 seata服务器
启动成功:
后台启动 seata
nohup ./bin/seata-server.sh -p 8091 -h 127.0.0.1 -m file >nohup.out 2>1 &