天天看點

spring cloud整合seata

1. 下載下傳seata-server

1.從

https://github.com/seata/seata/releases,下載下傳伺服器軟體包

,将其解壓縮。

2. 建表

全局事務過程中,會涉及3塊内容:

  • 全局事務 global_table
  • 分支事務 branch_table
  • 全局鎖 lock_table
-- -------------------------------- The script used when storeMode is 'db' --------------------------------
-- the table to store GlobalSession data
CREATE TABLE IF NOT EXISTS `global_table`
(
    `xid`                       VARCHAR(128) NOT NULL,
    `transaction_id`            BIGINT,
    `status`                    TINYINT      NOT NULL,
    `application_id`            VARCHAR(32),
    `transaction_service_group` VARCHAR(32),
    `transaction_name`          VARCHAR(128),
    `timeout`                   INT,
    `begin_time`                BIGINT,
    `application_data`          VARCHAR(2000),
    `gmt_create`                DATETIME,
    `gmt_modified`              DATETIME,
    PRIMARY KEY (`xid`),
    KEY `idx_gmt_modified_status` (`gmt_modified`, `status`),
    KEY `idx_transaction_id` (`transaction_id`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8;

-- the table to store BranchSession data
CREATE TABLE IF NOT EXISTS `branch_table`
(
    `branch_id`         BIGINT       NOT NULL,
    `xid`               VARCHAR(128) NOT NULL,
    `transaction_id`    BIGINT,
    `resource_group_id` VARCHAR(32),
    `resource_id`       VARCHAR(256),
    `branch_type`       VARCHAR(8),
    `status`            TINYINT,
    `client_id`         VARCHAR(64),
    `application_data`  VARCHAR(2000),
    `gmt_create`        DATETIME(6),
    `gmt_modified`      DATETIME(6),
    PRIMARY KEY (`branch_id`),
    KEY `idx_xid` (`xid`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8;

-- the table to store lock data
CREATE TABLE IF NOT EXISTS `lock_table`
(
    `row_key`        VARCHAR(128) NOT NULL,
    `xid`            VARCHAR(96),
    `transaction_id` BIGINT,
    `branch_id`      BIGINT       NOT NULL,
    `resource_id`    VARCHAR(256),
    `table_name`     VARCHAR(32),
    `pk`             VARCHAR(36),
    `gmt_create`     DATETIME,
    `gmt_modified`   DATETIME,
    PRIMARY KEY (`row_key`),
    KEY `idx_branch_id` (`branch_id`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8;           

### 3. 修改seata-server配置

#### 3.1 配置registry.conf

//注冊中心配置
registry {
# file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
type = "eureka"
eureka {
  serviceUrl = "http://192.xx.xx.xx:8761/eureka"
  application = "fsp_tx" //tc注冊時服務名
  weight = "1"
}

}

//配置中心配置
config {
# file、nacos 、apollo、zk、consul、etcd3
type = "file"
file {
  name = "file.conf"
}
}           

3.2 配置file.conf

## transaction log store, only used in seata-server
store {
  ## store mode: file、db
  mode = "db"

  ## database store property
  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://192.168.173.95:3306/seata-server" //tc的資料庫,可自定義命名,對應就好
    user = "mysql"
    password = "runlion@123"
    minConn = 5
    maxConn = 30
    globalTable = "global_table"
    branchTable = "branch_table"
    lockTable = "lock_table"
    queryLimit = 100
    maxWait = 5000
  }
}           

4. 啟動seata-server 注冊到eureka

  • linux
nohup sh seata-server.sh -h xx.xx.xx.xx -p 8091 -m db -n 1 &           
  • windows
seata-server.bat           
  • 這裡是以nohup的方式背景啟動,參數可選:
-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
高可用部署可參考:https://seata.io/zh-cn/docs/ops/deploy-ha.html           

5. 服務引入seata依賴

<!--seata-->
<dependency>
  <groupId>com.alibaba.cloud</groupId>
  <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
  <version>2.2.0.RELEASE</version>
//如果内嵌最新版本starter,我們不用排除再引入  
  <exclusions>
    <exclusion>
      <groupId>io.seata</groupId>
      <artifactId>seata-spring-boot-starter</artifactId>
    </exclusion>
  </exclusions>
</dependency>
<dependency>
  <groupId>io.seata</groupId>
  <artifactId>seata-spring-boot-starter</artifactId>
  <version>1.2.0</version>
</dependency>           

6. 服務配置檔案

# -----------seata--------------
seata:
    enabled: true
    application-id: order-server #服務名
    tx-service-group: default # default是自定義的事務分組名稱
    enable-auto-data-source-proxy: true # 啟用自動資料源代理
    use-jdk-proxy: false
    #    excludes-for-auto-proxying:
    #    client:
    #        rm:
    #            async-commit-buffer-limit: 1000
    #            report-retry-count: 5
    #            table-meta-check-enable: false
    #            report-success-enable: false
    #            saga-branch-register-enable: false
    #            lock:
    #                retry-interval: 10
    #                retry-times: 30
    #                retry-policy-branch-rollback-on-conflict: true
    #        tm:
    #            commit-retry-count: 5
    #            rollback-retry-count: 5
    #        undo:
    #            data-validation: true
    #            log-serialization: jackson
    #            log-table: undo_log
    #        log:
    #            exceptionRate: 100
    service:
        vgroup-mapping:
            default: seata-server # default是自定義的事務分組名稱,fsp_tx是tc注冊到注冊中心的服務名稱
        #        grouplist:
        #            default: 127.0.0.1:8091 #     僅注冊中心為file時使用
        enable-degrade: false # 是否啟用降級
        disable-global-transaction: false # 是否禁用全局事務
    #    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
    #            worker-thread-size: default
    #            boss-thread-size: 1
    #        type: TCP
    #        server: NIO
    #        heartbeat: true
    #        serialization: seata
    #        compressor: none
    #        enable-client-batch-send-request: true
    config:
        type: file # 配置中心為file模式
    #        consul:
    #            server-addr: 127.0.0.1:8500
    #        apollo:
    #            apollo-meta: http://192.168.1.204:8801
    #            app-id: seata-server
    #            namespace: application
    #        etcd3:
    #            server-addr: http://localhost:2379
    #        nacos:
    #            namespace:
    #            serverAddr: localhost
    #            group: SEATA_GROUP
    #            userName: ""
    #            password: ""
    #        zk:
    #            server-addr: 127.0.0.1:2181
    #            session-timeout: 6000
    #            connect-timeout: 2000
    #            username: ""
    #            password: ""
    registry:
        type: eureka # 注冊中心為eureka
        eureka:
            weight: 1
            service-url: http://127.0.0.1:50024/eureka # 注冊中心位址
#        consul:
#            server-addr: 127.0.0.1:8500
#        etcd3:
#            serverAddr: http://localhost:2379
#        nacos:
#            application: seata-server
#            server-addr: localhost
#            namespace:
#            userName: ""
#            password: ""
#        redis:
#            server-addr: localhost:6379
#            db: 0
#            password:
#            timeout: 0
#        sofa:
#            server-addr: 127.0.0.1:9603
#            region: DEFAULT_ZONE
#            datacenter: DefaultDataCenter
#            group: SEATA_GROUP
#            addressWaitTime: 3000
#            application: default
#        zk:
#            server-addr: 127.0.0.1:2181
#            session-timeout: 6000
#            connect-timeout: 2000
#            username: ""
#            password: ""

# -----------seata--------------           

7. 使用

@GlobalTransaction 全局事務注解