程序员的资源宝库

网站首页 > gitee 正文

Seata可视化界面之 Win系统和 Linux系统搭建

sanyeah 2024-04-08 12:54:06 gitee 6 ℃ 0 评论

目录
  • 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.3 Seata踩坑调试
      • 1.3.1 修改store.mode=db无效
        • 1.3.1.1 发现问题
        • 1.3.1.2 解决问题

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 文件

配置文件在解压后文件夹内:seata-1.6.0/script/config-center/config.txt

#For details about configuration items, see https://seata.io/zh-cn/docs/user/configurations.html
#Transport configuration, for client and server
transport.type=TCP
transport.server=NIO
transport.heartbeat=true
transport.enableTmClientBatchSendRequest=false
transport.enableRmClientBatchSendRequest=true
transport.enableTcServerBatchSendResponse=false
transport.rpcRmRequestTimeout=30000
transport.rpcTmRequestTimeout=30000
transport.rpcTcRequestTimeout=30000
transport.threadFactory.bossThreadPrefix=NettyBoss
transport.threadFactory.workerThreadPrefix=NettyServerNIOWorker
transport.threadFactory.serverExecutorThreadPrefix=NettyServerBizHandler
transport.threadFactory.shareBossWorker=false
transport.threadFactory.clientSelectorThreadPrefix=NettyClientSelector
transport.threadFactory.clientSelectorThreadSize=1
transport.threadFactory.clientWorkerThreadPrefix=NettyClientWorkerThread
transport.threadFactory.bossThreadSize=1
transport.threadFactory.workerThreadSize=default
transport.shutdown.wait=3
transport.serialization=seata
transport.compressor=none

#Transaction routing rules configuration, only for the client
service.vgroupMapping.default_tx_group=default
#If you use a registry, you can ignore it
service.default.grouplist=127.0.0.1:8091
service.enableDegrade=false
service.disableGlobalTransaction=false

#Transaction rule configuration, only for the client
client.rm.asyncCommitBufferLimit=10000
client.rm.lock.retryInterval=10
client.rm.lock.retryTimes=30
client.rm.lock.retryPolicyBranchRollbackOnConflict=true
client.rm.reportRetryCount=5
client.rm.tableMetaCheckEnable=true
client.rm.tableMetaCheckerInterval=60000
client.rm.sqlParserType=druid
client.rm.reportSuccessEnable=false
client.rm.sagaBranchRegisterEnable=false
client.rm.sagaJsonParser=fastjson
client.rm.tccActionInterceptorOrder=-2147482648
client.tm.commitRetryCount=5
client.tm.rollbackRetryCount=5
client.tm.defaultGlobalTransactionTimeout=60000
client.tm.degradeCheck=false
client.tm.degradeCheckAllowTimes=10
client.tm.degradeCheckPeriod=2000
client.tm.interceptorOrder=-2147482648
client.undo.dataValidation=true
client.undo.logSerialization=jackson
client.undo.onlyCareUpdateColumns=true
server.undo.logSaveDays=7
server.undo.logDeletePeriod=86400000
client.undo.logTable=undo_log
client.undo.compress.enable=true
client.undo.compress.type=zip
client.undo.compress.threshold=64k
#For TCC transaction mode
tcc.fence.logTableName=tcc_fence_log
tcc.fence.cleanPeriod=1h

#Log rule configuration, for client and server
log.exceptionRate=100

#Transaction storage configuration, only for the server. The file, db, and redis configuration values are optional.
store.mode=file
store.lock.mode=file
store.session.mode=file
#Used for password encryption
store.publicKey=

#If `store.mode,store.lock.mode,store.session.mode` are not equal to `file`, you can remove the configuration block.
store.file.dir=file_store/data
store.file.maxBranchSessionSize=16384
store.file.maxGlobalSessionSize=512
store.file.fileWriteBufferCacheSize=16384
store.file.flushDiskMode=async
store.file.sessionReloadReadSize=100

#These configurations are required if the `store mode` is `db`. If `store.mode,store.lock.mode,store.session.mode` are not equal to `db`, you can remove the configuration block.
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=username
store.db.password=password
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

#These configurations are required if the `store mode` is `redis`. If `store.mode,store.lock.mode,store.session.mode` are not equal to `redis`, you can remove the configuration block.
store.redis.mode=single
store.redis.single.host=127.0.0.1
store.redis.single.port=6379
store.redis.sentinel.masterName=
store.redis.sentinel.sentinelHosts=
store.redis.maxConn=10
store.redis.minConn=1
store.redis.maxTotal=100
store.redis.database=0
store.redis.password=
store.redis.queryLimit=100

#Transaction rule configuration, only for the server
server.recovery.committingRetryPeriod=1000
server.recovery.asynCommittingRetryPeriod=1000
server.recovery.rollbackingRetryPeriod=1000
server.recovery.timeoutRetryPeriod=1000
server.maxCommitRetryTimeout=-1
server.maxRollbackRetryTimeout=-1
server.rollbackRetryTimeoutUnlockEnable=false
server.distributedLockExpireTime=10000
server.xaerNotaRetryTimeout=60000
server.session.branchAsyncQueueSize=5000
server.session.enableBranchAsyncRemove=false
server.enableParallelRequestHandle=false

#Metrics configuration, only for the server
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命名空间id
      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命名空间id
      #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/

1.3 Seata踩坑调试

1.3.1 修改store.mode=db无效

application.yml 中的 seataServer.properties已经修改为如下,但还不生效

Transaction storage configuration, only for the server. The file, DB, and redis configuration values are optional.
store.mode=db
store.lock.mode=db
store.session.mode=db
#Used for password encryption
#store.publicKey=

#If `store.mode,store.lock.mode,store.session.mode` are not equal to `file`, you can remove the configuration block.


#These configurations are required if the `store mode` is `db`. If `store.mode,store.lock.mode,store.session.mode` are not equal to `db`, you can remove the configuration block.
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/seata?useUnicode=true&rewriteBatchedStatements=true
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.distributedLockTable=distributed_lock
store.db.queryLimit=100
store.db.lockTable=lock_table
store.db.maxWait=5000

1.3.1.1 发现问题

发现这个问题是指定的数据库里面一直没有数据落表,而且每次启动seata-server.bat时,旁边总是会有一个sessionStore文件夹,说明一直走的是 文件存储,即使指定了store.mode=db,依然会是file形式

所以可以强转指定db模式启动:seata-server.bat -m db,这时候就会看到报错,说明db模式有问题,就走默认的file形式了

1.3.1.2 解决问题

解决问题:

  • 原因一:
    查看是否配置有问题,把远端的配置,全部屏蔽掉,然后把db配置放在本地,即:在application.yml中删除config配置,而添加store配置,启动seata-server.bat 查看是否成功,若成功则数据库配置没问题,否则需要查看数据库配置信息
  • 原因二:
    如果数据库没问题,就尝试把数据库配置文件放到 nacos远端,单独抽离一个数据库配置文件,看看是否能连上 nacos 配置,如果不能连接,说明 seatanacos配置连接这里有问题
    顺便说下,nacos配置有两种方式:
    • 一种是在 nacos 指定一个分组如:SEATA_GROUP,把所有配置都拆分成一个个配置文件,这时候不指定data-id,会扫描该组下所有配置;
    • 第二种是把所有配置文件放在一个 seataServer.properties中,这样方便调试,统一管理

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表