分布式事务Seata1.5.2整合Nacos(一)

2年前 (2022) 程序员胖胖胖虎阿
172 0 0

分布式事务Seata1.5.2整合Nacos(一)

    • 一、服务端搭建
      • 1、下载服务端
      • 2、创建seata数据库
      • 3、Nacos添加配置文件
      • 4、修改seata/conf/application.yml文件内容:
      • 5、启动seata服务:
    • 二、客户端搭建

一、服务端搭建

1、下载服务端

官网下载Seata:https://github.com/seata/seata/releases
根据自己环境下载所对应的文件(我使用的是Linux)
分布式事务Seata1.5.2整合Nacos(一)

2、创建seata数据库

注意创建数据库的编码格式为:utf8mb4
添加以下表:

-- -------------------------------- 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_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);

3、Nacos添加配置文件

2.1、nacos中创建新的命名空间
分布式事务Seata1.5.2整合Nacos(一) 2.2、创建配置文件
分布式事务Seata1.5.2整合Nacos(一)
Data ID:seata-server.yml
Group: SEATA_GROUP
配置格式:YAML
文件内容如下(注意修改自己的mysql、redis信息,如果遇到mysql版本过高,mysql官网下载mysql所对应的连接驱动,使用命令解压seata/target/seata-server.jar,将内置的mysql连接驱动更换,然后使用命令打成jar包,必须使用命令解压和打包

service:
	#需要修改为自己的group,与客户端相对应,目前先这样
    vgroupMapping:
        netty-seata-test-group: default
        netty-system-platform-group: default
        netty-mybatis-plus-test-group: default
    default:
        grouplist: 127.0.0.1:8091
    enableDegrade: false
    disableGlobalTransaction: false

client:
    rm:
        asyncCommitBufferLimit: 10000
        lock:
            retryInterval: 10
            retryTimes: 30
            retryPolicyBranchRollbackOnConflict: true
        reportRetryCount: 5
        tableMetaCheckEnable: true
        tableMetaCheckerInterval: 60000
        sqlParserType: druid
        reportSuccessEnable: false
        sagaBranchRegisterEnable: false
        sagaJsonParser: fastjson
        tccActionInterceptorOrder: -2147482648
    tm:
        commitRetryCount: 5
        rollbackRetryCount: 5
        defaultGlobalTransactionTimeout: 60000
        degradeCheck: false
        degradeCheckAllowTimes: 10
        degradeCheckPeriod: 2000
        interceptorOrder: -2147482648
    undo:
        dataValidation: true
        logSerialization: jackson
        onlyCareUpdateColumns: true
        logTable: undo_log
        compress:
            enable: true
            type: zip
            threshold: 64k
server:
    undo:
        logSaveDays: 7
        logDeletePeriod: 86400000
    recovery:
        committingRetryPeriod: 1000
        asynCommittingRetryPeriod: 1000
        rollbackingRetryPeriod: 1000
        timeoutRetryPeriod: 1000
    maxCommitRetryTimeout: -1
    maxRollbackRetryTimeout: -1
    rollbackRetryTimeoutUnlockEnable: false
    distributedLockExpireTime: 10000
    xaerNotaRetryTimeout: 60000
    session:
        branchAsyncQueueSize: 5000
        enableBranchAsyncRemove: false
    enableParallelRequestHandle: false
tcc:
    fence:
        logTableName: tcc_fence_log
        cleanPeriod: 1h
log:
    exceptionRate: 100
store:
    mode: db
    lock:
        mode: db
    session:
        mode: db
    #publicKey: ''
    db:
        datasource: druid
        dbType: mysql
        driverClassName: com.mysql.jdbc.Driver
        url: jdbc:mysql://127.0.0.1:3306/seata?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2B8&allowMultiQueries=true&autoReconnect=true
        user: root
        password: 123456
        minConn: 5
        maxConn: 30
        globalTable: global_table
        branchTable: branch_table
        distributedLockTable: distributed_lock
        queryLimit: 100
        lockTable: lock_table
        maxWait: 5000
metrics:
    enabled: false
    registryType: compact
    exporterList: prometheus
    exporterPrometheusPort: 9898
    
transport:
    type: TCP
    server: NIO
    heartbeat: true
    enableTmClientBatchSendRequest: false
    enableRmClientBatchSendRequest: true
    enableTcServerBatchSendResponse: false
    rpcRmRequestTimeout: 30000
    rpcTmRequestTimeout: 30000
    rpcTcRequestTimeout: 30000
    threadFactory:
        bossThreadPrefix: NettyBoss
        workerThreadPrefix: NettyServerNIOWorker
        serverExecutorThreadPrefix: NettyServerBizHandler
        shareBossWorker: false
        clientSelectorThreadPrefix: NettyClientSelector
        clientSelectorThreadSize: 1
        clientWorkerThreadPrefix: NettyClientWorkerThread
        bossThreadSize: 1
        workerThreadSize: default
    shutdown:
        wait: 3
    serialization: seata
    compressor: none

4、修改seata/conf/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:
        serverAddr: 127.0.0.1:8848
        namespace: #修改为自己的
        group: SEATA_GROUP #需要与前面创建的配置文件相对应
        username: nacos
        password: nacos
        file-extension: yml
        shared-configs:
          - dataId: seata-server.yml #需要与前面创建的配置文件相对应
            namespace: #修改为自己的
            group: SEATA_GROUP
  registry:
    # support: nacos, eureka, redis, zk, consul, etcd3, sofa
    type: nacos
    nacos:
        serverAddr: 127.0.0.1:8848
        namespace: #修改为自己的
        group: SEATA_GROUP
        username: nacos
        password: nacos
        file-extension: yml
        shared-configs:
          - dataId: seata-server.yml #需要与前面创建的配置文件相对应
            namespace:  #需要与前面创建的配置文件相对应
            group: SEATA_GROUP #需要与前面创建的配置文件相对应
  #store:
    # support: file 、 db 、 redis
    #mode: file
#  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

5、启动seata服务:

1、使用启动命令:./seata-server.sh
	启动参数(具体参数请参考官网):
		-h: 如果是外网ip需要指定外网ip地址,例如:./seata-server.sh -h xxx.xxx.xxx.xxx
2、访问127.0.0.1:7091 
	用户名/密码 默认为seata/seata

分布式事务Seata1.5.2整合Nacos(一)

二、客户端搭建

查看下一篇文章:

版权声明:程序员胖胖胖虎阿 发表于 2022年11月24日 下午11:16。
转载请注明:分布式事务Seata1.5.2整合Nacos(一) | 胖虎的工具箱-编程导航

相关文章

暂无评论

暂无评论...