开发喵星球

若依微服务集成Nacos(219)

1、解压seata-server-$version.zip后修改conf/registry.conf文件:

registry {
  type = "nacos"
  nacos {
    application = "seata-server"
    serverAddr = "127.0.0.1:8848"
    group = "SEATA_GROUP"
    namespace = ""
    cluster = "default"
    username = "nacos"
    password = "nacos"
  }
}

config {
  type = "nacos"
  nacos {
    serverAddr = "127.0.0.1:8848"
    namespace = ""
    group = "SEATA_GROUP"
    username = "nacos"
    password = "nacos"
  }
}

由于使用nacos作为注册中心,所以conf目录下的file.conf无需理会。然后就可以直接启动bin/seata-server.bat,可以在nacos里看到一个名为seata-server的服务了。

2、新建ry-seata数据库

由于seata使用mysql作为db高可用数据库,故需要在mysql创建一个ry-seata库,并导入数据库脚本。

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

-- 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 = utf8mb4 COMMENT ='AT transaction mode undo table';

3、config.txt文件复制到seata目录

config.txt

service.vgroupMapping.ruoyi-system-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://127.0.0.1:3306/ry-seata?useUnicode=true
store.db.user=root
store.db.password=password
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

4、nacos-config.sh复制到seata的conf目录

nacos-config.sh

#!/usr/bin/env bash
# Copyright 1999-2019 Seata.io Group.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at、
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

while getopts ":h:p:g:t:u:w:" opt
do
  case opt in
  h)
    host=OPTARG
    ;;
  p)
    port=OPTARG
    ;;
  g)
    group=OPTARG
    ;;
  t)
    tenant=OPTARG
    ;;
  u)
    username=OPTARG
    ;;
  w)
    password=OPTARG
    ;;
  ?)
    echo " USAGE OPTION:0 [-h host] [-p port] [-g group] [-t tenant] [-u username] [-w password] "
    exit 1
    ;;
  esac
done

urlencode() {
  for ((i=0; i < {#1}; i++))
  do
    char="{1:i:1}"
    casechar in
    [a-zA-Z0-9.~_-]) printf char ;;
    *) printf '%%%02X' "'char" ;;
    esac
  done
}

if [[ -z {host} ]]; then
    host=localhost
fi
if [[ -z{port} ]]; then
    port=8848
fi
if [[ -z {group} ]]; then
    group="SEATA_GROUP"
fi
if [[ -z{tenant} ]]; then
    tenant=""
fi
if [[ -z {username} ]]; then
    username=""
fi
if [[ -z{password} ]]; then
    password=""
fi

nacosAddr=host:port
contentType="content-type:application/json;charset=UTF-8"

echo "set nacosAddr=nacosAddr"
echo "set group=group"

failCount=0
tempLog=(mktemp -u)
function addConfig() {
  curl -X POST -H "{contentType}" "http://nacosAddr/nacos/v1/cs/configs?dataId=(urlencode 1)&group=group&content=(urlencode2)&tenant=tenant&username=username&password=password" >"{tempLog}" 2>/dev/null
  if [[ -z (cat "{tempLog}") ]]; then
    echo " Please check the cluster status. "
    exit 1
  fi
  if [[ (cat "{tempLog}") =~ "true" ]]; then
    echo "Set 1=2 successfully "
  else
    echo "Set 1=2 failure "
    (( failCount++ ))
  fi
}

count=0
for line in (cat(dirname "PWD")/config.txt | sed s/[[:space:]]//g); do
  (( count++ ))
    key={line%%=*}
    value={line#*=}
    addConfig "{key}" "{value}"
done

echo "========================================================================="
echo " Complete initialization parameters,  total-count:count ,  failure-count:failCount "
echo "========================================================================="

if [[{failCount} -eq 0 ]]; then
    echo " Init nacos config finished, please start seata-server. "
else
    echo " init nacos config fail. "
fi

5、执行命令,后面填写nacos的IP地址

sh nacos-config.sh 127.0.0.1

成功后nacos配置列表也能查询到相关配置

6、修改服务配置文件

# spring配置
spring: 
  datasource:
    dynamic:
      # 开启seata代理
      seata: true

# seata配置
seata:
  enabled: true
  # Seata 应用编号,默认为 {spring.application.name}
  application-id:{spring.application.name}
  # Seata 事务组编号,用于 TC 集群名
  tx-service-group: ${spring.application.name}-group
  # 关闭自动代理
  enable-auto-data-source-proxy: false
  # 服务配置项
  service:
    # 虚拟组和分组的映射
    vgroup-mapping:
      ruoyi-system-group: default
  config:
    type: nacos
    nacos:
      serverAddr: 127.0.0.1:8848
      group: SEATA_GROUP
      namespace:
  registry:
    type: nacos
    nacos:
      application: seata-server
      server-addr: 127.0.0.1:8848
      namespace:
   
分类:Java/OOP 作者:无限繁荣, 吴蓉 发表于:2024-05-24 17:19:38 阅读量:86
<<   >>


powered by kaifamiao