Nacos+Seata+Feign分布式事务

本文介绍了如何使用Nacos作为服务注册与配置中心,结合Seata和Feign构建分布式事务。文章从Seata官方样例出发,详细讲解了POM文件配置、项目工程结构、数据库表设计、Nacos配置以及Feign的配置,并最终实现了调用成环的全局事务。
摘要由CSDN通过智能技术生成

本篇文章是从Seata官方样例改造而来的分布式事务,Nacos为服务的注册中心及配置中心,这里我采用的组件的版本为:Nacos 2.1.0、Seata 1.5.2

Seata的官方样例库为:https://github.com/seata/seata-samples

样例的结构如下:

启动seata服务让seata服务注册到Nacos上。
在这里插入图片描述

1.POM文件内容
<properties>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
    <spring-cloud.version>2021.0.4</spring-cloud.version>
    <cloud.alibaba.version>2021.0.4.0</cloud.alibaba.version>
    <nacos.version>2.1.0</nacos.version>
    <cloud.version>3.1.2</cloud.version>
    <druid.version>1.2.11</druid.version>
    <dynamic-datasource.version>3.5.1</dynamic-datasource.version>
    <mybatis-plus.version>3.5.2</mybatis-plus.version>
</properties>
<dependencyManagement>
   <dependencies>
      <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-dependencies</artifactId>
          <version>${spring-cloud.version}</version>
          <type>pom</type>
          <scope>import</scope>
      </dependency>
      <dependency>
          <groupId>com.alibaba.cloud</groupId>
          <artifactId>spring-cloud-alibaba-dependencies</artifactId>
          <version>${cloud.alibaba.version}</version>
          <type>pom</type>
          <scope>import</scope>
      </dependency>
      <dependency>
          <groupId>com.alibaba.nacos</groupId>
          <artifactId>nacos-client</artifactId>
          <version>2.1.1</version>
      </dependency>
  </dependencies>
</dependencyManagement>

<dependencies>
  <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-bootstrap</artifactId>
      <version>3.1.4</version>
  </dependency<
使用 Spring BootSpring Cloud Alibaba Seata 实现分布式事务需要以下步骤: 1. 添加依赖:在 pom.xml 中添加 seata-all 和 seata-spring-boot-starter 依赖。 2. 配置 Seata:在 application.properties 或 application.yml 中配置 Seata Server 的地址和端口,以及各个事务组的配置信息。 3. 配置数据源代理:使用 Seata 提供的数据源代理,将原本的数据源替换为 Seata 提供的代理数据源。 4. 配置分布式事务注解:在需要进行分布式事务的方法上添加 @GlobalTransactional 注解。 下面是一个示例 application.yml 配置文件: ```yaml spring: datasource: url: jdbc:mysql://localhost:3306/test username: root password: root driver-class-name: com.mysql.jdbc.Driver seata: enabled: true application-id: demo-app tx-service-group: my_tx_group config: type: nacos nacos: serverAddr: localhost:8848 namespace: public service: vgroupMapping: my_test_tx_group: default groupMapping: my_test_tx_group: "DEMO_GROUP" datasources: ds: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/test username: root password: root type: com.alibaba.druid.pool.DruidDataSource maxPoolSize: 20 minPoolSize: 1 initialSize: 1 maxLifetime: 1800000 validationQuery: select 1 validationQueryTimeout: 30000 testOnBorrow: true testWhileIdle: true testOnReturn: false ``` 在代码中使用 @GlobalTransactional 注解: ```java @Service public class OrderServiceImpl implements OrderService { @Autowired private OrderMapper orderMapper; @Autowired private AccountClient accountClient; @Autowired private StorageClient storageClient; @GlobalTransactional @Override public void create(Order order) { // 1. 创建订单 orderMapper.create(order); // 2. 扣减库存 storageClient.deduct(order.getProductId(), order.getCount()); // 3. 扣减账户余额 accountClient.deduct(order.getUserId(), order.getMoney()); // 4. 修改订单状态 orderMapper.update(order.getUserId(), 0); } } ``` 其中,AccountClient 和 StorageClient 是通过 Feign 实现的远程调用,这些服务也需要添加 Seata 的依赖和配置。 这样就可以实现分布式事务了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值