重学SpringBoot3-Spring Retry实践

更多SpringBoot3内容请关注我的专栏:《SpringBoot3》

期待您的点赞👍收藏⭐评论✍

重学SpringBoot3-Spring Retry实践

1. 简介

Spring Retry是Spring生态系统中的一个重要组件,它提供了自动重试失败操作的能力。在分布式系统中,由于网络抖动、服务暂时不可用等临时性故障,重试机制显得尤为重要。本文将详细介绍如何在 SpringBoot 3 应用中集成和使用 Spring Retry。

2. 环境准备

首先在 SpringBoot 3 项目中添加必要的依赖:

xml 复制代码
<dependency>
    <groupId>org.springframework.retry</groupId>
    <artifactId>spring-retry</artifactId>
    <version>2.0.5</version>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aspects</artifactId>
    <version>6.1.13</version>
</dependency>

在启动类或配置类上添加 @EnableRetry 注解以启用重试功能:

java 复制代码
@SpringBootApplication
@EnableRetry
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

3. 使用方式

3.1 注解方式

基础使用

最简单的使用方式是通过 @Retryable 注解:

java 复制代码
@Service
public class UserService {
    
    @Retryable
    public void riskyOperation() {
        // 可能失败的操作
    }
}

自定义重试策略

可以通过 @Retryable 注解的参数来自定义重试行为:

java 复制代码
@Service
@Slf4j
public class EmailServiceImpl implements IEmailService {

    @Resource
    private JavaMailSender mailSender;

    @Value("${spring.mail.username}")
    private String from;

    /**
     * 发送简单文本邮件
     *
     * @param to
     * @param subject
     * @param text
     */
    @Override
    @Retryable(retryFor = MailSendException.class, maxAttempts = 3, backoff = @Backoff(delay = 1000))
    public void sendSimpleEmail(String to, String subject, String text) {
        try {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(from);
            message.setTo(to);
            message.setSubject(subject);
            message.setText(text);
            mailSender.send(message);
            log.info("Simple email sent successfully to: {}", to);
        } catch (Exception e) {
            log.error("Failed to send simple email", e);
            throw new MailSendException("Failed to send email", e);
        }
    }
}

当执行发生指定异常,将会尝试进行重试,一旦达到最大尝试次数,但仍有异常发生,就会抛出 ExhaustedRetryException。重试最多可进行三次,两次重试之间的延迟时间默认为一秒。

失败恢复机制

使用 @Recover 注解定义重试失败后的恢复方法:

java 复制代码
    /**
     * 发送简单文本邮件
     *
     * @param to
     * @param subject
     * @param text
     */
    @Override
    @Retryable(retryFor = MailSendException.class, // 指定异常类型
            maxAttempts = 3, // 最大重试次数
            backoff = @Backoff(delay = 1000) // 指定退避策略,例如延迟时间
    )
    public void sendSimpleEmail(String to, String subject, String text) {
        try {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(from);
            message.setTo(to);
            message.setSubject(subject);
            message.setText(text);
            mailSender.send(message);
            log.info("Simple email sent successfully to: {}", to);
        } catch (Exception e) {
            log.error("Failed to send simple email", e.getMessage());
            throw new MailSendException("Failed to send email", e);
        }
    }

    @Recover
    public void recover(MailSendException e, String param) {
        // 处理最终失败的情况
        log.error("Final recovery : {}", param);
    }

重试和失败恢复效果

注意事项

注意@Recover 失效的情况:

  • @Recover 方法的参数类型与实际异常不匹配;
  • @Recover 方法的返回类型与 @Retryable 方法不一致;
  • @Recover 方法的其他参数与 @Retryable 方法参数不匹配。

3.2 编程式使用

除了注解方式,Spring Retry 还提供了 RetryTemplate 用于编程式重试:

java 复制代码
@Configuration
public class RetryConfig {
    
    @Bean
    public RetryTemplate retryTemplate() {
        RetryTemplate template = new RetryTemplate();
        
        // 配置重试策略
        SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
        retryPolicy.setMaxAttempts(3);
        
        // 配置退避策略
        FixedBackOffPolicy backOffPolicy = new FixedBackOffPolicy();
        backOffPolicy.setBackOffPeriod(1000L);
        
        template.setRetryPolicy(retryPolicy);
        template.setBackOffPolicy(backOffPolicy);
        
        return template;
    }
}

使用RetryTemplate:

java 复制代码
@Service
public class UserService {
    
    @Autowired
    private RetryTemplate retryTemplate;
    
    public void executeWithRetry() {
        retryTemplate.execute(context -> {
            // 需要重试的业务逻辑
            return null;
        });
    }
}

3.3 监听重试过程

通过实现RetryListener接口,可以监听重试的整个生命周期:

java 复制代码
public class CustomRetryListener extends RetryListenerSupport {
    
    @Override
    public <T, E extends Throwable> void onError(RetryContext context, 
            RetryCallback<T, E> callback, Throwable throwable) {
        // 记录错误日志
        log.error("Retry error occurred", throwable);
    }
    
    @Override
    public <T, E extends Throwable> void close(RetryContext context,
            RetryCallback<T, E> callback, Throwable throwable) {
        // 重试结束时的处理
        log.info("Retry completed");
    }
}

将监听器注册到RetryTemplate:

java 复制代码
@Configuration
public class RetryConfig {
    
    @Bean
    public RetryTemplate retryTemplate() {
        RetryTemplate template = new RetryTemplate();
        // ... 其他配置 ...
        template.registerListener(new CustomRetryListener());
        return template;
    }
}

监听重试效果

4. 最佳实践

  1. 明确重试场景:只对临时性故障使用重试机制,对于业务错误或永久性故障应直接失败。

  2. 设置合理的重试次数:通常3-5次即可,过多的重试可能会加重系统负担。

  3. 使用退避策略:建议使用指数退避策略(ExponentialBackOffPolicy),避免立即重试对系统造成冲击。

  4. 添加监控和日志:通过RetryListener记录重试情况,便于问题排查。

  5. 设置超时时间:避免重试过程持续时间过长。

5. 总结

Spring Retry为Spring应用提供了强大而灵活的重试机制,既可以通过注解优雅地实现重试,也可以使用RetryTemplate进行更细粒度的控制。在实际应用中,合理使用重试机制可以提高系统的健壮性和可用性。

需要注意的是,重试机制并非万能药,在使用时要根据具体场景选择合适的重试策略,并做好监控和告警,以便及时发现和处理问题。

相关推荐
飞天狗1111 小时前
零基础JavaWeb入门——第五课第二小节:九大内置对象 · 第2个:response(响应对象)
java·开发语言
许彰午1 小时前
39_Java单元测试JUnit入门
java·junit·单元测试
shushangyun_1 小时前
2026年快消品B2B系统推荐:支持终端门店订货、促销政策自动化的工具?
java·运维·网络·数据库·人工智能·spring·自动化
JAVA9651 小时前
JAVA面试-JVM篇 03-JVM运行时数据区哪些是线程私有的哪些是共享的
java·jvm·面试
于先生吖1 小时前
教育类Java实战项目:在线错题整理平台分层架构设计与接口源码解析
java·开发语言
慧一居士1 小时前
Feign的GET请求如何传递对象参数?
java·spring cloud
ofoxcoding1 小时前
在AI API聚合平台配置DeepSeek V3.2提示词缓存实战:快速接入与成本优化指南
人工智能·spring·缓存·ai
开发小能手-roy2 小时前
Java集合框架选型指南:从ArrayList到ConcurrentSkipListMap
java·开发语言
凡人叶枫2 小时前
Effective C++ 条款41:了解隐式接口和编译期多态
java·开发语言·c++·effective c++
凡人叶枫2 小时前
Effective C++ 条款42:了解 typename 的双重意义
java·linux·服务器·c++