SpringBoot集成Seata分布式事务OpenFeign远程调用

Docker Desktop 安装Seata Server

seata 本质上是一个服务,用docker安装更方便,配置默认:file

docker run -d --name seata-server -p 8091:8091 -p 7091:7091 seataio/seata-server:2.0.0

与SpringBoot集成

表结构

在这里插入图片描述

项目目录

在这里插入图片描述

dynamic和dynamic2新建user、undo_log表

每个数据库都必须包含undo_log表(user表不是必须的,这里只是演示

CREATE TABLE `undo_log` (
  `branch_id` bigint NOT NULL COMMENT 'branch transaction id',
  `xid` varchar(128) NOT NULL COMMENT 'global transaction id',
  `context` varchar(128) NOT NULL COMMENT 'undo_log context,such as serialization',
  `rollback_info` longblob NOT NULL COMMENT 'rollback info',
  `log_status` int NOT NULL COMMENT '0:normal status,1:defense status',
  `log_created` datetime(6) NOT NULL COMMENT 'create datetime',
  `log_modified` datetime(6) NOT NULL COMMENT 'modify datetime',
  UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`),
  KEY `ix_log_created` (`log_created`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='AT transaction mode undo table';

spring-boot-feign-seata1

pom.xml

		<!-- lombok 1.18.26 -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>provided</scope>
            <optional>true</optional>
        </dependency>
        <!-- jdbc -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <!-- mysql 8.0.32 -->
        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <version>${mysql.version}</version>
            <scope>runtime</scope>
        </dependency>
        <!-- jdbc -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <!-- web 2.7.9 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- MyBatis-Plus 3.5.2 -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
        </dependency>
        <!-- seata 调用方用 openfeign 必须引入spring-cloud-starter-alibaba-seata,否则被调用方获取到的xid可能会为null -->
        <dependency>
            <groupId>io.seata</groupId>
            <artifactId>seata-spring-boot-starter</artifactId>
        </dependency>
        <!-- cloud seata 2.0.0 -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>io.seata</groupId>
                    <artifactId>seata-spring-boot-starter</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- openfeign 3.1.9 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

application.yml

server:
  port: 8088

spring:
  application:
    name: spring-boot-feign-seata1
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    # 数据库必须包含 undo_log 表,如果没有则用 resources 目录下的undo_log.sql 创建
    url: jdbc:mysql://127.0.0.1:3306/dynamic?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&serverTimezone=Asia/Shanghai
    username: root
    password: 123456

seata:
  tx-service-group: my_test_tx_group

SpringBootFeignSeata1Application.java

@MapperScan
@EnableFeignClients
// seata 版本兼容性问题,如果启动报错就要排除 SeataRestTemplateAutoConfiguration 自动配置
@SpringBootApplication(exclude = SeataRestTemplateAutoConfiguration.class)
public class SpringBootFeignSeata1Application {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootFeignSeata1Application.class, args);
    }

}

User.java

@Data
@TableName("user")
public class User {
    @TableId(type = IdType.AUTO)
    private Integer id;
    private String name;
}

UserMapper.java

public interface UserMapper extends BaseMapper<User> {

}

UserController.java

@RestController
@RequiredArgsConstructor
public class UserController {
    private final UserService userService;

    @GetMapping("user")
    public String user(@RequestParam(name = "rollback") boolean rollback) {
        User user = new User();
        user.setName("Meta");
        try {
            userService.insert(rollback, user);
        } catch (Exception e) {
            return "rollback";
        }
        return "success";
    }

}

UserFeignClient.java(openfeign调用spring-boot-feign-seata2)

@FeignClient(name = "spring-boot-feign-seata2", url = "127.0.0.1:8089")
public interface UserFeignClient {

    @GetMapping("user")
    void user();

}

UserService.java(主要的)

@Slf4j
@Service
@RequiredArgsConstructor
public class UserService {
    private final UserMapper userMapper;
    private final UserFeignClient userFeignClient;

    /**
     * 全局事务
     * 1、pom.xml引入seata-spring-boot-starter
     * 2、pom.xml引入spring-cloud-starter-alibaba-seata(排除:seata-spring-boot-starter,因为前面已经引入了。)
     * 3、使用 @GlobalTransactional 全局事务注解开启全局事务控制(默认:Seata AT 模式)
     * 4、被调用方业务层需要引入 @Transactional 本地事务注解
     */
    @GlobalTransactional(rollbackFor = Exception.class)
    public void insert(boolean rollback, User user) {
        log.info("seata1 xid = {}", RootContext.getXID());
        userMapper.insert(user);
        userFeignClient.user();
        if (rollback) {
            throw new RuntimeException("rollback");
        }
    }
}

spring-boot-feign-seata2

pom.xml和spring-boot-feign-seata是一样的

application.yml

server:
  port: 8089

spring:
  application:
    name: spring-boot-feign-seata2
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    # 数据库必须包含 undo_log 表,如果没有则用 resources 目录下的undo_log.sql 创建
    url: jdbc:mysql://127.0.0.1:3306/dynamic2?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&serverTimezone=Asia/Shanghai
    username: root
    password: 123456

seata:
  tx-service-group: my_test_tx_group

SpringBootFeignSeata2Application.java

@MapperScan
// 被调用端引用 seata 默认 AT 模式
@EnableAutoDataSourceProxy
// seata 版本兼容性问题,如果启动报错就要排除 SeataRestTemplateAutoConfiguration 自动配置
@SpringBootApplication(exclude = SeataRestTemplateAutoConfiguration.class)
public class SpringBootFeignSeata2Application {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootFeignSeata2Application.class, args);
    }

}

UserController.java

@RestController
@RequiredArgsConstructor
public class UserController {
    private final UserService userService;

    @GetMapping("user")
    public void user() {
        User user = new User();
        user.setName("Meta2");
        userService.insert(user);
    }

}

UserService.java

@Slf4j
@Service
@RequiredArgsConstructor
public class UserService {
    private final UserMapper userMapper;

    /**
     * 调用方用 openfeign 必须引入spring-cloud-starter-alibaba-seata,否则被调用方获取到的xid可能会为null
     * PS:被调用方可以不用引入
     */
    @Transactional(rollbackFor = Exception.class)
    public void insert(User user) {
        log.info("seata2 xid = {}", RootContext.getXID());
        userMapper.insert(user);
    }

}

上面重复的文件就不说了

启动两个项目

访问localhost:8088/user?rollback=true

返回rollback,2个数据库都没有新增数据库,验证分布式(AT)事务回滚成功

访问localhost:8088/user?rollback=false

返回success,2个数据库都新增了数据,验证分布式(AT)事务成功

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: Se是一款分布式事务解决方案,可以帮助开发人员简化分布式事务的开发和管理。在SpringBoot集成Seata可以帮助我们更方便地使用Seata。 下面是集成Seata的步骤: 1. 添加Seata依赖 在pom.xml中添加Seata的依赖: ``` <dependency> <groupId>io.seata</groupId> <artifactId>seata-all</artifactId> <version>1.4.2</version> </dependency> ``` 2. 配置SeataSpringBoot的配置文件中,配置Seata的相关参数: ``` spring: application: name: xxx-service cloud: alibaba: seata: tx-service-group: my_test_tx_group # 事务组名称,需要与seata-server中的配置一致 enabled: true # 启用seata application-id: ${spring.application.name} # 应用ID,需要与seata-server中的配置一致 tx-service-group: my_test_tx_group # 事务组名称,需要与seata-server中的配置一致 config: type: nacos # 配置中心类型,可以是nacos、file、apollo等 nacos: server-addr: ${seata.server.ip}:${seata.server.port} # nacos配置中心地址,需要配置seata.server.ip和seata.server.port group: SEATA_GROUP # nacos配置中心中的group namespace: ${seata.namespace} # nacos配置中心中的namespace file: name: file.conf # 配置文件名称,需要与seata-server中的文件名一致 apollo: appId: ${seata.appId} # apollo的AppId namespace: ${seata.namespace} # apollo的namespace config-service-url: ${seata.configServiceUrl} # apollo的配置中心地址 cluster-name: ${seata.clusterName} # apollo的集群名称 namespace: ${seata.namespace} # apollo的namespace ``` 3. 配置数据源 在使用Seata时,我们需要使用Seata提供的数据源来管理分布式事务。因此,在配置数据源时,我们需要使用Seata提供的数据源。 ``` @Bean @ConfigurationProperties(prefix = "spring.datasource") public DataSource dataSource() { return new DruidDataSource(); } @Bean public DataSourceProxy dataSourceProxy(DataSource dataSource) { return new DataSourceProxy(dataSource); } ``` 4. 配置Mybatis 在使用Mybatis时,我们需要配置Mybatis的拦截器,以便Seata能够拦截到Mybatis的事务操作。 ``` @Configuration public class MybatisConfig { @Bean public ConfigurationCustomizer configurationCustomizer() { return configuration -> configuration.addInterceptor(new SeataInterceptor()); } } ``` 5. 配置Seata事务管理器 在SpringBoot中,我们可以使用Seata提供的注解来管理分布式事务。因此,我们需要配置Seata事务管理器。 ``` @Bean public GlobalTransactionScanner globalTransactionScanner() { return new GlobalTransactionScanner("xxx-service", "my_test_tx_group"); } ``` 以上就是在SpringBoot集成Seata的步骤。通过集成Seata,我们可以更方便地管理分布式事务,提高应用的可靠性和稳定性。 ### 回答2: Spring Boot是一种用于简化创建基于Spring框架的应用程序的框架,而Seata是一种分布式事务解决方案。集成Seata可以帮助我们在分布式环境下更好地处理事务问题。 首先,要将Seata集成Spring Boot中,我们需要在项目的pom.xml文件中添加Seata的依赖。然后,在Spring Boot的配置文件中,我们需要配置Seata的相关属性,如注册中心的地址、数据源的代理等。 一旦配置完成,我们就可以在Spring Boot应用程序中使用Seata来处理分布式事务了。在需要进行事务处理的方法上,我们可以使用Seata提供的@Transactional注解来标识,这样Seata就会自动为我们管理事务。此外,Seata还提供了一些其他的注解,如@Compensable注解用于标识可补偿的方法。 当我们使用Seata进行分布式事务处理时,事务管理器会自动将所有涉及到的数据源和资源加入到同一个事务中。如果任何一个操作失败,Seata会自动回滚整个事务,保证数据的一致性。 此外,Seata还提供了一些其他的功能,如分布式事务的日志记录和补偿机制。通过这些功能,我们可以更好地管理分布式事务,并且保证数据的可靠性和一致性。 总之,通过将Seata集成Spring Boot应用程序中,我们可以更好地处理分布式环境下的事务问题。Seata提供了一系列的功能和注解,帮助我们管理分布式事务,并且保证数据的一致性。 ### 回答3: Spring Boot是一种使用Java编写的开源框架,用于快速构建独立的、可部署的、生产级的应用程序。而Seata是一个开源的分布式事务解决方案,可用于解决微服务架构下的数据一致性问题。下面将介绍如何将Seata集成Spring Boot中。 首先,在Spring Boot项目中添加Seata的依赖项。可以通过在项目的构建配置文件pom.xml中添加相应的依赖来实现。这些依赖将引入Seata的核心组件,例如分布式事务协调器、代理等。 然后,配置Seata的相关属性。通过在Spring Boot项目的配置文件中添加Seata相关的配置,如事务组名称、事务日志存储模式、代理类型等。这些配置将告诉Seata如何与项目进行交互和协作。 接下来,编写业务逻辑。在Spring Boot项目中,编写业务逻辑的方式与平常一样,不同之处在于需要在涉及到数据库操作的地方添加Seata分布式事务注解。这些注解将告诉Seata哪些操作需要参与到分布式事务中,以保证数据的一致性。 最后,启动项目并验证集成效果。在项目启动后,Seata会根据配置信息自动启动并创建相应的事务组。在执行业务逻辑时,Seata将根据注解的配置来管理事务的开启、提交和回滚,以保证数据的一致性和可靠性。 综上所述,通过以上步骤,我们可以将Seata集成Spring Boot项目中,实现分布式事务的管理和控制。这对于构建高可靠性的微服务架构是非常有帮助的。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值