seata1.2.0搭建

選擇版本說明

Snipaste_2020-07-27_19-22-11.png

在LINUX下配置

nacos自己參考搭建

1. 下載服務(wù)

首先訪問:https://seata.io/zh-cn/blog/download.html

下載我們需要使用的seata1.2服務(wù)

2. 創(chuàng)建表和數(shù)據(jù)庫

  1. 在你的參與全局事務(wù)的數(shù)據(jù)庫中加入undo_log這張表

表的查找:

在官網(wǎng)下查找資源,下載官方配置資源

Snipaste_2020-07-28_10-22-00.png

根據(jù)下載到的資源下查找相對應(yīng)的表sql導(dǎo)入

Snipaste_2020-07-28_10-22-57.png

附上:

-- for AT mode you must to init this sql for you business database. the seata server not need it.
CREATE TABLE IF NOT EXISTS `undo_log`
(
    `id`            BIGINT(20)   NOT NULL AUTO_INCREMENT COMMENT 'increment id',
    `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     NOT NULL COMMENT 'create datetime',
    `log_modified`  DATETIME     NOT NULL COMMENT 'modify datetime',
    PRIMARY KEY (`id`),
    UNIQUE KEY `ux_undo_log` (`xid`, `branch_id`)
) ENGINE = InnoDB
  AUTO_INCREMENT = 1
  DEFAULT CHARSET = utf8 COMMENT ='AT transaction mode undo table';
  1. 在你的mysql數(shù)據(jù)庫中創(chuàng)建名為seata的庫,并使用以下下sql

根據(jù)下載到的資源下查找相對應(yīng)的表sql導(dǎo)入

Snipaste_2020-07-28_10-31-24.png

附上:

-- -------------------------------- 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. 修改config.txt文件

根據(jù)下載到的資源下查找相對應(yīng)的文件,在以往版本中為nacos-config.txt

Snipaste_2020-07-28_10-35-43.png

修改后:

service.vgroupMapping.nacos_seata_tx_group=default
service.default.grouplist=127.0.0.1:8091
service.enableDegrade=false
service.disableGlobalTransaction=false
store.mode=db
store.db.datasource=druid
store.db.dbType=mysql
store.db.driverClassName=com.mysql.jdbc.Driver
store.db.url=jdbc:mysql://127.0.0.1:3306/seata?useUnicode=true
store.db.user=root
store.db.password=root
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
  • 注意vgroupMapping在舊版中使用的是vgroup_mapping,nacos_seata_tx_group自行取名,在下文中需要配置

  • 數(shù)據(jù)庫地址,用戶名密碼,如果是mysql8的需要修改驅(qū)動(dòng)名稱并加入相對應(yīng)的驅(qū)動(dòng)包

運(yùn)行并將資源導(dǎo)入nacos

[root@localhost conf]# ./nacos-config.sh
-bash: ./nacos-config.sh: Permission denied
[root@localhost conf]# chmod 777 nacos-config.sh
[root@localhost conf]# ./nacos-config.sh
set nacosAddr=localhost:8848
set group=SEATA_GROUP
Set service.vgroupMapping.nacos_seata_tx_group=default successfully 
Set service.default.grouplist=127.0.0.1:8091 successfully 
Set service.enableDegrade=false successfully 
Set service.disableGlobalTransaction=false successfully 
Set store.mode=db successfully 
Set store.db.datasource=druid successfully 
Set store.db.dbType=mysql successfully 
Set store.db.driverClassName=com.mysql.jdbc.Driver successfully 
Set store.db.url=jdbc:mysql://127.0.0.1:3306/seata?useUnicode=true successfully 
Set store.db.user=root successfully 
Set store.db.password=57328242 successfully 
Set store.db.minConn=5 successfully 
Set store.db.maxConn=30 successfully 
Set store.db.globalTable=global_table successfully 
Set store.db.branchTable=branch_table successfully 
Set store.db.queryLimit=100 successfully 
Set store.db.lockTable=lock_table successfully 
Set store.db.maxWait=5000 successfully 
=========================================================================
 Complete initialization parameters,  total-count:18 ,  failure-count:0 
=========================================================================
 Init nacos config finished, please start seata-server.

導(dǎo)入成功后:查看nacos出現(xiàn)相關(guān)的配置

Snipaste_2020-07-28_10-58-22.png

創(chuàng)建dev命名空間

Snipaste_2020-07-28_12-13-11.png

將public中的配置信息導(dǎo)入到dev中

Snipaste_2020-07-28_12-14-32.png

4. 在idea引入相對應(yīng)的依賴

springcloud:

 <!--seata-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-seata</artifactId>
            <version>2.2.0.RELEASE</version>
            <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>

5. 更改server中的registry.conf

registry {
  # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
  type = "nacos"

  nacos {
    application = "seata-server"
    serverAddr = "localhost"
    namespace = "1ef39358-af4a-438b-8cf0-1ea1fb31de5f"  #導(dǎo)入dev命名空間的id
    cluster = "default"
    username = "nacos"
    password = "nacos"
}   
}

config {
  # file、nacos 、apollo、zk、consul、etcd3
  type = "nacos"

  nacos {
    serverAddr = "localhost"
    namespace = "1ef39358-af4a-438b-8cf0-1ea1fb31de5f" #導(dǎo)入dev命名空間的id
    group = "SEATA_GROUP"
    username = "nacos"
    password = "nacos"
  }
}

6. 進(jìn)入bin目錄下啟動(dòng)seata

sh seata-server.sh -p 8091 -h 192.168.235.130

官網(wǎng)提供相關(guān)命令

  • 命令啟動(dòng): seata-server.sh -h 127.0.0.1 -p 8091 -m db -n 1 -e test
  • 開啟多個(gè):seata-server.sh -h 127.0.0.1 -p 8092 -m db -n 1 -e test(直接在另外的窗口修改端口號注冊)


    Snipaste_2020-07-28_13-04-40.png
  • 官網(wǎng)提供相關(guān)命令
    -h: 注冊到注冊中心的ip
    -p: Server rpc 監(jiān)聽端口
    -m: 全局事務(wù)會話信息存儲模式,file、db、redis,優(yōu)先讀取啟動(dòng)參數(shù) (Seata-Server 1.3及以上版本支持redis)
    -n: Server node,多個(gè)Server時(shí),需區(qū)分各自節(jié)點(diǎn),用于生成不同區(qū)間的transactionId,以免沖突
    -e: 多環(huán)境配置參考 http://seata.io/en-us/docs/ops/multi-configuration-isolation.html

7. 創(chuàng)建application.yml

根據(jù)下載到的資源下查找相對應(yīng)的文件,并修改(除必要配置,默認(rèn)即可)

Snipaste_2020-07-28_11-10-05.png
seata:
  enabled: true
  application-id: applicationName
  tx-service-group: nacos_seata_tx_group    #與上文配置相對應(yīng)
  config:
    type: nacos
    nacos:
      namespace: 1ef39358-af4a-438b-8cf0-1ea1fb31de5f #與上文對應(yīng)
      serverAddr: 192.168.235.130:8848 #nacos的地址和端口
      group: SEATA_GROUP
      userName: "nacos"
      password: "nacos"
  registry:
    type: nacos
    nacos:
      application: seata-server
      server-addr: 192.168.235.130:8848
      namespace: 1ef39358-af4a-438b-8cf0-1ea1fb31de5f #與上文對應(yīng)
      userName: "nacos"
      password: "nacos"

8. 啟動(dòng)微服務(wù)

druid詳細(xì)配置信息缺失

Snipaste_2020-07-28_12-17-19.png

加入druid配置信息

yaml完整配置

spring:
  application:
    name: seata-order-service
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.235.130:8848
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    url: jdbc:mysql://192.168.235.130:3306/seata_order?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: root
    password: 57328242
    driver-class-name: org.gjt.mm.mysql.Driver
    druid: #druid配置
      filters: stat
      initialSize: 2
      maxActive: 300
      maxWait: 60000
      timeBetweenEvictionRunsMillis: 60000
      minEvictableIdleTimeMillis: 300000
      validationQuery: SELECT 1
      testWhileIdle: true
      testOnBorrow: false
      testOnReturn: false
      poolPreparedStatements: false
      maxPoolPreparedStatementPerConnectionSize: 200

seata:
  enabled: true
  application-id: applicationName
  tx-service-group: nacos_seata_tx_group
  config:
    type: nacos
    nacos:
      namespace: 1ef39358-af4a-438b-8cf0-1ea1fb31de5f
      serverAddr: 192.168.235.130:8848
      group: SEATA_GROUP
      userName: "nacos"
      password: "nacos"
  registry:
    type: nacos
    nacos:
      application: seata-server
      server-addr: 192.168.235.130:8848
      namespace: 1ef39358-af4a-438b-8cf0-1ea1fb31de5f
      userName: "nacos"
      password: "nacos"

9. 其他

業(yè)務(wù)系統(tǒng)集成Client

步驟一:添加seata依賴(建議單選)

  • 依賴seata-all

  • 依賴seata-spring-boot-starter,支持yml、properties配置(.conf可刪除),內(nèi)部已依賴seata-all

  • 依賴spring-cloud-alibaba-seata,內(nèi)部集成了seata,并實(shí)現(xiàn)了xid傳遞

步驟二:undo_log建表、配置參數(shù)

步驟三:數(shù)據(jù)源代理(不支持自動(dòng)和手動(dòng)配置并存,不支持XA數(shù)據(jù)源自動(dòng)代理)

  • 0.9.0版本開始seata支持自動(dòng)代理數(shù)據(jù)源
    1.1.0: seata-all取消屬性配置,改由注解@EnableAutoDataSourceProxy開啟,并可選擇jdk proxy或者cglib proxy
    1.0.0: client.support.spring.datasource.autoproxy=true
    0.9.0: support.spring.datasource.autoproxy=true
  • 手動(dòng)配置可參考下方的例子
 @Primary
    @Bean("dataSource")
    public DataSourceProxy dataSource(DataSource druidDataSource) {
        //AT 代理 二選一
        return new DataSourceProxy(druidDataSource);
        //XA 代理
        return new DataSourceProxyXA(druidDataSource)
    }

步驟四:初始化GlobalTransactionScanner

  • 手動(dòng)
   @Bean
       public GlobalTransactionScanner globalTransactionScanner() {
           String applicationName = this.applicationContext.getEnvironment().getProperty("spring.application.name");
           String txServiceGroup = this.seataProperties.getTxServiceGroup();
           if (StringUtils.isEmpty(txServiceGroup)) {
               txServiceGroup = applicationName + "-fescar-service-group";
               this.seataProperties.setTxServiceGroup(txServiceGroup);
           }
   
           return new GlobalTransactionScanner(applicationName, txServiceGroup);
       }
  • 自動(dòng),引入seata-spring-boot-starter、spring-cloud-starter-alibaba-seata等jar

步驟五:實(shí)現(xiàn)xid跨服務(wù)傳遞

  • 手動(dòng) 參考源碼integration文件夾下的各種rpc實(shí)現(xiàn) module

  • 自動(dòng) springCloud用戶可以引入spring-cloud-starter-alibaba-seata,內(nèi)部已經(jīng)實(shí)現(xiàn)xid傳遞

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容