天天看点

seata1.4.0+nacos+springboot2+feign案例1、下载安装seata-server 1.4.02、配置seata事务客户端

1、下载安装seata-server 1.4.0

下载地址:https://github.com/seata/seata/releases

本例下载的为:1.4.0

修改conf/file.conf 本例为db

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

  ## 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"
    url = "jdbc:mysql://127.0.0.1:3306/seata"
    user = "mysql"
    password = "mysql"
    minConn = 5
    maxConn = 100
    globalTable = "global_table"
    branchTable = "branch_table"
    lockTable = "lock_table"
    queryLimit = 100
    maxWait = 5000
  }

}
           

seata服务端sql脚本  https://github.com/seata/seata/blob/v1.4.0/script/server/db/mysql.sql

-- -------------------------------- 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;
           

修改conf/registry.conf 本例为nacos

registry {
  # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
  type = "nacos"
  loadBalance = "RandomLoadBalance"
  loadBalanceVirtualNodes = 10

  nacos {
    application = "seata-server"
    serverAddr = "127.0.0.1:8848"
    group = "SEATA_GROUP"
    namespace = ""
    cluster = "default"
    username = ""
    password = ""
  }
}

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

  nacos {
    serverAddr = "127.0.0.1:8848"
    namespace = ""
    group = "SEATA_GROUP"
    username = ""
    password = ""
  }
}
           

下载config.txt和nacos-config.sh

https://github.com/seata/seata/blob/v1.4.0/script/config-center/config.txt

https://github.com/seata/seata/blob/v1.4.0/script/config-center/nacos/nacos-config.sh

修改config.txt内数据库地址

然后将nacos-config.sh与config.txt置于同一路径

执行命令nacos  最后的ip为nacos地址

sh nacos-config.sh -h 127.0.0.1 -p 8848 -g SEATA_GROUP
           

2、配置seata事务客户端

配置依赖

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

配置seata配置

seata:
  enabled: true
  # 事务群组(可以每个应用独立取名,也可以使用相同的名字)
  tx-service-group: tx_group_test
  enable-auto-data-source-proxy: true
  client:
    rm-report-success-enable: true
    # 异步提交缓存队列长度(默认10000)
    rm-async-commit-buffer-limit: 1000
    # 一阶段全局提交结果上报TC重试次数(默认1次,建议大于1)
    tm-commit-retry-count:   3
    # 一阶段全局回滚结果上报TC重试次数(默认1次,建议大于1)
    tm-rollback-retry-count: 3
    support:
      # 数据源自动代理开关(默认false关闭)
      spring-datasource-autoproxy: true
  service:
    vgroup-mapping:
      tx_group_test: default
    grouplist:
      default: 127.0.0.1:8091
           

feign远程调用配置

/**
 * 描述
 *
 * @author huenbin
 * @date 1/21/21 4:44 PM
 */
@Configuration
@Slf4j
public class SeataFeignConfig implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate requestTemplate) {
        String xid = RootContext.getXID();
        if (StringUtils.isNotBlank(xid)) {
            log.info("feign传递分布式事务xid:{}", xid);
            requestTemplate.header(RootContext.KEY_XID, xid);
        }
    }
}
           

数据库需要新增表

CREATE TABLE `undo_log` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `branch_id` bigint(20) NOT NULL,
  `xid` varchar(100) NOT NULL,
  `context` varchar(128) NOT NULL,
  `rollback_info` longblob NOT NULL,
  `log_status` int(11) 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=1 DEFAULT CHARSET=utf8;
           

然后调用方需要事务的地方比如service层加上注解,被调用方服务也加上以下注解,即可默认生效AT事务

@GlobalTransactional