Spring Boot整合Seata的過程詳解(AT 模式)
在 Spring Boot 項(xiàng)目中整合 Seata(分布式事務(wù)框架),可以實(shí)現(xiàn)跨服務(wù)、跨數(shù)據(jù)庫(kù)的分布式事務(wù)管理。Seata 提供了多種模式,如 AT、TCC、Saga、XA 等,其中 AT 模式 是最常用的一種,它通過代理數(shù)據(jù)源和全局事務(wù)日志(undo_log)來實(shí)現(xiàn)自動(dòng)回滾。
下面介紹 Spring Boot 整合 Seata(AT 模式) :
?? 前提條件
- 已安裝并啟動(dòng) Seata Server
- 數(shù)據(jù)庫(kù)支持:MySQL / Oracle / PostgreSQL 等
- Spring Boot 項(xiàng)目使用 MyBatis / JPA / JDBC
- 使用 Dubbo 或 Spring Cloud Alibaba 微服務(wù)架構(gòu)
? 一:搭建 Seata Server(TC)
1. 下載 Seata Server
前往 Seata GitHub Releases 下載最新版本。
例如:
wget https://github.com/seata/seata/releases/download/v2.0.0/seata-server-2.0.0.zip
2. 配置registry.conf和file.conf
修改 registry.conf(注冊(cè)中心)
如果你用的是 Nacos 注冊(cè)中心,配置如下:
registry {
type = "nacos"
nacos {
server-addr = "127.0.0.1:8848"
namespace = ""
group = "DEFAULT_GROUP"
}
}
config {
type = "nacos"
nacos {
server-addr = "127.0.0.1:8848"
namespace = ""
group = "SEATA_GROUP"
data-id = "seataServer.properties"
}
}3. 啟動(dòng) Seata Server
cd seata/bin ./seata-server.sh -p 8091 -m file
注意:Windows 用戶執(zhí)行 seata-server.bat
? 二:配置數(shù)據(jù)庫(kù)(undo_log 表)
每個(gè)參與分布式事務(wù)的數(shù)據(jù)庫(kù)都需要?jiǎng)?chuàng)建 undo_log 表:
-- MySQL 示例 CREATE TABLE `undo_log` ( `id` BIGINT(20) NOT NULL AUTO_INCREMENT, `branch_id` BIGINT(20) NOT NULL, `xid` VARCHAR(100) NOT NULL, `context` VARCHAR(128) NOT NULL, `rollback_info` LONGBLOB NOT NULL, `log_status` INT(11) NOT NULL, `log_created` DATETIME NOT NULL, `log_modified` DATETIME NOT NULL, `ext` VARCHAR(100) DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `ux_undo_log` (`xid`, `branch_id`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
? 三:Spring Boot 項(xiàng)目整合 Seata
1. 添加依賴(Maven)
<!-- Spring Boot Starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- MyBatis Plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>
<!-- MySQL 驅(qū)動(dòng) -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- Seata Starter -->
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
</dependency>
<!-- Nacos Discovery(可選) -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>2. 配置 application.yml
server:
port: 8081
spring:
application:
name: order-service
datasource:
url: jdbc:mysql://localhost:3306/order_db?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
# 使用 Seata 數(shù)據(jù)源代理
type: io.seata.rm.datasource.SeataDataSourceProxy
# Seata 配置
seata:
enabled: true
application-id: ${spring.application.name} # 應(yīng)用唯一ID
tx-service-group: my_test_tx_group # 事務(wù)組名(與 TC 配置一致)
service:
vgroup-mapping:
my_test_tx_group: default # 映射事務(wù)組到默認(rèn)集群
grouplist:
default: 127.0.0.1:8091 # TC 地址(僅測(cè)試環(huán)境需要)
config:
type: nacos # 配置中心類型
nacos:
server-addr: 127.0.0.1:8848
group: SEATA_GROUP
data-id: seataServer.properties
registry:
type: nacos # 注冊(cè)中心類型
nacos:
server-addr: 127.0.0.1:88483. 編寫業(yè)務(wù)代碼
Service 層添加 @GlobalTransactional 注解
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.stereotype.Service;
@Service
public class OrderService {
@GlobalTransactional(name = "create-order", rollbackFor = Exception.class)
public void createOrder() throws Exception {
// 調(diào)用庫(kù)存服務(wù)扣減庫(kù)存(RPC)
stockService.decreaseStock();
// 插入訂單
orderMapper.insert(new Order(...));
// 模擬異常觸發(fā)回滾
if (true) {
throw new RuntimeException("模擬失敗");
}
}
}4. 啟動(dòng)類
@SpringBootApplication
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
}? 四:多服務(wù)協(xié)同調(diào)用(微服務(wù)場(chǎng)景)
- 所有服務(wù)都引入
seata-spring-boot-starter - 所有服務(wù)都要配置相同的
tx-service-group - 在 Feign/RPC 調(diào)用時(shí),Seata 會(huì)自動(dòng)傳播 XID,實(shí)現(xiàn)全局事務(wù)控制
? 驗(yàn)證是否生效
- 查看日志中是否有
xid=xxx輸出 - 觸發(fā)異常后,檢查各服務(wù)數(shù)據(jù)庫(kù)是否回滾
- 查看 Seata Server 控制臺(tái)日志
?? 常見問題排查
| 問題 | 解決方案 |
|---|---|
| 無法連接 TC | 檢查 Seata Server 是否啟動(dòng),配置的 IP/端口是否正確 |
| 未找到 undo_log 表 | 檢查數(shù)據(jù)庫(kù)是否已創(chuàng)建該表 |
| 分布式事務(wù)不生效 | 檢查是否開啟 @GlobalTransactional,是否使用 Seata 數(shù)據(jù)源代理 |
| 多個(gè)服務(wù)之間 XID 不一致 | 檢查 Feign/RPC 是否開啟攔截器,或是否遺漏 Seata 配置 |
? 小結(jié)
| 步驟 | 內(nèi)容 |
|---|---|
| 1 | 安裝并啟動(dòng) Seata Server |
| 2 | 創(chuàng)建 undo_log 表 |
| 3 | 引入依賴、配置數(shù)據(jù)源、添加注解 |
| 4 | 多服務(wù)協(xié)調(diào)調(diào)用,驗(yàn)證事務(wù)一致性 |
到此這篇關(guān)于Spring Boot整合Seata的過程詳解(AT 模式)的文章就介紹到這了,更多相關(guān)springboot整合seata內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Mybatis Interceptor 攔截器的實(shí)現(xiàn)
這篇文章主要介紹了Mybatis Interceptor 攔截器的實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-12-12
詳解Java集合中的基本數(shù)據(jù)結(jié)構(gòu)
總有小伙伴讓我總結(jié)一下Java集合中的基本數(shù)據(jù)結(jié)構(gòu)的相關(guān)知識(shí),今天特地整理了本篇文章,文中有非常詳細(xì)的介紹,需要的朋友可以參考下2021-06-06
SpringBoot部署和前端連接問題解決的完整指南(net::ERR_CONNECTION_REFUSED)
在開發(fā)和部署 Spring Boot 應(yīng)用時(shí),可能會(huì)遇到各種問題,例如 JAR 文件無法運(yùn)行、前端無法連接后端服務(wù)等,本文將詳細(xì)總結(jié)這些問題的解決方法,幫助你順利部署和運(yùn)行 Spring Boot 應(yīng)用,需要的朋友可以參考下2025-01-01
詳解 問題:HttpServlet cannot be resolved to a type
這篇文章主要介紹了詳解 問題:HttpServlet cannot be resolved to a type的相關(guān)資料,需要的朋友可以參考下2017-03-03

