SpringClouAlibab整合seata分布式事务搭建
·
整合Spring Cloud Alibaba 2025.0.0与Seata 2.0及MyBatis-Plus的完整教程
Spring Cloud Alibaba小白领基础学全栈教程必看
环境准备
- JDK 17+
- Maven 3.8+
- Spring Boot 3.2.0
- Spring Cloud Alibaba 2025.0.0
- Seata 2.0.0
- MyBatis-Plus 3.5.6
- Nacos 2.3.0(注册中心与配置中心)
项目依赖配置
在父工程pom.xml中定义版本管理:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2025.0.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
</dependencyManagement>
子模块中添加具体依赖:
<dependencies>
<!-- Spring Cloud Alibaba -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- Seata -->
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
</dependency>
<!-- MyBatis-Plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.6</version>
</dependency>
</dependencies>
Seata Server部署
下载Seata 2.0.0并解压,修改conf/application.yml:
seata:
config:
type: nacos
nacos:
server-addr: 127.0.0.1:8848
namespace: seata
group: SEATA_GROUP
registry:
type: nacos
nacos:
application: seata-server
server-addr: 127.0.0.1:8848
namespace: seata
初始化Nacos配置:
sh ${SEATA_HOME}/script/config-center/nacos/nacos-config.sh -h 127.0.0.1 -p 8848 -g SEATA_GROUP
启动Seata Server:
sh ${SEATA_HOME}/bin/seata-server.sh
业务系统配置
application.yml配置示例:
spring:
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
datasource:
url: jdbc:mysql://localhost:3306/seata_demo?useSSL=false
username: root
password: 123456
seata:
application-id: ${spring.application.name}
tx-service-group: my_test_tx_group
service:
vgroup-mapping:
my_test_tx_group: default
registry:
type: nacos
nacos:
server-addr: 127.0.0.1:8848
namespace: seata
MyBatis-Plus集成
配置数据源代理:
@Configuration
public class DataSourceConfig {
@Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource() {
return new DruidDataSource();
}
@Primary
@Bean("dataSourceProxy")
public DataSourceProxy dataSourceProxy(DataSource dataSource) {
return new DataSourceProxy(dataSource);
}
@Bean
public SqlSessionFactory sqlSessionFactory(DataSourceProxy dataSourceProxy) throws Exception {
MybatisSqlSessionFactoryBean factory = new MybatisSqlSessionFactoryBean();
factory.setDataSource(dataSourceProxy);
return factory.getObject();
}
}
分布式事务测试
定义服务层方法:
@Service
public class OrderService {
@GlobalTransactional
public void createOrder(Order order) {
// 1. 创建订单
orderMapper.insert(order);
// 2. 调用库存服务扣减库存
storageFeignClient.deduct(order.getProductId(), order.getCount());
// 3. 调用账户服务扣款
accountFeignClient.debit(order.getUserId(), order.getMoney());
}
}
异常回滚测试
模拟业务异常触发回滚:
@GlobalTransactional
public void createOrderWithException(Order order) {
orderMapper.insert(order);
storageFeignClient.deduct(order.getProductId(), order.getCount());
// 模拟异常
int i = 1/0;
}
关键注意事项
- 确保所有微服务的
seata.tx-service-group命名一致 - 每个涉及分布式事务的数据库都需要创建undo_log表
- Feign调用时需要传递XID(通过Seata的自动配置已实现)
- 生产环境建议使用Seata的高可用模式
通过以上步骤即可完成Spring Cloud Alibaba 2025.0.0与Seata 2.0及MyBatis-Plus的完整集成,实现分布式事务控制。
更多推荐
所有评论(0)