当前位置:  首页>> 技术小册>> Spring Cloud微服务项目实战

33 | 分布式事务:搭建 Seata 服务器

在微服务架构中,服务间的交互频繁且复杂,这带来了数据一致性的挑战。传统的单体应用事务管理机制在分布式系统中往往不再适用,因为跨多个服务的操作需要一种更为灵活和强大的事务管理机制。Seata(Simple Extensible Autonomous Transaction Architecture)便是在这样的背景下应运而生,它提供了一套高性能、易于使用的分布式事务解决方案,旨在帮助开发者简化分布式事务的管理。本章将详细介绍如何搭建Seata服务器,为后续的分布式事务管理奠定基础。

一、Seata简介

Seata 是一款开源的分布式事务解决方案,致力于提供高性能和简单易用的分布式事务服务。它实现了标准的两阶段提交协议(2PC),并在此基础上做了大量优化,以应对微服务架构下的高并发、低延迟等挑战。Seata 的核心优势包括:

  • 高性能:通过异步化、并行化等技术手段,提升事务处理性能。
  • 简单易用:提供简洁的API和配置方式,降低开发难度。
  • 灵活扩展:支持多种事务模式,如AT(Automatic Transaction)、TCC(Try-Confirm-Cancel)、SAGA等,满足不同场景需求。

二、搭建环境准备

在搭建Seata服务器之前,需要准备以下环境:

  • JDK:推荐使用JDK 1.8及以上版本。
  • Maven:用于构建和管理项目依赖。
  • Git:用于克隆Seata的源代码(如果需要)。
  • 数据库:用于存储Seata的事务日志和配置信息,可以选择MySQL、PostgreSQL等关系型数据库。
  • 服务器:可以是物理机或虚拟机,确保有足够的资源运行Seata服务。

三、下载并配置Seata

  1. 下载Seata

    可以从Seata的官方GitHub仓库下载最新版本的Seata源代码,或者使用Maven直接引入Seata的依赖到你的项目中(但本章节主要聚焦于搭建独立的Seata服务器)。

    1. git clone https://github.com/seata/seata.git
    2. cd seata
  2. 配置数据库

    Seata需要数据库来存储事务日志和配置信息。首先,需要在数据库中创建相应的表结构。Seata提供了SQL脚本(位于script/server/db/目录下),你可以根据自己的数据库类型选择合适的脚本执行。

    以MySQL为例,执行以下命令:

    1. mysql -u root -p
    2. # 在MySQL命令行中
    3. CREATE DATABASE seata;
    4. USE seata;
    5. SOURCE /path/to/seata/script/server/db/mysql/seata-server.sql;

    替换/path/to/seata为你的Seata源代码路径。

  3. 配置Seata服务器

    编辑conf/registry.confconf/file.conf文件以配置Seata服务器的注册中心、配置中心和事务日志存储等。

    • registry.conf:配置注册中心和配置中心的地址。常见的注册中心有Nacos、Eureka等,配置中心可以使用Nacos Config、Apollo等。

      1. registry {
      2. type = "nacos"
      3. nacos {
      4. serverAddr = "127.0.0.1:8848"
      5. namespace = ""
      6. cluster = "default"
      7. }
      8. }
      9. config {
      10. type = "nacos"
      11. nacos {
      12. serverAddr = "127.0.0.1:8848"
      13. namespace = ""
      14. group = "SEATA_GROUP"
      15. }
      16. }
    • file.conf:配置事务日志存储的数据库连接信息、服务分组等。

      1. service {
      2. # 事务服务组
      3. vgroupMapping.my_test_tx_group = "default"
      4. # 禁用全局事务
      5. disableGlobalTransaction = false
      6. }
      7. store {
      8. ## store mode: file、db
      9. mode = "db"
      10. db {
      11. ## the implement of javax.sql.DataSource, such as DruidDataSource(druid)/HikariDataSource(hikari)/BasicDataSource(dbcp) etc.
      12. datasource = "druid"
      13. ## mysql/oracle/postgresql/h2/oceanbase etc.
      14. dbType = "mysql"
      15. driverClassName = "com.mysql.cj.jdbc.Driver"
      16. url = "jdbc:mysql://127.0.0.1:3306/seata"
      17. username = "root"
      18. password = "your_password"
      19. ## connection init size
      20. initialSize = 5
      21. ## connection max active
      22. maxActive = 10
      23. ## connection max wait
      24. maxWait = 10000
      25. ## connection min idle
      26. minIdle = 2
      27. ## whether pool prepared statement
      28. poolPreparedStatements = true
      29. ## max pool prepared statement per connection size
      30. maxPoolPreparedStatementPerConnectionSize = 20
      31. ## after how long time, the idle connections will be tested
      32. testWhileIdle = true
      33. ## after how long time, the connection will be tested when borrowed from pool
      34. testOnBorrow = false
      35. ## after how long time, the connection will be tested when returned to pool
      36. testOnReturn = false
      37. ## sql to validate connections from pool before returning to client
      38. validationQuery = "SELECT 1"
      39. ## specify the class name of the query validator
      40. queryValidatorClassName = "io.seata.common.util.DefaultQueryValidator"
      41. ## specify the class name of the transaction query validator
      42. transactionQueryValidatorClassName = "io.seata.common.util.DefaultTransactionQueryValidator"
      43. }
      44. }

四、启动Seata服务器

  1. 编译Seata

    如果你从GitHub上下载了Seata的源代码,需要先编译它。在Seata的根目录下执行Maven命令:

    1. mvn -Prelease-all clean install -DskipTests=true

    这会编译所有模块,并打包成可执行文件。

  2. 启动Server

    编译完成后,进入seata/server/seata-server/target目录,找到seata-server.jar文件。使用Java命令启动Seata服务器:

    1. java -jar seata-server.jar -p 8091

    其中-p 8091指定了Seata服务器的端口号,你可以根据需要修改它。

五、验证与调试

启动Seata服务器后,可以通过访问其提供的监控页面(如果配置了相应的服务)或使用日志输出验证服务是否正常运行。此外,还可以编写一个简单的微服务应用来测试Seata的分布式事务功能,确保配置正确且Seata服务器能够正确处理分布式事务。

六、结论

本章详细介绍了如何搭建Seata服务器,包括环境准备、Seata的下载与配置、服务器的启动等步骤。通过搭建Seata服务器,可以为微服务架构下的应用提供强大且灵活的分布式事务支持,确保数据的一致性和完整性。在后续章节中,我们将进一步探讨如何在微服务项目中集成和使用Seata来处理分布式事务。


该分类下的相关小册推荐: