seata + nacos + springcloud

本文是建立在已有seata对应的数据库基础上,只要介绍seata如何注册到nacos以及在springcloud中的使用。

参考:seata1.4.0落地_腾飞124的博客-CSDN博客_seata1.4.0

目录

 1、Seata配置

1.1、在根目录seata下创建config.txt文件

1.2、在seata/conf/文件夹下增加nacos-config.sh 文件

1.3、将onfig.txt上传至nacos

1.4、配置registry.conf

1.5、配置file.conf

 1.6、启动seata服务,在seata/bin 文件夹下执行,windows的话执行.bat  linux执行.sh文件

2、springcloud微服务配置

2.1、 pom.xml文件引入

2.2、yml配置

 1、Seata配置

1.1、在根目录seata下创建config.txt文件

# sfs_mis_tx_group 为分组名称,与springcloud微服务中的配置一致
service.vgroupMapping.sfs_mis_tx_group=default
# seata对应的IP+端口
service.default.grouplist=192.168.0.10:8091
service.disableGlobalTransaction=false
# 数据库的相关配置
store.mode=db
store.db.datasource=druid
store.db.dbType=oracle
store.db.driverClassName=oracle.jdbc.driver.OracleDriver
store.db.url=jdbc:oracle:thin:@192.168.0.1:1521:ORCL
store.db.user=TEST
store.db.password=TEST
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

1.2、在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}"
    case $char 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=$(urlencode $2)&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")/conf/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

1.3、将onfig.txt上传至nacos

在seata/conf文件夹下执行如下上传命令:

# 以下涉及的IP、端口等信息为nacos配置, SEATA为推送到nacos上的命名空间
sh nacos-config.sh -h 192.168.0.10 -p 8848 -g SEATA_GROUP -t SEATA -u admin -w admin

seata + nacos + springcloud_第1张图片

seata + nacos + springcloud_第2张图片

1.4、配置registry.conf

seata + nacos + springcloud_第3张图片

1.5、配置file.conf

 seata + nacos + springcloud_第4张图片

 1.6、启动seata服务,在seata/bin 文件夹下执行,windows的话执行.bat  linux执行.sh文件

seata + nacos + springcloud_第5张图片

 seata + nacos + springcloud_第6张图片

2、springcloud微服务配置

2.1、 pom.xml文件引入


  com.alibaba.cloud
  spring-cloud-starter-alibaba-nacos-config
  2.1.2.RELEASE


  com.alibaba.cloud
  spring-cloud-starter-alibaba-nacos-discovery
  2.1.2.RELEASE


    io.seata
    seata-spring-boot-starter
    1.4.0

2.2、yml配置

spring.profiles.active: dev
sfs:
  nacos:
    server-addr: 192.168.0.10:8848
    namespace: ZH
    seata-namespace: SEATA
    username: admin
    password: admin

spring:
  application:
    name: test-application
  # 注册多个接口上的@FeignClient
  main:
    allow-bean-definition-overriding: true
  cloud:
    nacos:
      discovery:
        server-addr: ${sfs.nacos.server-addr}
        namespace: ${sfs.nacos.namespace}
        #权重:取值范围 1 到 100,数值越大,权重越大
        weight: 1
      config:
        server-addr: ${sfs.nacos.server-addr}
        #拓展名
        file-extension: yml
        # Nacos 认证用户
        username: ${sfs.nacos.username}
        password: ${sfs.nacos.password}
        #命名空间ID
        namespace: ${sfs.nacos.namespace}
        group: MIS_GROUP
        # 拓展配置,data-id,越后,优先级越高(相同配置,后面会覆盖前面)
        extension-configs:
          - data-id: redis-${spring.profiles.active}.yml

# seata 配置, 代替file.conf和registry.conf配置
seata:
  enabled: true
  tx-service-group: sfs_mis_tx_group
  use-jdk-proxy: true
  registry:
    type: nacos
    nacos:
      application: seata-server
      server-addr: ${sfs.nacos.server-addr}
      username: ${sfs.nacos.username}
      password: ${sfs.nacos.password}
      namespace: ${sfs.nacos.namespace}
  config:
    type: nacos
    nacos:
      server-addr: ${sfs.nacos.server-addr}
      group: SEATA_GROUP
      username: ${sfs.nacos.username}
      password: ${sfs.nacos.password}
      namespace: ${sfs.nacos.seata-namespace}
  service:
    vgroupMapping:
      sfs_mis_tx_group: default

 

 

你可能感兴趣的:(seata,spring,cloud)