Seata(一)分布式事务Seata Server集群环境搭建

分布式事务
placeholder image
admin 发布于:2023-02-04 19:50:56
阅读:loading

基本介绍

Seata 是一款开源的分布式事务解决方案,致力于在微服务架构下提供高性能和简单易用的分布式事务服务。在 Seata 开源之前,其内部版本在阿里系内部一直扮演着应用架构层数据一致性的中间件角色,帮助经济体平稳的度过历年的双11,对上层业务进行了有力的技术支撑。经过多年沉淀与积累,其商业化产品先后在阿里云、金融云上售卖。2019.1 为了打造更加完善的技术生态和普惠技术成果,Seata 正式宣布对外开源,未来 Seata 将以社区共建的形式帮助用户快速落地分布式事务解决方案。

分布式事务是来源于微服务的(或类似的场景),多个服务存在相互调用的关系,可能在某个请求的调用链路上存在着多处写数据库表的动作,分布式事务就要保证这些操作表的动作要么全部成功,要么全部失败。Seata 将为用户提供了 AT、TCC、SAGA 和 XA 事务模式,为用户打造一站式的分布式解决方案。前面一篇文章《分布式事务Atomikos介绍与实践》则是基于XA(基于数据源的代理,用于同一个项目中的多个数据源进行的事务机制)事务模式进行示例分享的,本篇主要以AT(不同微服务模块之间的接口调用的事务机制)事务模式摸索着分享一下分布式事务的实践,以下是seata官网的特殊功能截图,参考如下:

image.png

官方网站“https://seata.io/zh-cn/index.html”、“https://github.com/seata/seata”,坚持闭着眼睛最新版的原则下载最新版“seata-server-1.6.1.zip”,从Github官网下载Seata项目最新Releases版本v1.6.1(截至2023年1月时的最新版本),也可以在Seata官网进行最新版本下载,下载链接位置参考如下图所示:

image.png

安装过程

(1)前置条件Seata Server安装包 + Nacos + MySQL jdk8,并配置环境变量;准备一台Windows主机和3台CentOS虚拟机(防火墙状态为关闭,保证其能互通访问);(同前面的一些中间件保持一致

(2)将下载好的seata-server-1.6.1.zip文件分别传递至192.168.244.139服务器,并解压重命名为seata-server,参考文件目录如下:

image.png

(3)假设约定/app/seata/seata-server/bin为%SEATA_HOME%(并不配置环境变量),修改%SEATA_HOME%/bin目录的seata-server.sh文件,修改130行的JAVA_OPT参数,将JVM_XMX、JVM_XMS、JVM_XMN几个参数值修改为512M、256M、256M,注意该步骤不是必须的,默认启动占用2G内存的大小,若机器配置高可无视;

(4)备份%SEATA_HOME%/conf目录的application.yml文件为application.yml.bak,该步骤将设置服务的配置中心和注册中心以及seata-server数据的持久化存储,关于配置中心一直并未使用,所以默认不用调整,所以我主要调整的是服务注册中心和持久化存储,在application.yml文件中的seata.registry参数部分增加上application.example.yml(该文件位于同目录下,提供了全量的配置参数)文件中的seata.registry处关于nacos相关的配置;同时还需增加seata.store.db处的参数配置部分,参考调整后的配置文件为:

#  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.

server:
  port: 7091

spring:
  application:
    name: seata-server

logging:
  config: classpath:logback-spring.xml
  file:
    path: ${user.home}/logs/seata
  extend:
    logstash-appender:
      destination: 127.0.0.1:4560
    kafka-appender:
      bootstrap-servers: 127.0.0.1:9092
      topic: logback_to_logstash

console:
  user:
    username: seata
    password: seata

seata:
  config:
    # support: nacos, consul, apollo, zk, etcd3
    type: file
  registry:
    # support: nacos, eureka, redis, zk, consul, etcd3, sofa
    type: nacos 
    preferred-networks: 30.240.*
    nacos:
      application: seata-server
      server-addr: 192.168.244.134:8848,192.168.244.138:8848,192.168.244.139:8848
      group: chendd-SpringCloud-Seata-Group
      namespace: chendd-SpringCloud-Seata-NamespaceId
      cluster: default
      username: 
      password: 
      context-path: 
      ##if use MSE Nacos with auth, mutex with username/password attribute
      #access-key:
      #secret-key:
  store:
    # support: file 、 db 、 redis
    mode: db
    db:
      datasource: druid
      db-type: mysql
      driver-class-name: com.mysql.jdbc.Driver
      url: jdbc:mysql://192.168.244.1:3306/chendd-seata?rewriteBatchedStatements=true
      user: root
      password: root
      min-conn: 10
      max-conn: 100
      global-table: global_table
      branch-table: branch_table
      lock-table: lock_table
      distributed-lock-table: distributed_lock
      query-limit: 1000
      max-wait: 5000
#  server:
#    service-port: 8091 #If not configured, the default is '${server.port} + 1000'
  security:
    secretKey: SeataSecretKey0c382ef121d778043159209298fd40bf3850a017
    tokenValidityInMilliseconds: 1800000
    ignore:
      urls: /,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/api/v1/auth/login

安装说明

(1)服务的注册中心使用Nacos,Nacos的三台集群配置,且自定义了group和namespace,分别为:chendd-SpringCloud-Seata-Group和chendd-SpringCloud-Seata-NamespaceId;

(2)服务的出就会存储使用了druid连接池配置的MySQL数据库,细节参数都是默认的,需要创建chendd-seata数据库和表,相关的建表语句在%SEATA_SERVER%/script/server/db/mysql.sql位置,将脚本文件按需执行,将会创建4张表和4条数据(关系型数据库的建表脚本包含:mysql、oracle、postgresql),表说明:全局事务-->分支事务-->全局锁,对应表global_table、branch_table、lock_table;;

(3)安装过程(3)和(4)均不是必须的,默认使用文件存储数据,即便是所有的设置不调整也可以正常的启动和应用;

(4)启动服务,访问console控制台可以看到seata的管理界面,启动脚本参考为:“sh bin/seata-server.sh -p 8091 -h 192.168.244.134 -m db”。注意,启动脚本中的8091是seata-server的端口号,访问控制台需要访问7091(在注释中可以找到它们的对应关系是端口号 - 1000),如:http://192.168.244.134:7091/,启动命令支持多个参数,详细可参考https://seata.io/zh-cn/docs/ops/deploy-server.html,启动过程和访问(需要启动Nacos)参考如下截图所示:

image.png

image.png

image.png

image.png

集群部署

(1)停止本机服务,将上述步骤中134服务器的seata-server拷贝到其它的两个服务器,再次分别启动3台服务,启动脚本参考如下:

sh bin/seata-server.sh -p 8091 -h 192.168.244.134 -m db -n 1

sh bin/seata-server.sh -p 8091 -h 192.168.244.138 -m db -n 2

sh bin/seata-server.sh -p 8091 -h 192.168.244.139 -m db -n 3

(2)访问Nacos控制台界面,查看注册的seata应用程序服务;

image.png

其它说明

(1)seata是SpringCloud Alibaba系的组件之一,官网的中文文档值得去看看(理论太多,太复杂,我这里略过);

(2)seata-server-1.6.1 部署的原始配置部署包已上传至百度网盘,可按需下载:

链接:https://pan.baidu.com/s/1uxrF8X-2Ltw0LjJxbiKagQ

提取码:cw07


 点赞


 发表评论

当前回复:作者

 评论列表


留言区