Spring Retry -- 重试机制

£神魔★判官ぃ 2023-10-05 20:14 141阅读 0赞
1. 简介

  Spring Retry是从Spring Batch独立出来的一个功能,主要实现了重试和熔断。
  在一般业务中,需要重试的场景有很多,比如网络中断,连接超时时,可能需要重试机制进行重试或者熔断(终止重试)。Spring Retry提供了丰富的重试功能,只需简单配置即可实现。

2. 示例代码
  • 创建工程
    cd69d7e73860294cb5d413d1d1a1da46.png
  • 修改pom.xml


    4.0.0
    com.c3stones
    spring-retry-demo
    0.0.1-SNAPSHOT
    spring-retry-demo
    Spring Retry Demo


    org.springframework.boot
    spring-boot-starter-parent
    2.3.4.RELEASE




    org.springframework.retry
    spring-retry


    org.aspectj
    aspectjweaver


    org.springframework.boot
    spring-boot-starter-web






    org.springframework.boot
    spring-boot-maven-plugin




  • 创建自定义异常类

    /**

    • 自定义业务异常
    • @author CL
      /
      public class BusinessException extends RuntimeException {

      private static final long serialVersionUID = 1L;

      public BusinessException() {

      1. super();

      }

      public BusinessException(String message) {

      1. super(message);

      }

    }

  • 创建Service

    /**

    • 发送Service
    • @author CL
      /
      public interface SendService {

      /**

      • 发送短信
      • @return
        */
        String sms();

    }

  • 创建Service实现类

    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.retry.annotation.Backoff;
    import org.springframework.retry.annotation.Recover;
    import org.springframework.retry.annotation.Retryable;
    import org.springframework.stereotype.Service;

    import com.c3stones.exceptions.BusinessException;
    import com.c3stones.service.SendService;

    /**

    • 发送Service 实现
    • @author CL
      /
      @Service
      public class SendServiceImpl implements SendService {

      private static final Logger logger = LoggerFactory.getLogger(SendServiceImpl.class);

      /**

      • 发送短信
      • @Retryable : 需要重试
      • value : 当抛出指定异常时,开始重试
      • maxAttempts : 最大重试次数,默认为3
      • @Backoff : 重试中的退避策略
      • delay : 延迟时间,默认为0
      • maxDelay : 最大延迟时间,默认为0
      • multiplier : 此次延时时间和上一次延迟时间的倍数,默认为0

      • */
        @Override
        @Retryable(value = BusinessException.class, maxAttempts = 3, backoff = @Backoff(delay = 1000, maxDelay = 10000, multiplier = 2))
        public String sms() {
        logger.info(“开始发送短信!”);
        throw new BusinessException(“发送短信异常”);
        }

        /**

      • 兜底方法,即多次重试后仍失败则执行此方法
      • @param e
      • @return
        */
        @Recover
        public String recover(BusinessException e) {
        logger.info(“重试发送失败”);
        return e.getMessage();
        }

    }

  • 创建Controller

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;

    import com.c3stones.service.SendService;

    /**

    • 发送Controller
    • @author CL
      /
      @RestController
      @RequestMapping(value = “/send”)
      public class SendController {

      @Autowired
      private SendService sendService;

      /**

      • 发送短信
      • @return
        */
        @RequestMapping(value = “”)
        public String send() {
        return sendService.sms();
        }

    }

  • 创建启动类

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.retry.annotation.EnableRetry;

    /**

    • 启动类
    • @EnableRetry : 开启重试机制
    • @author CL
      /
      @SpringBootApplication
      @EnableRetry
      public class Application {

      public static void main(String[] args) {

      1. SpringApplication.run(Application.class, args);

      }

    }

  • 启动项目

3. 测试
  • 访问发送接口

    curl http://127.0.0.1:8080/send

    返回:

    1. 发送短信异常
  • 控制台输出日志

    2021-02-21 16:58:40.808 INFO 9920 —- [nio-8080-exec-1] c.c3stones.service.impl.SendServiceImpl : 开始发送短信!
    2021-02-21 16:58:41.810 INFO 9920 —- [nio-8080-exec-1] c.c3stones.service.impl.SendServiceImpl : 开始发送短信!
    2021-02-21 16:58:43.810 INFO 9920 —- [nio-8080-exec-1] c.c3stones.service.impl.SendServiceImpl : 开始发送短信!
    2021-02-21 16:58:43.810 INFO 9920 —- [nio-8080-exec-1] c.c3stones.service.impl.SendServiceImpl : 重试发送失败
    2021-02-21 16:58:43.811 INFO 9920 —- [nio-8080-exec-1] com.c3stones.controller.SendController : 总耗时:3 s

4. 项目地址

  spring-retry-demo

发表评论

表情:
评论列表 (有 0 条评论,141人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Spring Retry机制

    在调用第三方接口或者使用mq时,会出现网络抖动,连接超时等网络异常,所以需要重试。为了使处理更加健壮并且不太容易出现故障,后续的尝试操作,有时候会帮助失败的操作最后执行成功。例

    相关 Retry机制

    1、业务场景        应用中需要实现一个功能: 需要将数据上传到远程存储服务,同时在返回处理成功情况下做其他操作。这个功能不复杂,分为两个步骤:第一步调用远程的Rest