Seata1.4.2+Nacos搭建使用(Linux)

下载seata1.4.2

下载地址:https://github.com/seata/seata/releases
这里省略上传Linux,解压等过程,本人seata安装目录为 /usr/local/seata/seata-server-1.4.2

创建seata需要的数据库表

sql文件下载:https://github.com/seata/seata/blob/v1.4.2/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(128),
    `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;

将config.txt中的配置导入Nacos

下载一键导入脚本:https://github.com/seata/seata/blob/v1.4.2/script/config-center/nacos/nacos-config.sh

nacos-config.sh脚本中会去找config.txt的位置,如果你的config.conf的位置和nacos-config.sh中的位置不一致,要么改nacos-config.sh文件,要么更换config.conf的位置。

下载conifg.txt:https://github.com/seata/seata/blob/v1.4.2/script/config-center/config.txt

config.txt中配置很多,大部分都是默认配置,没有必要全部都导入Nacos,已下是精简版本:

service.vgroupMapping.default_tx_group=default
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&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.distributedLockTable=distributed_lock
store.db.queryLimit=100
store.db.lockTable=lock_table
store.db.maxWait=5000

导入成功,naocs上会有很多配置:
Seata1.4.2+Nacos搭建使用(Linux)_第1张图片

1.4.2后支持把所有配置放在一个dataId(seataServer.properties)中,且若上面散装配置和下图的整合配置都配,下图方式的优先级要高于上面散装配置

Seata1.4.2+Nacos搭建使用(Linux)_第2张图片

修改file.conf文件

该文件是事务日志的相关配置,本人是记录在数据库的

## 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"
    ## if using mysql to store the data, recommend add rewriteBatchedStatements=true in jdbc connection param
    url = "jdbc:mysql://127.0.0.1:3306/seata?rewriteBatchedStatements=true"
    user = "root"
    password = "123456"
    minConn = 5
    maxConn = 100
    # 这三张表就是上面需要创建的数据库表
    globalTable = "global_table"
    branchTable = "branch_table"
    lockTable = "lock_table"
    queryLimit = 100
    maxWait = 5000
  }
}

修改registry.conf文件

该文件是配置 注册中心和配置中心的,本人用的都是Nacos

# 注册中心配置
registry {
  # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa、custom
  type = "nacos"

  nacos {
    application = "seata-server"
    serverAddr = "127.0.0.1:8848"
    group = "SEATA_GROUP"
    namespace = "224435a3-a0e5-4045-9aaf-b80c03bae620"
    username = "nacos"
    password = "123456"
    cluster = "default"
  }
}
## 配置中心配置
config {
  # file、nacos 、apollo、zk、consul、etcd3、springCloudConfig、custom
  type = "nacos"

  nacos {
    serverAddr = "127.0.0.1:8848"
    namespace = "224435a3-a0e5-4045-9aaf-b80c03bae620"
    group = "SEATA_GROUP"
    username = "nacos"
    password = "123456"
	dataId = "seataServer.properties"
  }
}

启动seata

本人习惯自己创建一个启动文件:

#!/bin/sh
export LANG="en_US.UTF-8"
cd /usr/local/seata/seata-server-1.4.2/bin
runMessage=`ps aux | grep \`cat pidfile.txt\``
projectStartCommand="sh seata-server.sh"
if [[ $runMessage == *$projectStartCommand* ]]
then
    echo "Application has starting ,restarting..."
    kill -9 `cat pidfile.txt`
    nohup sh seata-server.sh -h 127.0.0.1 -p 8091 -n 1  >nohup.out 2>1 & echo $! > pidfile.txt
else
    echo "Application has stopped ,starting..."
    nohup sh seata-server.sh -h 127.0.0.1 -p 8091 -n 1  >nohup.out 2>1 & echo $! > pidfile.txt
fi

本人习惯自己创建一个停止文件:

#!/bin/sh
cd /usr/local/seata/seata-server-1.4.2/bin
PID=$(cat pidfile.txt)
if [ ${PID} ];
then
    echo 'Application is stpping...'
    echo kill $PID DONE
    kill $PID
    echo 'Application is already stopped...'
else
    echo 'Application is already stopped...'
fi

启动成功,nacos上会有服务实例:
Seata1.4.2+Nacos搭建使用(Linux)_第3张图片

spring cloud整合seata1.4.2

<!-- 分布式事务解决方案 -->
<dependency>
    <groupId>io.seata</groupId>
    <artifactId>seata-spring-boot-starter</artifactId>
    <version>1.4.2</version>
</dependency>
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
    <exclusions>
        <exclusion>
            <artifactId>seata-spring-boot-starter</artifactId>
            <groupId>io.seata</groupId>
        </exclusion>
    </exclusions>
</dependency>

yml文件中配置

seata:
  enabled: true
  # 启用自动代理数据源 多数据源的时候关闭
  enable-auto-data-source-proxy: true
  application-id: ${spring.application.name}
  # 与nacos事务分组配置项保持一致
  tx-service-group: default_tx_group
  # 注册中心
  registry:  
    type: nacos
    nacos:
      # seata-server注册在nacos中的服务名
      application: seata-server
      # nacos的地址端口 
      server-addr: 49.234.102.234:8848
      # seata-server在nacos的分组
      group: SEATA_GROUP
      # seata-server在nacos的命名空间ID
      namespace: 224435a3-a0e5-4045-9aaf-b80c03bae620
      userName: "nacos"
      password: "123456"
  # 配置中心
  config:  
    type: nacos
    nacos:
      server-addr: 49.234.102.234:8848
      group: SEATA_GROUP
      # 与配置项命名空间ID保持一致
      namespace: 224435a3-a0e5-4045-9aaf-b80c03bae620
      userName: "nacos"
      password: "123456"
      # 与配置项Data Id保持一致
      dataId: seataServer.properties

代码

Seata1.4.2+Nacos搭建使用(Linux)_第4张图片

启动后观察日志

在这里插入图片描述

你可能感兴趣的:(LINUX,linux)