Spring Boot集成 Spring Retry 实现容错重试机制并附源码

在这里插入图片描述

😄 19年之后由于某些原因断更了三年,23年重新扬帆起航,推出更多优质博文,希望大家多多支持~
🌷 古之立大事者,不惟有超世之才,亦必有坚忍不拔之志
🎐 个人CSND主页——Micro麦可乐的博客
🐥《Docker实操教程》专栏以最新的Centos版本为基础进行Docker实操教程,入门到实战
🌺《RabbitMQ》专栏主要介绍使用JAVA开发RabbitMQ的系列教程,从基础知识到项目实战
🌸《设计模式》专栏以实际的生活场景为案例进行讲解,让大家对设计模式有一个更清晰的理解
💕《Jenkins实战》专栏主要介绍Jenkins+Docker的实战教程,让你快速掌握项目CI/CD,是2024年最新的实战教程
🌞《Spring Boot》专栏主要介绍我们日常工作项目中经常应用到的功能以及技巧,代码样例完整
如果文章能够给大家带来一定的帮助!欢迎关注、评论互动~

1、前言

本文对应源码下载地址: https://download.csdn.net/download/lhmyy521125/89430153 无需积分

在日常开发过程中,我们经常会与第三方接口进行交互,例如:短信发送远程服务调用争抢锁等场景,当正常调用发生异常时,例如:网络延迟服务宕机临时故障等问题,会导致本次请求交互失败,而借助 Spring Retry 能够帮助我们在方法调用失败时自动重试,从而提高系统的稳定性和健壮性。

本文跟着博主由浅入深一起来学习 Spring Retry
在这里插入图片描述

2、什么是 Spring Retry?

Spring Retry 是一个用于简化 Java 方法重试逻辑的库,它能够在方法调用失败时自动重试,并提供了丰富的配置选项,支持重试次数、重试间隔时间、异常类型等配置。通过使用 Spring Retry,可以方便地在 Spring Boot 应用中实现容错和重试机制。

Spring Retry 的特性

  • 自动重试:当方法调用失败时,根据配置的重试策略自动重试
  • 支持多种异常类型:可以配置在遇到特定异常时重试,如 IOExceptionSQLException
  • 重试间隔控制:支持配置重试间隔时间,可以设置固定间隔或指数增长间隔
  • 自定义重试策略:提供了灵活的重试策略接口,可以实现自定义的重试逻辑

3、开始简单集成

构建你的 Spring Boot 项目,在pom.xml中引入依赖

<dependency>
    <groupId>org.springframework.retry</groupId>
    <artifactId>spring-retry</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

配置 Spring Retry 可以通过注解或配置文件进行配置。下面展示如何使用注解方式配置

使用@EnableRetry注解开启
在主类 或对应配置类上加@EnableRetry注解,表示启用重试机制

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

使用 @Retryable 注解
@Retryable 注解是 Spring Retry 的核心注解,用于标记需要重试的方法。示例代码如下:

import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;

@Service
public class RetryService {

	//初始化重试次数
    private int attempt = 0;

    @Retryable(value = {RuntimeException.class}, maxAttempts = 5, backoff = @Backoff(delay = 2000))
    public String retryMethod() {
        attempt++;
        System.out.println("重试次数Attempt: " + attempt);
        if (attempt < 3) {
            throw new RuntimeException("出现故障, 重试中...");
        }
        return "Success!";
    }
}

配置参数说明
value 指定了需要重试的异常类型,这里是 RuntimeException
maxAttempts 设置了最大重试次数,这里是 5 次
backoff 设置了重试间隔,使用 @Backoff 注解指定延迟时间,单位是毫秒,这里是 2000 毫秒(2秒)

创建一个controller,用于调用重试方法,并测试重试逻辑:

import com.example.demo.service.RetryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController

public class RetryController {
    @Autowired
    private RetryService retryService;

    @GetMapping("/retry")
    public String retry() {
        try {
            return retryService.retryMethod();
        } catch (Exception e) {
            return "重试失败: " + e.getMessage();
        }
    }
}

接口请求测试
在这里插入图片描述

观察控制台
在这里插入图片描述
测试你会发现接口请求过程中,控制台一直输出了三次重试次数后成功返回,你以为Spring Retry 就这么点东西?我们继续往下看

4、Spring Retry的高级配置

❶ 自定义重试策略

在某些情况下,默认的重试策略可能无法满足需求,这时可以通过实现 RetryPolicy 接口来自定义重试策略,控制重试的逻辑和条件

import org.springframework.retry.RetryContext;
import org.springframework.retry.RetryPolicy;

public class CustomRetryPolicy implements RetryPolicy {
	
    private static final int MAX_RETRIES = 3;
    @Override
    public boolean canRetry(RetryContext context) {
        // 检查重试次数
        int retryCount = context.getRetryCount();
        if (retryCount >= MAX_RETRIES) {
            return false; // 如果重试次数达到最大值,不再重试
        }
        // 检查异常类型
        Throwable lastException = context.getLastThrowable();
        if (lastException instanceof MyCustomException) {
            // 如果是我们关心的特定异常类型,允许重试
            return true;
        }
        // 其他情况下不允许重试
        return false;
    }

    //MyCustomException 是你定义的异常类型
    public class MyCustomException extends Exception { /* ... */ }
	
	//在重试操作开始时创建新的上下文对象,用于存储重试信息
	@Override
    public RetryContext open(RetryContext retryContext) {
        return null;
    }

	//在重试操作结束时执行清理或记录操作
    @Override
    public void close(RetryContext retryContext) {

    }

	//在重试过程中遇到异常时记录异常信息
    @Override
    public void registerThrowable(RetryContext retryContext, Throwable throwable) {

    }
}

canRetry方法的介绍

boolean canRetry(RetryContext context);

canRetry 方法是重试策略的核心逻辑,通过检查当前的重试上下文信息,决定是否可以继续进行重试。通常会根据重试次数、异常类型等条件来判断是否继续重试

作用:判断是否应该继续进行重试操作
参数:RetryContext context,表示当前的重试上下文,包含了重试次数、上次异常等信息
返回值:boolean,返回 true 表示可以继续重试,返回 false 表示停止重试

open方法的介绍

RetryContext open(RetryContext parent);

在重试操作开始时,Spring Retry 会调用 open 方法创建一个新的 RetryContext,这个上下文对象会在整个重试过程中传递,用于存储重试相关的信息

作用:创建并返回一个新的 RetryContext 对象,表示一次新的重试操作的上下文信息
参数:RetryContext parent,表示父级上下文,如果没有父级上下文则为 null
返回值:RetryContext,表示新的重试上下文

close方法的介绍

当重试操作完成后,无论是否成功,Spring Retry 都会调用 close 方法,可以在这个方法中执行一些清理操作或记录日志

void close(RetryContext context);

作用:在重试操作完成时(无论成功或失败)调用,用于清理或记录重试操作的结束
参数:RetryContext context,表示当前的重试上下文

registerThrowable方法的介绍

void registerThrowable(RetryContext context, Throwable throwable);

在重试过程中,如果遇到异常,Spring Retry 会调用 registerThrowable 方法,将异常信息记录到 RetryContext 中。这个信息可以在后续的重试操作中使用

作用:在重试过程中遇到异常时调用,用于记录当前的异常信息
参数:RetryContext context,表示当前的重试上下文
参数:Throwable throwable,表示当前遇到的异常

Spring配置中使用自定义 RetryPolicy ,同时也可以配置定制 RetryTemplate

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.retry.backoff.FixedBackOffPolicy;
import org.springframework.retry.support.RetryTemplate;

@Configuration
public class RetryConfig {

    @Bean
    public RetryTemplate retryTemplate() {
        RetryTemplate retryTemplate = new RetryTemplate();

        // 设置自定义的RetryPolicy
        CustomRetryPolicy retryPolicy = new CustomRetryPolicy(5);

        // 还可以设置其他的策略,如BackoffPolicy等  
        FixedBackOffPolicy backOffPolicy = new FixedBackOffPolicy();
        backOffPolicy.setBackOffPeriod(2000);

        retryTemplate.setRetryPolicy(retryPolicy);
        retryTemplate.setBackOffPolicy(backOffPolicy);

        return retryTemplate;
    }
}

❷ 使用 RetryTemplate

RetryTemplateSpring Retry 提供的一个模板类,允许我们更细粒度地控制重试逻辑。下面是使用 RetryTemplate 的例子

import org.springframework.retry.support.RetryTemplate;
import org.springframework.stereotype.Service;

@Service
public class TemplateRetryService {
    private final RetryTemplate retryTemplate;

    public TemplateRetryService(RetryTemplate retryTemplate) {
        this.retryTemplate = retryTemplate;
    }

    public String executeWithRetry() {
        return retryTemplate.execute(context -> {
            System.out.println("TemplateRetryService执行重试..");
            if (Math.random() < 0.7) {
                throw new RuntimeException("重试失败");
            }
            return "Success!";
        });
    }
}

5、代码汇总

Spring Retry 也是可以与 Spring AOP 配合使用,通过切面拦截方法调用,实现全局的重试逻辑,只需要创建一个切面即可

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class RetryAspect {

    @Before("@annotation(org.springframework.retry.annotation.Retryable)")
    public void beforeRetry() {
        System.out.println("执行重试前...");
        //比如记录请求发起的时间等
    }
}

讲解了这么多,我们还是把代码汇总一下,进行一次演示,编写一个Controller用来测试两个重试服务

@RestController
@RequestMapping("/api")
public class RetryController {
    @Autowired
    private RetryService retryService;

    @Autowired
    private TemplateRetryService templateRetryService;

    @GetMapping("/retry")
    public String retry() {
        try {
            return retryService.retryMethod();
        } catch (Exception e) {
            return "重试失败: " + e.getMessage();
        }
    }

    @GetMapping("/template-retry")
    public String templateRetry() {
        return templateRetryService.executeWithRetry();
    }
}

最终代码结构如下:(目的只是为了演示忽略都在一个包中)

在这里插入图片描述

再次运行测试,分别测试 /api/retry 接口以及模板重试服务的接口 /api/template-retry,结果如下:

在这里插入图片描述

OK,至此 Spring Retry 的介绍以及使用已经讲完了,小伙伴们可以根据代码片段自行定制自己的重试策略

6、总结

Spring Retry 是一个强大的工具,能够帮助我们在系统出现临时故障时自动重试,提高系统的稳定性和健壮性。在实际开发中,根据具体的业务需求和系统情况,合理配置和使用 Spring Retry,可以显著提升系统的可靠性。

本文的代码主要是演示使用,小伙伴们可以根据自己业务需求进行修改升级。如果本文对您有所帮助,希望 一键三连 给博主一点点鼓励,如果您有任何疑问或建议,请随时留言讨论


在这里插入图片描述

  • 63
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 46
    评论
Spring Boot是一个快速开发框架,而RocketMQ是一个可靠的消息队列系统。在使用Spring Boot集成RocketMQ时,可以采用重试机制来处理发送失败的消息。 对于消息发送失败的情况,RocketMQ提供了重试机制来确保消息的可靠传输。在Spring Boot中,可以使用RocketMQ的Retry Service来处理这些失败的消息。 首先,需要配置RocketMQ的重试参数,包括重试次数和重试间隔时间。可以通过配置文件或代码来设置这些参数。重试次数和重试间隔时间的设置应该根据实际情况来决定,以确保消息能够成功发送。 接下来,可以在消息发送失败的情况下使用RocketMQ的Retry Service来重试发送消息。可以通过监听RocketMQ的回调接口来获取发送失败的消息,并在回调方法中进行重试操作。重试操作可以通过重新发送消息来实现,直到消息成功发送为止。 在重试过程中,可以根据具体的业务需求进行一些额外的处理。例如,可以记录重试次数和重试时间,以便后续分析和监控。 需要注意的是,在进行消息重试时,应该考虑到可能出现的重复消息的问题。可以通过消息的唯一标识来进行去重处理,确保不会重复发送相同的消息。 总之,Spring Boot集成RocketMQ可以很方便地实现消息的重试机制。通过配置重试参数和使用RocketMQ的Retry Service,可以确保消息的可靠传输,提高系统的稳定性和可靠性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Micro麦可乐

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

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

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

打赏作者

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

抵扣说明:

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

余额充值