seata的架構(gòu)
image.png
seata是無(wú)狀態(tài)时甚,低耦合的微服務(wù)嘹裂,為了達(dá)到回滾的狀態(tài)需要建立seata的服務(wù)
1聚唐、下載seata 服務(wù)
http://seata.io/zh-cn/blog/download.html
本文下載的是 1.4.2
下載后解壓的目錄
image.png
2考廉、修改seata配置文件
image.png
2.1惯殊、配置注冊(cè)配置文件
本文由于沒(méi)有架設(shè)集群采用file 模式
registry.conf
registry {
# file 、nacos 纳本、eureka窍蓝、redis、zk繁成、consul吓笙、etcd3、sofa
type = "eureka" # 改為 eureka
eureka { # 修改eureka 數(shù)據(jù)塊中的數(shù)據(jù)
serviceUrl = "http://192.168.1.2:9876/eureka" # eureka服務(wù)的地址
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 版本(或更早的版本) 已經(jīng)將 ‘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 降級(jí)處理
enableDegrade = false
#disable seata 是否開(kāi)啟本地事務(wù)
disableGlobalTransaction = false
}
此代碼塊和 server 同級(jí)
image.png
此時(shí) 啟動(dòng) seata server 即可
image.png
image.png
啟動(dòng)后可以在 eureka中看到
image.png
3巾腕、更新業(yè)務(wù)代碼配置
3.1 業(yè)務(wù)代碼中引入依賴
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 # 此時(shí)不能變化面睛,否則會(huì)出現(xiàn)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、在數(shù)據(jù)庫(kù)中加入 undo_log
如果你需要用 seata 這個(gè)表必須加入
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';
啟動(dòng) seata服務(wù)器
啟動(dòng)成功:
image.png
后臺(tái)啟動(dòng) seata
nohup ./bin/seata-server.sh -p 8091 -h 127.0.0.1 -m file >nohup.out 2>1 &