試一試Seata

安裝Seata

  1. https://github.com/seata/seata/releases下載最新版的安裝包

  2. 解壓後的conf目錄中的兩個重要配置文件

    file.conf:配置存儲方式透傳事務信息的NIO事務組配置鎖配置等信息,file.conf.example文件中有可以參考的樣例。這種文件形式的配置默認對應registry.conf文件內的file方式配置。

    registry.conf:配置服務註冊方式配置讀取方式。registry{}中是註冊中心相關配置,config{}中是配置中心相關配置。

    詳細配置項的含義參考http://seata.io/zh-cn/docs/user/configurations.html

  3. seata啓動

    sh seata-server.sh -p 8091 -h 127.0.0.1 -m db

    -h: 註冊到註冊中心的ip
    -p: Server rpc 監聽端口
    -m: 全局事務會話信息存儲模式,file、db,優先讀取啓動參數
    -n: Server node,多個Server時,需區分各自節點,用於生成不同區間的transactionId,以免衝突
    -e: 多環境配置參考 http://seata.io/en-us/docs/ops/multi-configuration-isolation.html
    

    –storeMode, -m
    log store mode : file、db
    Default: file

demo工程

  1. 工程介紹
    用戶購買商品的業務邏輯。整個業務邏輯由3個微服務提供支持:

    • 倉儲服務:對給定的商品扣除倉儲數量。
    • 訂單服務:根據採購需求創建訂單。
    • 帳戶服務:從用戶帳戶中扣除餘額。

    在官網有比較詳細的介紹 http://seata.io/zh-cn/docs/user/quickstart.html

  2. 下載demo工程 https://github.com/seata/seata-samples

    我是用裏面的springcloud-eureka-feign-mybatis-seata這個工程嘗試的。業務就是倉儲-訂單-賬戶這套系統。涉及的內容也比較全面,包括

    註冊中心:eureka
    服務間調用:feign
    持久層:mybatis
    數據庫:mysql 5.7.22
    Springboot:2.1.7.RELEASE
    Springcloud:Greenwich.SR2
    jdk:1.8
    seata:我用的1.2.0版本

  3. 運行demo

    • 先啓動註冊中心eureka。後啓seata和訂單、庫存、賬戶服務(三個RM)。seata要作爲TC(事務協調者)註冊上來的。

    • 這裏我們的seata是基於db的配置的,要建立數據庫seata, 再建立三個表global_table,branch_table,lock_table,建表sql在https://github.com/seata/seata/blob/develop/script/server/db/mysql.sql

      修改seata/conf目錄下的file.conf文件,指定到我們創建的數據庫。另外自己下載的seata的file.conf

    • undo_log是存儲在業務庫,訂單、庫存、賬戶每個項目的數據庫中都要建立undo_log表,sql在 https://github.com/seata/seata/blob/develop/script/client/at/db/mysql.sql>。每個業務系統都有file.conf和registry.conf文件. registry.conf文件中的registry節點修改type爲eureka,其他不用動,config節點我們默認就是使用file配置的,file指向file.conf。

      file.conf中的service配置中的default.grouplist要指向我們運行seata的ip和端口;vgroupMapping.my_test_tx_group = "default"中的這一項要改成vgroupMapping.fsp_tx_group = “default” , 與application.yml中的spring.cloud.alibaba.seata.tx-service-group: fsp_tx_group對應。這一條配置是作者自定義的一個事務組名稱。

    • 都跑起來沒有報錯的時候,測試接口 http://localhost:8180/order/create,參數

      {
      “userId”:“1”,
      “productId”:“1”,
      “count”:“5”,
      “money”:“10”
      }

      注意把demo中OrderController中的create方法改成@PostMapping,請求參數前面加上@RequestBody,這樣就能正常運行了。作者可能沒來得及調試程序。

  4. 調用接口,觀察中間狀態和結束狀態表格記錄
    AccountServiceImpl中也有Thread.sleep(30*1000);代碼模擬超時,全局事務回滾的情況。把這段代碼的註釋打開,請求接口10秒後失敗,在這10秒內可以看到數據庫每個表的中間狀態,10秒後請求失敗,事務回滾,訂單、庫存、賬戶的表數據恢復,seata的三個表global_table,branch_table,lock_table中本次請求相關的記錄被刪除。

我遇到的問題:

  1. 如果在windows下跑,可能出現這個錯誤,linux上沒有
D:\pkg\seata-server-1.2.0\seata\bin>seata-server.bat -p 8091 -h 127.0.0.1 -m db
Java HotSpot(TM) 64-Bit Server VM warning: Cannot open file D:\pkg\seata-server-
1.2.0\seata\bin\\../logs/seata_gc.log due to No such file or directory

在seata目錄下建logs目錄即可

  1. 內存不夠可以先調小內存seata-server.cmd(或seata-server.sh)中
D:\pkg\seata-server-1.2.0\seata\bin>seata-server.bat -p 8091 -h 127.0.0.1 -m db
Error occurred during initialization of VM
Could not reserve enough space for object heap

調小啓動腳本中的內存設置

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章