12

Seata 环境搭建 - 小码code

 1 year ago
source link: https://www.cnblogs.com/jeremylai7/p/16832440.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Seata 环境搭建

在使用微服务中,单体事务注解@Transactional 就不适用了,需要采用分布式事务解决方案,本文介绍分布式事务Seata的安装。Seata一款开源的分布式事务解决方案,致力于在微服务架构下提供高性能和简单易用的分布式事务服务。

seata版本: 1.5.2

2448954-20221027153040533-1777569408.png

Seata是一个分布式事务,seata服务端也是一个微服务,需要和其他微服务一样需要注册中心配置中心。同时事务回滚,需要数据库日志记录。

  • 注册中心和配置中心: nacos
  • 数据库: mysql

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

新建数据库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

3.配置 nacos

nacos控制台添加新的命名空间:

2448954-20221027153102195-24228986.png

添加一条seata命名空间ID在后面需要用到:

2448954-20221027153115479-783618403.png

3.1 上传配置至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
2448954-20221027153133511-193793889.png

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

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控制台查看配置:

2448954-20221027153145912-248947636.png

说明配置上传成功。上传不成功,大部分原因是配置没有配置成功,比如命名空间ID没配置正确。

4.修改 appplication.yml

找到appplication.yml文件,路径为:seata -> conf -> application.yml,以下三个小节分别配置storeconfigregistry:

2448954-20221027153159887-1765744341.png

4.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

4.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"

其中namespace是步骤3中的命名空间ID

4.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也是步骤3中的命名空间ID

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

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日志:

2448954-20221027153214423-111726030.png

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

nacos控制台服务列表新增了一个服务,说明seata服务成功注册到了nacos注册中心:

2448954-20221027153227863-907698805.png
  • seata安装版本是1.5.2,版本不同,安装流程也可能不同,这里的版本需要保持一致
  • 执行sql创建数据表
  • 使用脚本添加配置到nacos配置中心
  • 修改application.yml文件,分别修改storeconfigregistry相关配置。
  • 启动服务,成功登陆seata控制台。
  • 查看nacos控制台,服务列表新增seata服务。

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK