SpringBoot整合spring-retry实现重试

SpringBoot整合spring-retry实现重试

1. jar导入

  • 除了引入spring-retry外,还需要使用spring-aspects
implementation 'org.springframework.retry:spring-retry'
implementation 'org.springframework:spring-aspects'

2. 启动类添加@EnableRetry注解

package com.example.fisher.gradledemo;

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

import lombok.extern.slf4j.Slf4j;

@EnableRetry
@MapperScan("com.example.fisher.gradledemo.*.dao")
@SpringBootApplication
public class GradleDemoApplication {

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

}

3. service类具体实现

  • @Retryable下参数说明
    recover:指定重试失败后,回调的具体方法
    value:指定出现哪种异常才重试
    maxAttempts:指定方法执行次数,默认是执行3次
    backoff:补偿机制
  • @Backoff下参数说明
    delay:指定执行的延迟时间
    multiplier:延迟时间的倍数
package com.example.fisher.gradledemo.retry.service.impl;

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.example.fisher.gradledemo.retry.service.RetryService;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service("retryService")
public class RetryServiceImpl implements RetryService {

    @Retryable(recover = "recoverMethod", maxAttempts = 3, value = Exception.class,
        backoff = @Backoff(delay = 1000L, multiplier = 2))
    @Override
    public String retry(String msg) {
        log.info("msg={}", msg);
        int i = 1 / 0;
        return msg;
    }

    @Recover
    public String recoverMethod(Exception exception, String msg) {
        log.error("recoverMethod: param={}", msg, exception);
        return "recover";
    }
}

4. controller类

package com.example.fisher.gradledemo.retry.controller;

import com.example.fisher.gradledemo.retry.service.RetryService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("retry")
public class RetryController {

    @Resource
    private RetryService retryService;

    @GetMapping
    public String retryTest(@RequestParam String msg) {
        String retry = retryService.retry(msg);
        return retry;
    }

}

5. 启动项目,调用接口

  • 查看打印

在这里插入图片描述

  • 延迟执行,1s和2s

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值