springboot整合seata1.4.0【TODO】


一、簡介

  • 本文主要介紹分布式事務框架seata(seata版本1.4.0)的安裝以及springboot與seata的整合的事務回滾案例;
  • seata的官方文檔路徑: http://seata.io/zh-cn/
  • 本文介紹的方式是AT模式,采用的中間件是nacos,存儲類型為db模式;
  • 本文默認你已會nacos的安裝,如果不會請移步nacos的官方文檔進行搭建;

二、seata服務端安裝

2.1 seata-server服務端

2.1.1 下載seata-server服務端

  • 本文介紹的是window版本的安裝;
  • 當前最新版本為1.4.0,選擇binary類型的點擊下載即可;
  • 下載路徑: http://seata.io/zh-cn/blog/download.html
  • 下載下來進行解壓,目錄結構如下:

2.1.2 添加log文件

  • 在根目錄創建logs文件夾,里面創建一個seata_gc.log的文件,結構如下:

2.2 初始化數據庫

2.2.1 seata庫數據表

  • 數據庫名稱就叫seata;

  • branch_table表
    CREATE TABLE branch_table (
    branch_id bigint(20) NOT NULL,
    xid varchar(128) NOT NULL,
    transaction_id bigint(20) DEFAULT NULL,
    resource_group_id varchar(32) DEFAULT NULL,
    resource_id varchar(256) DEFAULT NULL,
    branch_type varchar(8) DEFAULT NULL,
    status tinyint(4) DEFAULT NULL,
    client_id varchar(64) DEFAULT NULL,
    application_data varchar(2000) DEFAULT NULL,
    gmt_create datetime(6) DEFAULT NULL,
    gmt_modified datetime(6) DEFAULT NULL,
    PRIMARY KEY (branch_id),
    KEY idx_xid (xid)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

  • global_table表
    CREATE TABLE global_table (
    xid varchar(128) NOT NULL,
    transaction_id bigint(20) DEFAULT NULL,
    status tinyint(4) NOT NULL,
    application_id varchar(32) DEFAULT NULL,
    transaction_service_group varchar(32) DEFAULT NULL,
    transaction_name varchar(128) DEFAULT NULL,
    timeout int(11) DEFAULT NULL,
    begin_time bigint(20) DEFAULT NULL,
    application_data varchar(2000) DEFAULT NULL,
    gmt_create datetime DEFAULT NULL,
    gmt_modified datetime DEFAULT NULL,
    PRIMARY KEY (xid),
    KEY idx_gmt_modified_status (gmt_modified,status),
    KEY idx_transaction_id (transaction_id)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

  • lock_table表
    CREATE TABLE lock_table (
    row_key varchar(128) NOT NULL,
    xid varchar(96) DEFAULT NULL,
    transaction_id bigint(20) DEFAULT NULL,
    branch_id bigint(20) NOT NULL,
    resource_id varchar(256) DEFAULT NULL,
    table_name varchar(32) DEFAULT NULL,
    pk varchar(36) DEFAULT NULL,
    gmt_create datetime DEFAULT NULL,
    gmt_modified datetime DEFAULT NULL,
    PRIMARY KEY (row_key),
    KEY idx_branch_id (branch_id)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

2.2.2 業務庫表

  • seata需要在每個業務庫添加一張undo_log表
  • undo_log表
    CREATE TABLE undo_log (
    branch_id bigint(20) NOT NULL COMMENT 'branch transaction id',
    xid varchar(100) NOT NULL COMMENT 'global transaction id',
    context varchar(128) NOT NULL COMMENT 'undo_log context,such as serialization',
    rollback_info longblob NOT NULL COMMENT 'rollback info',
    log_status int(11) NOT NULL COMMENT '0:normal status,1:defense status',
    log_created datetime(6) NOT NULL COMMENT 'create datetime',
    log_modified datetime(6) NOT NULL COMMENT 'modify datetime',
    UNIQUE KEY ux_undo_log (xid,branch_id)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='AT transaction mode undo table';

2.3 修改file.conf文件

2.3.1 信息

  • 只需要修改模式為db和數據庫鏈接信息即可,如下圖:

2.3.2 修改內容

  • store.mode = db;
  • store.db.url = jdbc:mysql://ip:port/seata;
  • store.db.user = 賬號
  • store.db.password = 密碼

2.4 修改registry.conf文件

2.4.1 信息

  • 只需要修改注冊類型和配置類型均為nacos和nacos的相關信息即可

2.4.2 修改內容

  • registry.type = nacos

  • registry.nacos.application = seata-server, 默認服務端的名稱

  • registry.nacos.serverAddr = nacos服務器的8848端口地址

  • registry.nacos.group = SEATA_GROUP, 默認seata的組

  • registry.nacos.namespace = seata, 默認seata的配置在nacos的命名空間

  • registry.nacos.cluster = default, 采取默認集群即可

  • registry.nacos.username = nacos的用戶名

  • registry.nacos.password = naocs的密碼

  • config.type = nacos

  • config.nacos.namespace = seata, 默認seata的配置在nacos的命名空間

  • config.nacos.group = SEATA_GROUP, 默認seata的組

  • config.nacos.username = nacos的用戶名

  • config.nacos.password = naocs的密碼

2.5 修改context.txt文件

2.5.1 context.txt文件內容

  • 在/seata根目錄下創建config.txt文件,內容如下:
    transport.type=TCP
    transport.server=NIO
    transport.heartbeat=true
    transport.enableClientBatchSendRequest=false
    transport.threadFactory.bossThreadPrefix=NettyBoss
    transport.threadFactory.workerThreadPrefix=NettyServerNIOWorker
    transport.threadFactory.serverExecutorThreadPrefix=NettyServerBizHandler
    transport.threadFactory.shareBossWorker=false
    transport.threadFactory.clientSelectorThreadPrefix=NettyClientSelector
    transport.threadFactory.clientSelectorThreadSize=1
    transport.threadFactory.clientWorkerThreadPrefix=NettyClientWorkerThread
    transport.threadFactory.bossThreadSize=1
    transport.threadFactory.workerThreadSize=default
    transport.shutdown.wait=3
    service.vgroupMapping.my_test_tx_group=default
    service.default.grouplist=127.0.0.1:8091
    service.enableDegrade=false
    service.disableGlobalTransaction=false
    client.rm.asyncCommitBufferLimit=10000
    client.rm.lock.retryInterval=10
    client.rm.lock.retryTimes=30
    client.rm.lock.retryPolicyBranchRollbackOnConflict=true
    client.rm.reportRetryCount=5
    client.rm.tableMetaCheckEnable=false
    client.rm.tableMetaCheckerInterval=60000
    client.rm.sqlParserType=druid
    client.rm.reportSuccessEnable=false
    client.rm.sagaBranchRegisterEnable=false
    client.tm.commitRetryCount=5
    client.tm.rollbackRetryCount=5
    client.tm.defaultGlobalTransactionTimeout=60000
    client.tm.degradeCheck=false
    client.tm.degradeCheckAllowTimes=10
    client.tm.degradeCheckPeriod=2000
    store.mode=db
    store.publicKey=
    store.file.dir=file_store/data
    store.file.maxBranchSessionSize=16384
    store.file.maxGlobalSessionSize=512
    store.file.fileWriteBufferCacheSize=16384
    store.file.flushDiskMode=async
    store.file.sessionReloadReadSize=100
    store.db.datasource=druid
    store.db.dbType=mysql
    store.db.driverClassName=com.mysql.jdbc.Driver
    store.db.url=jdbc:mysql://IP:poert/seata?useUnicode=true&rewriteBatchedStatements=true
    store.db.user=root
    store.db.password=123456
    store.db.minConn=5
    store.db.maxConn=30
    store.db.globalTable=global_table
    store.db.branchTable=branch_table
    store.db.queryLimit=100
    store.db.lockTable=lock_table
    store.db.maxWait=5000
    store.redis.mode=single
    store.redis.single.host=127.0.0.1
    store.redis.single.port=6379
    store.redis.maxConn=10
    store.redis.minConn=1
    store.redis.maxTotal=100
    store.redis.database=0
    store.redis.password=
    store.redis.queryLimit=100
    server.recovery.committingRetryPeriod=1000
    server.recovery.asynCommittingRetryPeriod=1000
    server.recovery.rollbackingRetryPeriod=1000
    server.recovery.timeoutRetryPeriod=1000
    server.maxCommitRetryTimeout=-1
    server.maxRollbackRetryTimeout=-1
    server.rollbackRetryTimeoutUnlockEnable=false
    client.undo.dataValidation=true
    client.undo.logSerialization=jackson
    client.undo.onlyCareUpdateColumns=true
    server.undo.logSaveDays=7
    server.undo.logDeletePeriod=86400000
    client.undo.logTable=undo_log
    client.undo.compress.enable=true
    client.undo.compress.type=zip
    client.undo.compress.threshold=64k
    log.exceptionRate=100
    transport.serialization=seata
    transport.compressor=none
    metrics.enabled=false
    metrics.registryType=compact
    metrics.exporterList=prometheus
    metrics.exporterPrometheusPort=9898

2.5.2 修改內容

  • service.vgroupMapping.my_test_tx_group=default
  • store.mode=db
  • store.db.url=jdbc:mysql://IP:PORT/seata?useUnicode=true&rewriteBatchedStatements=true
  • store.db.user=root
  • store.db.password=123456

2.5.3 添加nacos-config.sh文件

  • 在seata/conf目錄下添加nacos-config.sh文件,內容如下:
    待補充

2.5.4 將context.txt文件內容上傳到nacos

  • 目錄結構如下:

  • 在conf文件下執行命令上傳配置文件到nacos

  • 此時可以看到nacos上有對應的配置文件信息

2.6 啟動服務端seata-server

2.6.1 腳本啟動

  • 執行/seata/bin/seata-server.bat命令,看到如下日志即是啟動成功;

  • 同時可以看到nacos上已經注冊成功

四、代碼整合

4.1 POM依賴

  • seata依賴
    <dependency> <groupId>io.seata</groupId> <artifactId>seata-spring-boot-starter</artifactId> <version>1.4.0</version> </dependency>

4.2 配置文件

  • bootstrap.yml內容如下:
    `# seata
    seata:
    enabled: true
    application-id: ${spring.application.name}

    是否開啟數據源自動代理 如果不開啟設置為false

    enable-auto-data-source-proxy: true

    需要和config.txt上傳到nacos上的service.vgroupMapping.my_test_tx_group保持一致

    tx-service-group: my_test_tx_group
    service:
    vgroup-mapping:
    my_test_tx_group: default
    disable-global-transaction: false

    registry

    registry:
    type: nacos
    nacos:
    application: seata-server
    server-addr: xxx:8848
    group: "SEATA_GROUP"
    namespace: "seed-local"
    username: "xxx"
    password: "xxx"
    config:
    type: nacos
    nacos:
    server-addr: xxx:8848
    namespace: "seed-local"
    group: "SEATA_GROUP"
    username: "xxx"
    password: "xxx"`

五、回滾案例


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM