美文网首页收藏springcloud
seata使用nacos进行配置

seata使用nacos进行配置

作者: pq217 | 来源:发表于2022-01-25 11:45 被阅读0次

    seata服务

    环境

    seata 1.3.0
    nacos 2.0.3

    新建命名空间

    为了和开发的项目进行区分,在nacos新建了一个命名空间:seata,并新建了个group:SEATA_GROUP

    seata的id是69...

    配置registry.conf

    文件位置:seata/conf/registry.conf
    完整配置如下

    # 服务注册地址(把seata服务注册到nacos)
    registry {
      # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
      type = "nacos"
    
      nacos {
        application = "seata-server"
        # nacos的地址
        serverAddr = "192.168.1.135:8848"
        # group
        group = "SEATA_GROUP"
        # seata的命名空间
        namespace = "69aee9e4-5654-4f6b-87f6-47c88b7fdb28"
        cluster = "default"
      }
    }
    # 配置地址(由于nacos也是配置中心,所以把配置也存入nacos)
    config {
      # file、nacos 、apollo、zk、consul、etcd3
      type = "nacos" # 使用nacos配置
      nacos {
        # nacos的地址
        serverAddr = "192.168.1.135:8848"
        # group
        group = "SEATA_GROUP"
        # seata的命名空间
        namespace = "69aee9e4-5654-4f6b-87f6-47c88b7fdb28"
      }
    }
    

    因为使用nacos config就不需要设置file.conf了,可以忽略

    编辑配置文件

    在conf下新建nacos-config.txt内容如下(其实在什么目录下建都无所谓)

    service.vgroupMapping.dddstruct_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://192.168.1.135:3306/dddstruct-seata
    store.db.user=root
    store.db.password=Xiaoming58
    store.db.minConn=5
    store.db.maxConn=30
    store.db.globalTable=global_table
    store.db.branchTable=branch_table
    store.db.lockTable=lock_table
    store.db.queryLimit=100
    

    其实还有很多参数可配,暂时使用默认就行了

    上传配置文件至nacos

    sh nacos-config.sh 127.0.0.1
    

    就已经上传成功了,nacos-config.sh的脚本如下

    #!/usr/bin/env bash
    if [ $# != 1 ]; then
    echo "./nacos-config.sh nacosIp"
    exit -1
    fi
    
    nacosIp=$1
    echo "set nacosIp=$nacosIp"
    error=0
    
    for line in $(cat nacos-config.txt)
    
    do
    
    key=${line%%=*}
    value=${line#*=}
    echo "\r\n set "${key}" = "${value}
    
    result=`curl -X POST "http://$nacosIp:8848/nacos/v1/cs/configs?dataId=$key&group=SEATA_GROUP&content=$value"`
    
    if [ "$result"x == "true"x ]; then
    
      echo "\033[42;37m $result \033[0m"
    
    else
    
      echo "\033[41;37 $result \033[0m"
      let error++
    
    fi
    
    done
    
    
    if [ $error -eq 0 ]; then
    
    echo  "\r\n\033[42;37m init nacos config finished, please start seata-server. \033[0m"
    
    else
    
    echo  "\r\n\033[41;33m init nacos config fail. \033[0m"
    
    fi
    

    这个脚本指定不了命名空间,端口等,应该有更好的脚本,但是这个也够用了,如果想换命名空间,去nacos克隆配置即可,上传后配置如下


    image.png

    启动seata

    进入bin目录下./seata-server.sh 启动即可

    数据库

    数据库分两种,一种是seata服务使用的数据库,通过store.db.url指定,一种是项目使用的数据库

    image.png

    seata服务数据库

    需要新建store.db.url指定的数据库,然后新建三张表, 对应

    store.db.globalTable=global_table
    store.db.branchTable=branch_table
    store.db.lockTable=lock_table
    

    这三张表,脚本如下

    -- -------------------------------- 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(96),
        `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;
    

    项目数据库

    项目数据库中每个微服务都要加一个表undo_log,脚本如下

    -- for AT mode you must to init this sql for you business database. the seata server not need it.
    CREATE TABLE IF NOT EXISTS `undo_log`
    (
        `branch_id`     BIGINT(20)   NOT NULL COMMENT 'branch transaction id',
        `xid`           VARCHAR(100) NOT NULL COMMENT 'global transaction id',
        `context`       VARCHAR(128) NOT NULL COMMENT 'undo_log context,such as serialization',
        `rollback_info` LONGBLOB     NOT NULL COMMENT 'rollback info',
        `log_status`    INT(11)      NOT NULL COMMENT '0:normal status,1:defense status',
        `log_created`   DATETIME(6)  NOT NULL COMMENT 'create datetime',
        `log_modified`  DATETIME(6)  NOT NULL COMMENT 'modify datetime',
        UNIQUE KEY `ux_undo_log` (`xid`, `branch_id`)
    ) ENGINE = InnoDB
      AUTO_INCREMENT = 1
      DEFAULT CHARSET = utf8 COMMENT ='AT transaction mode undo table';
    

    项目配置

    引入依赖

    <!--seata-->
    <dependency>    
        <groupId>com.alibaba.cloud</groupId>    
        <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
        <version>1.3.0</version>
    </dependency>
    

    application.yml

    seata:
      service:
        grouplist:
          seata-server: 192.168.1.135:8091
      # 下面俩个seata服务的配置一样    
      registry:
        type: nacos
        nacos:
          application: seata-server
          serverAddr: 192.168.1.135:8848
          group: SEATA_GROUP
          cluster: default
          namespace: 69aee9e4-5654-4f6b-87f6-47c88b7fdb28
      config:
        type: nacos
        nacos:
          serverAddr: 192.168.1.135:8848
          group: SEATA_GROUP
          namespace: 69aee9e4-5654-4f6b-87f6-47c88b7fdb28
    

    spring-cloud-alibaba配置

    spring:
      ...
      ## cloud  
      cloud:
        alibaba:
          ## seata 
          seata:
            #自定义事务组名称需要与seata-server中的对应
            tx-service-group: dddstruct_tx_group  
    

    注意dddstruct_tx_group与上面的service.vgroupMapping.dddstruct_tx_group=default对应

    相关文章

      网友评论

        本文标题:seata使用nacos进行配置

        本文链接:https://www.haomeiwen.com/subject/pegbhrtx.html