seata实例运用

seata的安装和配置

Seata安装和配置

安装nacos,本案例使用了nacos作为注册中心
https://github.com/alibaba/nacos/releases
下载nacos,本文使用的是windows版本1.4.0
使用命令行进入bin目录,以单机模式启动nacos

startup -m standalone

安装和配置Seata

http://seata.io/zh-cn/blog/download.html
下载Seata,这里是Windows版本的1.4.0
解压后,进入conf目录,配置file.conf和registry.conf两个文件
file.conf主要是数据库的配置,配置如下
在这里插入图片描述
registry.conf 是注册中心的配置
在这里插入图片描述
另外conf目录中还需要一个脚本文件:nacos-config.sh 用于对nacos进行初始化配置
在seata1.4.0中是没有的,需要自行创建,内容如下:

#!/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")/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 "
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
假设我们将seata应用程序部署在3个不同的实例上,每个实例都在不同的端口上运行。 1. 配置文件 在每个实例seata配置文件中,需要设置以下参数: - serverNode.ip:设置seata服务端节点的IP地址,这里应该是相同的,因为它们是在同一个环境中运行的。 - serverNode.port:设置seata服务端节点的端口号,这里应该是不同的,因为每个实例都在不同的端口上运行。 - service.vgroupMapping.{groupName}:将seata服务组名映射到实际的服务名,这里我们将它们都设置为default。 比如,假设我们有3个实例,它们的端口分别是8091、8092、8093,那么它们的配置文件应该如下: 实例1: ```conf ## registry registry.type = "file" registry.file.name = "file.conf" ## file file.conf = "file:/path/to/conf/nacos.conf" ## nacos nacos.serverAddr = "localhost:8848" nacos.namespace = "" nacos.group = "SEATA_GROUP" nacos.username = "" nacos.password = "" ## service service.vgroupMapping.default = "default" ## enablePessimisticLockFallback enablePessimisticLockFallback = true ## server server.recoverDelayInMills = 1000 server.maxCommitRetryTimeoutInMills = 120000 server.maxRollbackRetryTimeoutInMills = 120000 server.rollbackRetryTimeoutUnlockEnable = true server.transport.server = "netty" server.transport.codec = "seata" server.transport.heartbeat = "true" server.transport.enableClientBatchSendRequest = "true" server.shutdown.wait = "3" ## client client.rm.asyncCommitBufferLimit = 10000 client.rm.lock.retryInterval = 10 client.rm.lock.retryTimes = 30 client.rm.reportRetryCount = 5 client.rm.tableMetaCheckEnable = true client.rm.reportSuccessEnable = false client.tm.commitRetryCount = 5 client.tm.rollbackRetryCount = 5 client.tm.defaultGlobalTransactionTimeout = 600000 client.tm.degradeCheckAllowTimes = 10 client.tm.degradeCheckPeriod = 2000 client.tm.degradeCheck = true client.tm.disableGlobalTransaction = false client.tm.expirationScannerInterval = 60000 client.tm.globalTransactionTimeout = 600000 client.tm.highPerformanceMode = false client.tm.idGeneratorType = "SIMPLE" client.tm.rollbackRetryTimeout = 120000 client.tm.tableMetaCheckEnable = true client.tm.reportRetryCount = 5 client.tm.reportSuccessEnable = true client.tm.committingRetryPeriod = 1000 client.tm.rollbackingRetryPeriod = 1000 client.tm.timeoutRetryPeriod = 1000 client.rm.disableGlobalTransaction = false client.rm.datasource.autoproxy = true ## serverNode serverNode.ip = "127.0.0.1" serverNode.port = "8091" ``` 实例2: ```conf ## registry registry.type = "file" registry.file.name = "file.conf" ## file file.conf = "file:/path/to/conf/nacos.conf" ## nacos nacos.serverAddr = "localhost:8848" nacos.namespace = "" nacos.group = "SEATA_GROUP" nacos.username = "" nacos.password = "" ## service service.vgroupMapping.default = "default" ## enablePessimisticLockFallback enablePessimisticLockFallback = true ## server server.recoverDelayInMills = 1000 server.maxCommitRetryTimeoutInMills = 120000 server.maxRollbackRetryTimeoutInMills = 120000 server.rollbackRetryTimeoutUnlockEnable = true server.transport.server = "netty" server.transport.codec = "seata" server.transport.heartbeat = "true" server.transport.enableClientBatchSendRequest = "true" server.shutdown.wait = "3" ## client client.rm.asyncCommitBufferLimit = 10000 client.rm.lock.retryInterval = 10 client.rm.lock.retryTimes = 30 client.rm.reportRetryCount = 5 client.rm.tableMetaCheckEnable = true client.rm.reportSuccessEnable = false client.tm.commitRetryCount = 5 client.tm.rollbackRetryCount = 5 client.tm.defaultGlobalTransactionTimeout = 600000 client.tm.degradeCheckAllowTimes = 10 client.tm.degradeCheckPeriod = 2000 client.tm.degradeCheck = true client.tm.disableGlobalTransaction = false client.tm.expirationScannerInterval = 60000 client.tm.globalTransactionTimeout = 600000 client.tm.highPerformanceMode = false client.tm.idGeneratorType = "SIMPLE" client.tm.rollbackRetryTimeout = 120000 client.tm.tableMetaCheckEnable = true client.tm.reportRetryCount = 5 client.tm.reportSuccessEnable = true client.tm.committingRetryPeriod = 1000 client.tm.rollbackingRetryPeriod = 1000 client.tm.timeoutRetryPeriod = 1000 client.rm.disableGlobalTransaction = false client.rm.datasource.autoproxy = true ## serverNode serverNode.ip = "127.0.0.1" serverNode.port = "8092" ``` 实例3: ```conf ## registry registry.type = "file" registry.file.name = "file.conf" ## file file.conf = "file:/path/to/conf/nacos.conf" ## nacos nacos.serverAddr = "localhost:8848" nacos.namespace = "" nacos.group = "SEATA_GROUP" nacos.username = "" nacos.password = "" ## service service.vgroupMapping.default = "default" ## enablePessimisticLockFallback enablePessimisticLockFallback = true ## server server.recoverDelayInMills = 1000 server.maxCommitRetryTimeoutInMills = 120000 server.maxRollbackRetryTimeoutInMills = 120000 server.rollbackRetryTimeoutUnlockEnable = true server.transport.server = "netty" server.transport.codec = "seata" server.transport.heartbeat = "true" server.transport.enableClientBatchSendRequest = "true" server.shutdown.wait = "3" ## client client.rm.asyncCommitBufferLimit = 10000 client.rm.lock.retryInterval = 10 client.rm.lock.retryTimes = 30 client.rm.reportRetryCount = 5 client.rm.tableMetaCheckEnable = true client.rm.reportSuccessEnable = false client.tm.commitRetryCount = 5 client.tm.rollbackRetryCount = 5 client.tm.defaultGlobalTransactionTimeout = 600000 client.tm.degradeCheckAllowTimes = 10 client.tm.degradeCheckPeriod = 2000 client.tm.degradeCheck = true client.tm.disableGlobalTransaction = false client.tm.expirationScannerInterval = 60000 client.tm.globalTransactionTimeout = 600000 client.tm.highPerformanceMode = false client.tm.idGeneratorType = "SIMPLE" client.tm.rollbackRetryTimeout = 120000 client.tm.tableMetaCheckEnable = true client.tm.reportRetryCount = 5 client.tm.reportSuccessEnable = true client.tm.committingRetryPeriod = 1000 client.tm.rollbackingRetryPeriod = 1000 client.tm.timeoutRetryPeriod = 1000 client.rm.disableGlobalTransaction = false client.rm.datasource.autoproxy = true ## serverNode serverNode.ip = "127.0.0.1" serverNode.port = "8093" ``` 2. 启动实例 在每个实例中启动seata服务。 ``` bin/seata-server.sh -p 8091 -h 127.0.0.1 -m db bin/seata-server.sh -p 8092 -h 127.0.0.1 -m db bin/seata-server.sh -p 8093 -h 127.0.0.1 -m db ``` 3. 配置应用程序 在应用程序中,需要配置seata数据源和事务管理器,以及在每个需要事务支持的方法上添加@GlobalTransactional注解。 ```java @Configuration public class SeataConfiguration { @Bean public DataSourceProxy dataSourceProxy(DataSource dataSource) { return new DataSourceProxy(dataSource); } @Bean public GlobalTransactionScanner globalTransactionScanner() { return new GlobalTransactionScanner("my-app", "default"); } } @Service public class MyService { @Autowired private MyMapper myMapper; @GlobalTransactional public void doSomething() { myMapper.insertRecord(); } } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值