Seata Server搭建和配置

Seata Server搭建和配置

搭建Nacos可参考我的博客:Nacos服务治理中心和配置中心

第一步:下载安装服务端,并解压到指定位置

unzip  seata-server-1.1.0.zip 

seata的目录结构:

  • bin:存放各系统的启动脚本
  • conf:存放seata server启动时所需要配置信息、数据库模式下所需要的建表语句
  • lib:运行seata server所需要的的依赖包

第二步:配置seata
seata的配置文件(conf目录下)

  • file.conf: 该文件用于配置存储方式、透传事务信息的NIO等信息,默认对应registry.conf中file配置方式
  • registry.conf:seata server核心配置文件,可以通过该文件配置服务注册方式、配置读取方式。

注册方式目前支持file、nacos、eureka、redis、zk、consul、etcd3、sofa等方式,默认为file,对应读取file.conf内的注册方式信息。
读取配置信息的方式支持file、nacos、apollo、zk、consul、etcd3等方式,默认为file,对应读取file.conf文件内的配置。

修改registry.conf

  • 注册中心使用Nacos
  • 配置中心使用file进行配置

注册中心配置,用于TC,TM,RM的相互服务发现

registry {
  # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
  type = "nacos"
  consul {
    cluster = "seata"
    serverAddr = "127.0.0.1:8848"
  }
}

配置中心配置,用于读取TC的相关配置

config {
  # file、nacos 、apollo、zk、consul、etcd3
  type = "file"
  file {
    name = "file.conf"
  }
}

修改file.conf,配置中心配置,用于读取TC的相关配置

store {
  ## store mode: file?.b
  mode = "file"

  ## 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) etc.
    datasource = "dbcp"
    ## mysql/oracle/h2/oceanbase etc.
    dbType = "mysql"
    driverClassName = "com.mysql.jdbc.Driver"
    url = "jdbc:mysql://127.0.0.1:3306/seata"
    registry.confuser = "mysql"
    password = "mysql"
    minConn = 1
    maxConn = 10
    globalTable = "global_table"
    branchTable = "branch_table"
    lockTable = "lock_table"
    queryLimit = 100
  }
}

第三步:创建seata数据库、以及所需的三张表

  • global_table: 存储全局事务session数据的表
  • branch_table:存储分支事务session数据的表
  • lockTable:存储分布式锁数据的表
-- -------------------------------- 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,
    `gmt_modified`      DATETIME,
    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;

第四步:启动seata server

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

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

补充:

  • 外网访问:如果需要外网访问 需要将0.0.0.0转成外网IP
  • 后台启动: nohup sh seata-server.sh -p 8091 -h 127.0.0.1 -m file >
    catalina.out 2>&1 &

在nacos中看到seata的注册信息

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_40990818/article/details/107943923
今日推荐