Seata框架實戰
- Seata集成實戰
- Seata須知
- Seata與SpringBoot、Cloud集成
- 1. 下載seata1.2.0
- 2. 創建資料庫
- 3.進入到重要環節: 修改seata-server的配置
- 3.1找到registry.conf并修改
- 3.2 修改file.conf
- 4.修改專案檔案集成seata功能
- 4.1修改pom檔案 加入seata依賴
- 4.2修改專案組態檔
- 5.使用注解@GlobalTransactional開啟分布式事務
- 測驗結果
- 1.測驗事務能否正確回滾(Rollback)
- 3.1 開啟所有專案
- 3.2 開啟seata服務器
- 3.3 在訂單介面里制造錯誤
- 3.4 測驗介面
- 1.測驗事務能否正確提交(Commit)
不了解分布式事務的 看上篇文章點擊這里
Seata集成實戰
Seata須知
下面進入實戰,環境: springBoot2.2.2 + springCloud Hoxton.SR1 + seata1.2.0 + Mysql5.7
首先我們要明白seata中的幾個術語:

我們來想象一個場景,現在有一個微服務架構的電商系統,其中包含了訂單服務、庫存服務
他們分別部署在不同的服務器上,連接的也是不同的資料庫,(以下的操作不考慮復雜情況)
現在用戶對商品進行購買操作,我們要做兩件事:
我們可以想到,訂單專案和庫存專案 每一個都是RM(資源管理器)
因為他們驅動著自己本地的分支事務提交或者回滾
如圖:

·
那么TM是誰?
如果我們還有一個服務 用來專門處理業務,這個服務中會呼叫 訂單服務,庫存服務,那么他就應該是TM
因為這個業務服務,需要開始全域事務,

那么誰又是TC(事務協調者)
其實就是Seata服務,那么一會我們會開啟seata服務,他就是TC
Seata與SpringBoot、Cloud集成
1. 下載seata1.2.0

2. 創建資料庫
這里注意:我們需要創建什么 這里的sql我會在后邊粘貼,現在只需要知道創建什么即可,
a.創建單獨的資料庫(資料庫名隨意),用來記錄分支資訊等等

b.在每個業務資料庫中都要創建undo_log表(用于記錄回滾操作的)
如圖:訂單服務連接distribute_order資料庫,則需要創建一張undo_log , 庫存服務連接的是distribute_store 也是如此

- 撰寫order訂單服務,store庫存服務
撰寫程序略,我們只需要知道對資料庫進行了什么操作即可
訂單服務:連接distribute_order庫,會插入一條記錄進order表,
庫存服務:連接distribute_store庫,會根據購買的商品id,針對商品進行庫存削減,
這里只貼出關鍵代碼:
訂單服務:
<mapper namespace="com.hzyc.springcloud.mapper.OrderMapper">
<insert id="insert" parameterType="Order">
insert into `order`(onum) values (#{onum})
</insert>
</mapper>
庫存服務:
<mapper namespace="com.hzyc.springcloud.mapper.GoodMapper">
<update id="update" parameterType="int">
update good set count = count - 1 where id = #{id}
</update>
</mapper>
3.進入到重要環節: 修改seata-server的配置
3.1找到registry.conf并修改

我們都知道微服務是有注冊中心的(注冊中心不懂說明不會微服務…),我們現在需要把seata服務注冊到中心中
注冊中心我用的是eureka,如圖

所以我們修改registry.conf 主要就是type=“eureka” , 以及eureka的地址別寫錯了,
registry {
# file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
type = "eureka"
nacos {
application = "seata-server"
serverAddr = "localhost"
namespace = ""
cluster = "default"
username = ""
password = ""
}
eureka {
serviceUrl = "http://localhost:8761/eureka/"
application = "seata-server"
weight = "1"
}
}
config {
# file、nacos 、apollo、zk、consul、etcd3
type = "file"
file {
name = "file.conf"
}
}
3.2 修改file.conf
這個檔案主要是配置seata-server連接的資料庫,還記得我們在第二步創建的資料庫嗎
這里主要就是配置service模塊 、和下面的db模塊
#這里手動加入service模塊
service {
#transaction service group mapping
#修改,可不改,my_test_tx_group隨便起名字,
vgroup_mapping.my_test_tx_group = "default"
#only support when registry.type=file, please don't set multiple addresses
# 此服務的地址
default.grouplist = "127.0.0.1:8091"
#disable seata
disableGlobalTransaction = false
}
## transaction log store, only used in seata-server
store {
## store mode: file、db
mode = "db"
## 資料庫連接的位置 重要
db {
## the implement of javax.sql.DataSource, such as DruidDataSource(druid)/BasicDataSource(dbcp) etc.
datasource = "druid"
## mysql/oracle/postgresql/h2/oceanbase etc.
dbType = "mysql"
driverClassName = "com.mysql.jdbc.Driver"
url = "jdbc:mysql://localhost:3306/seata"
user = "root"
password = "mysql"
minConn = 5
maxConn = 30
globalTable = "global_table"
branchTable = "branch_table"
lockTable = "lock_table"
queryLimit = 100
maxWait = 5000
}
}
4.修改專案檔案集成seata功能
這一步主要是讓我們的order服務,store服務、以及我們的業務服務,能夠使用分布式事務
4.1修改pom檔案 加入seata依賴
只要涉及分布式事務的專案,都加上 ! !
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
4.2修改專案組態檔
這里大家可以修改properties檔案/yml組態檔,加入以下的配置,當然yml和properties寫法有些區別這里不多贅述,大家都懂
##############################[seata配置]###################################################
#是否使用分布式事物
seata:
enabled: true
application-id: seata-bus
tx-service-group: my_test_tx_group
enable-auto-data-source-proxy: true
service:
vgroup-mapping.my_test_tx_group: default
grouplist.default: localhost:8091
##############################[seata配置]###################################################
5.使用注解@GlobalTransactional開啟分布式事務
這里呼叫了庫存服務,又呼叫了訂單服務
這是service業務層:
@GlobalTransactional
public String testGlobalTx(){
System.out.println("事務XID="+RootContext.getXID());
String url2 = "http://seata-store/update?id=1";
String result2 = restTemplate.getForObject(url2, String.class);
//如何消費provider的服務,重點問題
String url = "http://seata-order/insert?onum=shaoshuai";
String result = restTemplate.getForObject(url, String.class);
System.out.println("seata=============開始全域事務~~~");
return result;
}
順便撰寫下controller
@RestController
public class TestController {
@Autowired
TestService tv;
@RequestMapping("/globalTx")
public String select() {
return tv.testGlobalTx();
}
}
測驗結果
我們先看下整體的專案結構

1.測驗事務能否正確回滾(Rollback)
3.1 開啟所有專案
eureka-server(注冊中心),開啟springcloud-seata(業務服務)、springcloud-order(訂單服務)、springcloud-store(庫存服務)

3.2 開啟seata服務器
找到seata/bin目錄下的seata-server.bat 開啟

3.3 在訂單介面里制造錯誤

3.4 測驗介面
我們剛才在訂單介面中制造一個錯誤,是為了觀察整個事務是否能正確回滾
如果庫存依舊減少: 事務沒有正確回滾,
庫存沒有減少: 事務可能回滾 / 事務沒有執行 (我們需要觀察seata的日志)
先來看一下資料庫中的資料情況 訂單表

庫存表 300個

測驗介面訪問
http://localhost:10005/globalTx

報錯了,沒錯 by / zero就是我們訂單服務的錯誤
我們觀察store服務執行了沒,看日志

其實是執行了的,我們再看資料庫庫存是否削減

沒有削減,那么究竟是事務根本就沒有生效 還是 事務已經生效了呢,我們觀察seata服務器日志

其實已經rollback了,所以是沒問題的,
到這文章就已經結束了,接下來是成功測驗環節,
1.測驗事務能否正確提交(Commit)
接下來把訂單介面的錯誤取消,試試,

訪問介面:
http://localhost:10005/globalTx

看資料庫


再看seata日志

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/239655.html
標籤:其他
上一篇:MySQL優化建議
下一篇:springcloud集成seata,nacos,sharding-jdbc,mybatis-plus,sentinel,gateway實踐
