Seata服务搭建 —— nacos

Posted mry6

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Seata服务搭建 —— nacos相关的知识,希望对你有一定的参考价值。

Seata配置Nacos注册中心 负责事务参与者(微服务)和TC通信

流程图:

将Seata Server注册到Nacos,修改conf目录下的registry.conf配置

registry 
  # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
  type = "nacos"

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


#设置负载均衡策略,默认是轮询
loadBalance = “RandomLoadBalance”

loadBalanceVirtualNodes = 10

Seata服务 使用Nacos配置中心,修改conf目录下的registry.conf配置

config 
  # file、nacos 、apollo、zk、consul、etcd3
  type = "nacos"

  nacos 
    serverAddr = "127.0.0.1:8848"
    namespace = ""
    group = "SEATA_GROUP"
    username = "nacos"
    password = "nacos"
  
  consul 
    serverAddr = "127.0.0.1:8500"
  
  apollo 
    appId = "seata-server"
    apolloMeta = "http://192.168.1.204:8801"
    namespace = "application"
  
  zk 
    serverAddr = "127.0.0.1:2181"
    sessionTimeout = 6000
    connectTimeout = 2000
    username = ""
    password = ""
  
  etcd3 
    serverAddr = "http://localhost:2379"
  
  file 
    name = "file.conf"
  


seata默认的配置文件在:
下载地址:https://github.com/seata/seata/blob/develop/script/config-center/config.txt

修改seata配置文件:
my_test_tx_group需要与客户端保持一致,default需要跟客户端和registry中的cluster保持一致。

my_test_tx_group可以自定义 比如:(beijing),对应的client也要去设置:

seata.service.vgroup-mapping.projectA=beijing

事务分组的作用:解决异地机房停电容错。
default 必须要等于 registry.conf 中的 cluster=“default”

把seata本地配置文件同步到nacos配置中心的流程:
1》把config.txt 放在 seata目录的同级目录

2》在seata目录下添加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

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="nacos"
fi
if [[ -z $password ]]; then
    password="nacos"
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=$1&group=$group&content=$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")/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


双击:nacos-config.sh这个文件
shell:

sh nacos-config.sh -h localhost -p 8848 -g SEATA_GROUP -t 9704bb45-3e8b-479d-b491-f0f77765e2df

参数说明:
-h:host,默认值localhost
-p:port,默认值8848
-g:配置分组,默认值为 ‘SEATA_GROUP’
-t :租户信息,对应Nacos的命名空间ID字段,默认值为空

查看nacos中的配置列表:

启动Seata Server

1》源码启动:执行server模块下io.seata.server.Server.java的main方法
2》命令启动:bin/seata-server.sh -h 127.0.0.1 -p 8091 -m db -n 1 -e test

支持的启动参数:

参数全写作用备注
-h–host指定在注册中心注册的IP不指定时获取当前的IP,外部访问部署在云环境和容器中的server建议指定
-p–port指定server启动的端口默认为8091
-m–storeMode事务日志存储方式支持file,db,redis,默认为file 注意: redis需要seata-server 1.3 版本及以上
-n–serverNode用于指定seata-server节点ID如 1,2,3…,默认为1
-e–seataEnv指定seata-server运行环境如dev,test等,服务启动时会使用registry-dev.conf这样的配置

启动Seata Server

bin/seata-server.sh -p 8092

启动成功,默认端口8091

在注册中心可以查看到seata-server注册成功

以上是关于Seata服务搭建 —— nacos的主要内容,如果未能解决你的问题,请参考以下文章

分布式事务Seata使用

分布式事务Seata使用

Seata部署TC服务实现高可用和异地容灾

Seata部署TC服务实现高可用和异地容灾

Seata下载安装 | 集成Nacos配置 | 简单栗子

SpringCloud第二季之Nacos,Sentinel,Seata以及雪花算法学习笔记