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注册成功
在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值