Seata之 Win系统和 Linux系统搭建

上善若泪 / 2023-05-14 / 原文

目录
  • 1 Seata搭建
    • 1.1 Linux环境搭建
      • 1.1.1 准备工作
      • 1.1.2 下载
      • 1.1.3 建表
      • 1.1.4 配置 nacos
        • 1.1.4.1 新建命名空间
        • 1.1.4.2 上传配置至Nacos配置中心
        • 1.1.4.3 不上传而使用配置
      • 1.1.5 修改 appplication.yml
        • 1.1.5.1 seata.store
        • 1.1.5.2 seata.config
        • 1.1.5.3 seata.registry
      • 1.1.6 启动
    • 1.2 Windows环境搭建
      • 1.2.1 下载安装包
      • 1.2.2 建表
      • 1.2.3 配置nacos
      • 1.2.4 修改 application.yml
      • 1.2.5 启动

1 Seata搭建

1.1 Linux环境搭建

1.1.1 准备工作

Seata 是一个分布式事务,seata 服务端也是一个微服务,需要和其他微服务一样需要注册中心和配置中心。同时事务回滚,需要数据库日志记录。
注册中心和配置中心: nacos(点击了解Nacos原理和使用)
数据库: mysql(点击了解Linux下安装MySQL)

1.1.2 下载

进入Seata的github官网下载,下载版本是1.6.0,找到 seata-server-1.6.0.tar.gz下载。解压文件后进入seata文件。

1.1.3 建表

新建数据库seata,然后在 seata 文件夹里面的 script 文件,找到server -> db -> mysql.sql,在数据库中执行sql语句:

-- 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_status_gmt_modified` (`status` , `gmt_modified`),
    KEY `idx_transaction_id` (`transaction_id`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4;

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

-- 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),
    `status`         TINYINT      NOT NULL DEFAULT '0' COMMENT '0:locked ,1:rollbacking',
    `gmt_create`     DATETIME,
    `gmt_modified`   DATETIME,
    PRIMARY KEY (`row_key`),
    KEY `idx_status` (`status`),
    KEY `idx_branch_id` (`branch_id`),
    KEY `idx_xid_and_branch_id` (`xid` , `branch_id`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4;

CREATE TABLE IF NOT EXISTS `distributed_lock`
(
    `lock_key`       CHAR(20) NOT NULL,
    `lock_value`     VARCHAR(20) NOT NULL,
    `expire`         BIGINT,
    primary key (`lock_key`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4;

INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('AsyncCommitting', ' ', 0);
INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('RetryCommitting', ' ', 0);
INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('RetryRollbacking', ' ', 0);
INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('TxTimeoutCheck', ' ', 0);

全局事务会话由:全局事务分支事务全局锁,对应表分别为global_tablebranch_tablelock_table

1.1.4 配置 nacos

1.1.4.1 新建命名空间

nacos 控制台添加新的命名空间:
在这里插入图片描述
添加一条 seata,命名空间ID在后面需要用到:
在这里插入图片描述

1.1.4.2 上传配置至Nacos配置中心

进到 seata 目录中,找到 nacos-config.sh 文件,路径:script -> config-center -> nacos -> nacos-config.sh。执行nacos-config.sh脚本:

sh nacos-config.sh -h 127.0.0.1 -p 8848 -g SEATA_GROUP -t xxxx -u username -w password

参数详解:

  • -h:nacos服务IP
  • -p:nacos服务端口
  • -u:nacos登录名
  • -w:nacos登录密码
  • -g:nacos 配置的分组名称,默认设置SEATA_GROUP
  • -t:上一步配置的命名空间ID

执行脚本之后,输出以下脚本:

Set server.maxCommitRetryTimeout=-1 successfully 
Set server.maxRollbackRetryTimeout=-1 successfully 
Set server.rollbackRetryTimeoutUnlockEnable=false successfully 
Set server.distributedLockExpireTime=10000 successfully 
Set server.xaerNotaRetryTimeout=60000 successfully 
Set server.session.branchAsyncQueueSize=5000 successfully 
Set server.session.enableBranchAsyncRemove=false successfully 
Set server.enableParallelRequestHandle=false successfully 
Set metrics.enabled=false successfully 
Set metrics.registryType=compact successfully 
Set metrics.exporterList=prometheus successfully 
Set metrics.exporterPrometheusPort=9898 successfully 

再去nacos控制台查看配置:
在这里插入图片描述

1.1.4.3 不上传而使用配置

在nacos中 创建 seataServer.properties 文件
在这里插入图片描述
配置文件参考官方:https://seata.io/zh-cn/docs/user/configurations.html
windows一般使用这种方式,不过Linux两种都可以

1.1.5 修改 appplication.yml

找到 appplication.yml 文件,路径为:seata -> conf -> application.yml

1.1.5.1 seata.store

seata.store 配置 seata 的存储,修改 store.mode="db":

seata:
  store:
    # support: file 、 db 、 redis
    mode: db

修改数据库连接,将 seata -> conf -> application.example.yml 中附带额外配置,将其db相关配置复制至application.yml,修改 store.db 相关属性。数据库是步骤一配置的数据库:

seata:
  store:
    # support: file 、 db 、 redis
    mode: db
    db:
      datasource: druid
      db-type: mysql
      driver-class-name: com.mysql.jdbc.Driver
      url: jdbc:mysql://xxxxx:3306/seata?rewriteBatchedStatements=true
      user: xxxx
      password: xxx
      min-conn: 5
      max-conn: 100
      global-table: global_table
      branch-table: branch_table
      lock-table: lock_table
      distributed-lock-table: distributed_lock
      query-limit: 100
      max-wait: 5000

1.1.5.2 seata.config

seata.config 是配置 nacos 配置中心相关的配置。将 seata.config.type 修改成nacos

seata:
  config:
    # support: nacos, consul, apollo, zk, etcd3
    type: nacos

然后添加 seata.config.nacos 相关的配置:

seata:
  config:
    # support: nacos, consul, apollo, zk, etcd3
    type: nacos
    nacos:
      server-addr: 127.0.0.1:8848
      group : SEATA_GROUP
      namespace: xxxxx
      username: xxx
      password: xxx

其中 namespacenacos 命名空间ID而不是名称

1.1.5.3 seata.registry

seata.registry 是配置注册中心相关字段,将 seata 服务作为一个微服务注册到注册中心。将 registry.type 改成 nacos ,配置如下:

seata:
  registry:
    # support: nacos, eureka, redis, zk, consul, etcd3, sofa
    type: nacos
    nacos:
      application: seata-server
      serverAddr: 127.0.0.1:8848
      group: SEATA_GROUP
      namespace: xxxxxx
      username: xxxx
      password: xxx

namespace也是是 nacos 命名空间ID而不是名称

1.1.6 启动

找到 seata 文件中的 bin 目录,执行启动命令:

sh seata-server.sh -h 127.0.0.1 -p 8091 -m db

控制台输出:

apm-skywalking not enabled
seata-server is starting, you can check the /opt/seata/logs/start.out

打开 start.out 日志:
在这里插入图片描述

系统启动成功,再登录 http://127.0.0.1:7091,就能看到seata控制台信息。

nacos控制台服务列表新增了一个服务,说明seata服务成功注册到了nacos注册中心:
在这里插入图片描述

1.2 Windows环境搭建

1.2.1 下载安装包

进入Seata的github官网可以下载windows的zip包,或者在Seata官网直接下在windows的zip包,下载版本是1.6.0,找到 seata-server-1.6.0.zip下载

1.2.2 建表

同上面的建表步骤,还是在路径:script\server\db\文件夹下的mysql.sql

1.2.3 配置nacos

在nacos中 创建 seataServer.properties 文件
在这里插入图片描述
配置文件参考官方:https://seata.io/zh-cn/docs/user/configurations.html

# 数据存储方式,db代表数据库
store.mode=db
store.db.datasource=druid
store.db.dbType=mysql
store.db.driverClassName=com.mysql.cj.jdbc.Driver
store.db.url=jdbc:mysql://127.0.0.1:3306/seatab?useUnicode=true&rewriteBatchedStatements=true&serverTimezone=GMT
 
store.db.user=xxxx
store.db.password=xxxx
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
# 事务、日志等配置
server.recovery.committingRetryPeriod=3000
server.recovery.asynCommittingRetryPeriod=3000
server.recovery.rollbackingRetryPeriod=3000
server.recovery.timeoutRetryPeriod=3000
server.maxCommitRetryTimeout=-1
server.maxRollbackRetryTimeout=-1
server.rollbackRetryTimeoutUnlockEnable=false
server.undo.logSaveDays=7
server.undo.logDeletePeriod=86400000
 
# 客户端与服务端传输方式
transport.serialization=seata
transport.compressor=none
# 关闭metrics功能,提高性能
metrics.enabled=false
metrics.registryType=compact
metrics.exporterList=prometheus
metrics.exporterPrometheusPort=9898

1.2.4 修改 application.yml

server:
  port: 7091
 
spring:
  application:
    name: seata-server
 
logging:
  config: classpath:logback-spring.xml
  file:
    path: ${user.home}/logs/seata
  extend:
    logstash-appender:
      destination: 127.0.0.1:4560
    kafka-appender:
      bootstrap-servers: 127.0.0.1:9092
      topic: logback_to_logstash  
console:
  user:
    username: seata
    password: seata     
seata:
  config:
    # support: nacos, consul, apollo, zk, etcd3
    type: nacos
    nacos:
      server-addr: 127.0.0.1:8848
      namespace: seata
      group: SEATA_GROUP
      username: nacos
      password: nacos
      ##if use MSE Nacos with auth, mutex with username/password attribute
      #access-key: ""
      #secret-key: ""
      data-id: seataServer.properties
 
  registry:
    # support: nacos, eureka, redis, zk, consul, etcd3, sofa
    type: nacos
    # preferred-networks: 30.240.*
    nacos:
      application: seata-server
      server-addr: 127.0.0.1:8848
      group: SEATA_GROUP
      namespace: dev
      #cluster: SH
      username: nacos
      password: nacos
      ##if use MSE Nacos with auth, mutex with username/password attribute
      #access-key: ""
      #secret-key: ""
    
  store:
    # support: file 、 db 、 redis
    mode: db
    db:
      datasource: druid
      db-type: mysql
      driver-class-name: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://127.0.0.1:3306/seata?useUnicode=true&rewriteBatchedStatements=true&serverTimezone=GMT
      user: xxxx
      password: xxxx
      min-conn: 5
      max-conn: 100
      global-table: global_table
      branch-table: branch_table
      lock-table: lock_table
      distributed-lock-table: distributed_lock
      query-limit: 100
      max-wait: 5000
#  server:
#    service-port: 8091 #If not configured, the default is '${server.port} + 1000'
  security:
    secretKey: SeataSecretKey0c382ef121d778043159209298fd40bf3850a017
    tokenValidityInMilliseconds: 1800000
    ignore:
      urls: /,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/api/v1/auth/login

1.2.5 启动

找到 seata 文件中的 bin 目录
双击 seata-server.bat 文件,在浏览器中输入:http://127.0.0.1:7091/