這邊採取SpringCloud-JPA-Seata
來測試
按照:https://github.com/seata/seata-samples/tree/master/springcloud-jpa-seata
一 準備工作
(1)執行SQL
在springcloud-jpa-seata
目錄下有sql/all_in_one.sql
,可直接執行
(2)下載Seata Sever
,並運行
下載地址:https://github.com/seata/seata/releases
這邊下載最新版本:0.7.1
// zip文件
unzip seata-server-xxx.zip
// tar.gz
tar -zxvf seata-server.ta.gz -C seata-server
sh ./bin/seata-server.sh -p 8091 -m file
啓動後如下:
(3)更改項目配置
例如account-service
服務,打開application.properties
按照自己實際的配置更新,比如:
spring.datasource.url=jdbc:mysql://localhost:3306/db_account?useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
(4)啓動服務
啓動account-service
busniess-service
order-service
storage-service
服務
二 測試
(1)無錯誤,成功提交
- 請求訪問
curl http://127.0.0.1:8084/purchase/commit
account_tbl
的id = 1
的money
減少5
storage_tbl
的id = 1
的count
減少1
(2)發生異常,事務回滾
- 請求訪問
curl http://127.0.0.1:8084/purchase/rollback
-
account-service
拋出異常 -
business-service
捕獲異常
seata server
顯示