Apache Seata 示例项目快速入门

Apache Seata 示例项目快速入门

incubator-seata-samplesApache Seata(incubating) Samples for Java项目地址:https://gitcode.com/gh_mirrors/inc/incubator-seata-samples

Apache Seata 是一个用于分布式事务解决方案的高性能开源框架,而 incubator-seata-samples 则是该项目的一系列示例,旨在帮助开发者更好地理解和使用 Seata。接下来我们将通过以下几个部分来了解这个项目。

1. 项目的目录结构及介绍

此项目包含了多个子目录,每个子目录代表了一种特定的集成或使用场景:

  1. springboot-mybatis:Seata 与 Spring Boot 和 Mybatis 的集成示例。
  2. spring-boot-multiple-datasource-mybatis-plus:Seata 与 Spring Boot 多数据源以及 MyBatisPlus 的集成实例。
  3. springcloud-nacos-seata:展示了如何在 Spring Cloud 应用中集成 Seata 和阿里云 Nacos。
  4. saga:Saga 模式的分布式事务演示。
  5. dubbo-multiple-datasource-mybatis-plus:Seata 集成 Spring Boot、Apache Dubbo、动态多数据源和 MyBatisPlus,同时使用了 Nacos。

每个子目录下通常包含 pom.xml 文件(构建配置)、应用代码、配置文件等资源。

2. 项目的启动文件介绍

在各个子目录中,你通常会找到一个 Application.java 文件或者类似的名字,这是 Spring Boot 应用的主入口点。例如,在 springboot-mybatis 目录下,你可以运行 com.example.seata.Application 类中的 main 方法以启动该示例应用程序。

对于基于 Spring Cloud 的示例,启动文件可能与 Spring Boot 不同,但原理相同,即寻找并执行项目的主类以启动服务。

3. 项目的配置文件介绍

在 Seata 示例中,主要的配置文件包括 application.propertiesapplication.yml,这些文件位于应用的 src/main/resources 目录下。

- application.properties / application.yml

这些配置文件包含了 Seata 与相关服务(如数据库、注册中心)连接的相关参数。例如,你会看到关于 Seata server 地址 (seata.server.address)、数据库连接信息 (spring.datasource.*) 和注册中心类型与配置 (seata.config.type 和对应设置) 等内容。

- seata.conf

在某些示例中,可能会有一个单独的 seata.conf 文件,这是 Seata 客户端的配置文件,它包含客户端与 Seata 服务器交互时所需的信息。

- bootstrap.propertiesbootstrap.yml

如果是 Spring Cloud 应用,可能会有一个 bootstrap.propertiesbootstrap.yml 文件,这是Spring Cloud 初始化加载的配置文件,通常用来配置服务发现等。

在实际操作之前,请确保根据你的环境修改相应的配置项,比如数据库连接信息、Seata 服务器地址等。

现在,你已经对 incubator-seata-samples 的基本结构和启动方式有了初步了解。接下来,选择一个感兴趣的示例,根据其配置和引导文件进行实践,就可以深入学习 Seata 分布式事务管理的能力了。

incubator-seata-samplesApache Seata(incubating) Samples for Java项目地址:https://gitcode.com/gh_mirrors/inc/incubator-seata-samples

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
假设我们将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(); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

丁群曦Mildred

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值