seata的架構

image.png
seata是無狀態(tài),低耦合的微服務,為了達到回滾的狀態(tài)需要建立seata的服務
1、下載seata 服務
http://seata.io/zh-cn/blog/download.html
本文下載的是 1.4.2
下載后解壓的目錄

image.png
2、修改seata配置文件

image.png
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 互換名稱

image.png
并在 新 的 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 同級

image.png
此時 啟動 seata server 即可

image.png

image.png
啟動后可以在 eureka中看到

image.png
3、更新業(yè)務代碼配置
3.1 業(yè)務代碼中引入依賴
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

image.png
在 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服務器
啟動成功:

image.png
后臺啟動 seata
nohup ./bin/seata-server.sh -p 8091 -h 127.0.0.1 -m file >nohup.out 2>1 &