1. 程式人生 > 其它 >seata file模式引入springboot專案步驟(不使用註冊中心)

seata file模式引入springboot專案步驟(不使用註冊中心)

seata的官方文件如下

http://seata.io/zh-cn/docs/overview/what-is-seata.html

seata-server的啟動和配置

  1. 從官方下載seata-server安裝包https://github.com/seata/seata/releases,下載完成後解壓該安裝包

    tar -xvf seata-server-1.4.2.tar.gz
  2. 在配置檔案中修改,設定部署模式為file模式

    cd seata-server-1.4.2/conf
    vim file.conf

    配置檔案如下

    ## transaction log store, only used in seata-server
    store {
    ## store mode: file、db、redis
    mode = "file"
    ## rsa decryption public key
    ## transaction log store, only used in seata-server
    store {
    ## store mode: file、db、redis
    mode = "file"
    ## rsa decryption public key
    publicKey = ""
    ## file store property
    file {
    ## store location dir
    dir = "sessionStore"
    # branch session size , if exceeded first try compress lockkey, still exceeded throws exceptions
    maxBranchSessionSize = 16384
    # globe session size , if exceeded throws exceptions
    maxGlobalSessionSize = 512
    # file buffer size , if exceeded allocate new buffer
    fileWriteBufferCacheSize = 16384
    # when recover batch read size
    sessionReloadReadSize = 100
    # async, sync
    flushDiskMode = async
    }

    ## database store property
    db {
    ## the implement of javax.sql.DataSource, such as DruidDataSource(druid)/BasicDataSource(dbcp)/HikariDataSource(hikari) etc.
    datasource = "druid"
    ## mysql/oracle/postgresql/h2/oceanbase etc.
    dbType = "mysql"
    driverClassName = "com.mysql.jdbc.Driver"
    ## if using mysql to store the data, recommend add rewriteBatchedStatements=true in jdbc connection param
    url = "jdbc:mysql://127.0.0.1:3306/seata?rewriteBatchedStatements=true"
    user = "mysql"
    password = "mysql"
    minConn = 5
    maxConn = 100
    globalTable = "global_table"
    branchTable = "branch_table"
    lockTable = "lock_table"
    queryLimit = 100
    maxWait = 5000
    }

    ## redis store property
    redis {
    ## redis mode: single、sentinel
    mode = "single"
    ## single mode property
    single {
    host = "127.0.0.1"
    port = "6379"
    }
    ## sentinel mode property
    sentinel {
    masterName = ""
    ## such as "10.28.235.65:26379,10.28.235.65:26380,10.28.235.65:26381"
    sentinelHosts = ""
    }
    password = ""
    database = "0"
    minConn = 1
    maxConn = 10
    maxTotal = 100
    queryLimit = 100
    }
    }

    單機版將store.mode改為file即可,這裡的db為高可用配置,需要使用mysql資料庫,這裡不做說明

  3. 啟動服務

    cd bin
    nohup sh seata-server.sh &

    這裡由於我伺服器記憶體不夠,於是刪除了官方啟動指令碼中對java虛擬機器的引數設定

    啟動引數如下:

    Usage: sh seata-server.sh(for linux and mac) or cmd seata-server.bat(for windows) [options]
    Options:
    --host, -h
    The host to bind.
    Default: 0.0.0.0
    --port, -p
    The port to listen.
    Default: 8091
    --storeMode, -m
    log store mode : file、db
    Default: file
    --help

    e.g.

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

在springboot專案中引入seata

  1. 在所有需要使用seata的專案的資料庫中引入undolog日誌表,建表語句如下:

    CREATE TABLE `undo_log` (
    `id` bigint NOT NULL AUTO_INCREMENT,
    `branch_id` bigint NOT NULL,
    `xid` varchar(100) NOT NULL,
    `context` varchar(128) NOT NULL,
    `rollback_info` longblob NOT NULL,
    `log_status` int 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=6 DEFAULT CHARSET=utf8mb3
  2. 為專案引入seata的starter

    不知道為什麼直接引入seata-spring-boot-starter會發生包衝突,因此這裡另外引入了seata-all來配合starter

     <dependency>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
<version>1.4.0</version>
<exclusions>
<exclusion>
<artifactId>seata-all</artifactId>
<groupId>io.seata</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-all</artifactId>
<version>1.4.0</version>
</dependency>
  1. yml配置檔案

#====================================Seata Config===============================================
seata:
enabled: true
client:
rm-report-success-enable: true
rm-table-meta-check-enable: false # 自動重新整理快取中的表結構(預設false)
rm-report-retry-count: 5 # 一階段結果上報TC重試次數(預設5)
rm-async-commit-buffer-limit: 10000 # 非同步提交快取佇列長度(預設10000)
rm:
lock:
lock-retry-internal: 10 # 校驗或佔用全域性鎖重試間隔(預設10ms)
lock-retry-times: 30 # 校驗或佔用全域性鎖重試次數(預設30)
lock-retry-policy-branch-rollback-on-conflict: true # 分支事務與其它全域性回滾事務衝突時鎖策略(優先釋放本地鎖讓回滾成功)
tm-commit-retry-count: 3 # 一階段全域性提交結果上報TC重試次數(預設1次,建議大於1)
tm-rollback-retry-count: 3 # 一階段全域性回滾結果上報TC重試次數(預設1次,建議大於1)
undo:
undo-data-validation: true # 二階段回滾映象校驗(預設true開啟)
undo-log-serialization: jackson # undo序列化方式(預設jackson)
undo-log-table: undo_log # 自定義undo表名(預設undo_log)
support:
spring:
datasource-autoproxy: true
service:
vgroup-mapping:
my_test_tx_group: default # TC 叢集(必須與seata-server保持一致)
enable-degrade: false # 降級開關
disable-global-transaction: false # 禁用全域性事務(預設false)
grouplist:
default: 1.15.113.171:8091
transport:
shutdown:
wait: 3
thread-factory:
boss-thread-prefix: NettyBoss
worker-thread-prefix: NettyServerNIOWorker
server-executor-thread-prefix: NettyServerBizHandler
share-boss-worker: false
client-selector-thread-prefix: NettyClientSelector
client-selector-thread-size: 1
client-worker-thread-prefix: NettyClientWorkerThread
type: TCP
server: NIO
heartbeat: true
serialization: seata
compressor: none
enable-client-batch-send-request: true # 客戶端事務訊息請求是否批量合併傳送(預設true)
registry:
file:
name: file.conf
type: file
config:
file:
name: file.conf
type: file
log:
exception-rate: 100

這裡需要重點關注的配置如下

  • seata.service.vgourpmapping

    隨便取個名字,同一個業務中該名稱保持相同

  • seata.service.grouplist

    這裡用來配置連線的seata-server ip與地址