seata

2023/8/7 17:38:20

seata

2023/8/7 17:38:43

Seata(Simple Extensible Autonomous Transaction Architecture)是一个开源的分布式事务解决方案,旨在简化分布式事务的开发和管理。Seata可以支持各种数据库和应用框架,为分布式系统提供强一致性的事务支持。

Seata的主要特点和功能包括:

  1. 分布式事务支持:Seata提供了对分布式事务的完整支持,可以保证多个参与者之间的操作具有原子性、一致性、隔离性和持久性(ACID)。
  2. 高性能:Seata通过优化资源管理和事务日志存储等方式,提供了高效的分布式事务处理能力,降低了性能开销。
  3. 支持多种数据存储:Seata支持主流的关系型数据库,如MySQL、Oracle、PostgreSQL等,并提供了对NoSQL数据库的扩展支持。
  4. 透明化接入:Seata提供了透明化接入的能力,不需要修改现有业务逻辑代码,只需简单配置即可实现分布式事务的管理。
  5. 高可用性和容错性:Seata提供了高可用的架构设计和容错机制,支持故障自动切换和恢复,保证分布式事务的可靠性。
  6. 多模式事务:Seata支持全局事务(Global Transaction)和本地事务(Branch Transaction)两种模式,适用于不同的场景和需求。
  7. 分布式事务解决方案集成:Seata可以与Spring Cloud、Dubbo、gRPC等分布式微服务框架无缝集成,提供全面的分布式事务解决方案。

Seata是一个开源且活跃的项目,得到了广泛的社区支持和贡献。它的目标是帮助开发者更好地处理分布式事务问题,提供简单、可靠和高性能的分布式事务解决方案。在大规模分布式系统中,使用Seata可以有效地解决分布式事务的一致性和可靠性问题,提升系统的稳定性和可用性。

1.下载

# 官网地址
http://seata.io/zh-cn/docs/user/configurations.html

2.解压

# 解压
unzip seata-server-1.4.0.zip
# 进入seata目录
cd seata

3.在seata目录下新建config.txt文件(第一次使用需要此文件初始化配置到nacos中)

[root@sizx153 seata]# vim config.txt
[root@sjzx153 seata]#

config.txt

ervice.vgroupMapping.jsxy-system-group=default
service.vgroupMapping.sjzx-dimensiondoor-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://192.168.0.151:3306/seata?useUnicode=true
store.db.user=root
store.db.password=root
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.到conf目录下修改registry.conf文件

注意,在这之前先在nacos中新建一个命名空间 SEATA

registry {
  # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
  type = "nacos"
  loadBalance = "RandomLoadBalance"
  loadBalanceVirtualNodes = 10

  nacos {
    application = "seata-server"
    serverAddr = "192.168.0.153:8848,192.168.0.152:8848,192.168.0.151:8848"
    group = "SEATA_GROUP"
    namespace = "SEATA"
    cluster = "default"
    username = "nacos"
    password = "nacos"
  }
  eureka {
    serviceUrl = "http://localhost:8761/eureka"
    application = "default"
    weight = "1"
  }
  redis {
    serverAddr = "localhost:6379"
    db = 0
    password = "redis"
    cluster = "default"
    timeout = 1
  }
  zk {
    cluster = "default"
    serverAddr = "127.0.0.1:2181"
    sessionTimeout = 6000
    connectTimeout = 2000
    username = ""
    password = ""
  }
  consul {
    cluster = "default"
    serverAddr = "127.0.0.1:8500"
  }
  etcd3 {
    cluster = "default"
    serverAddr = "http://localhost:2379"
  }
  sofa {
    serverAddr = "127.0.0.1:9603"
    application = "default"
    region = "DEFAULT_ZONE"
    datacenter = "DefaultDataCenter"
    cluster = "default"
    group = "SEATA_GROUP"
    addressWaitTime = "3000"
  }
  file {
    name = "file.conf"
  }
}

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

  nacos {
    serverAddr = "192.168.0.153:8848,192.168.0.152:8848,192.168.0.151:8848"
    namespace = "SEATA"
    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"
    apolloAccesskeySecret = ""
  }
  zk {
    serverAddr = "127.0.0.1:2181"
    sessionTimeout = 6000
    connectTimeout = 2000
    username = ""
    password = ""
  }
  etcd3 {
    serverAddr = "http://localhost:2379"
  }
  file {
    name = "file.conf"
  }
}

5.在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")/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

6.执行nacos-config.sh脚本将配置写到nacos

先使用chmod +x nacos-config.sh 命令使其可执行

./nacos-config.sh -h 192.168.0.153 -p 8848 -g SEATA_GROUP -t SEATA -u nacos -w nacos

注:命令解析:-h -p 指定nacos的端口地址;-g 指定配置的分组,注意,是配置的分组;-t 指定命名空间id; -u -w指定nacos的用户名和密码,同样,这里开启了nacos注册和配置认证的才需要指定。

7.到bin目录下启动seata服务

后台启动命令

nohup sh seata-server.sh -h 11.141.230.153 -p 8091 > ./seata.log 2>&1 &

注:-h为服务器ip,不设置可能会默认使用局域网ip导致其他应用访问不到。-p为端口

8.客户端应用程序中seata配置

# seata配置
seata:
  # 默认关闭,如需启用spring.datasource.dynami.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:
      jsxy-system-group: default
    # 分组和 Seata 服务的映射
    # grouplist:
    #   default: 11.141.230.153:8091
  config:
    type: nacos
    nacos:
      serverAddr: 192.168.0.151:8848,192.168.0.152:8848,192.168.0.153:8848
      group: SEATA_GROUP
      namespace: SEATA
      username: nacos
      password: nacos
  registry:
    type: nacos
    nacos:
      application: seata-server
      server-addr: 192.168.0.151:8848,192.168.0.152:8848,192.168.0.153:8848
      group: SEATA_GROUP
      namespace: SEATA
      username: nacos
      password: nacos
      # 可选  默认
      cluster: default