SpringBoot 一个注解,优雅的实现重试机制retry

1:POM配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.3</version>
<!--        <version>2.3.5.RELEASE</version>-->
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.mail</groupId>
    <artifactId>elegant</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>elegant</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.retry</groupId>
            <artifactId>spring-retry</artifactId>
            <version>1.3.4</version>
        </dependency>

        <dependency>
            <groupId>org.aspectj</groupId >
            <artifactId>aspectjweaver</artifactId >
<!--            <version>1.6.11</version >-->
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2:yml配置

server:
  port: 8080
  servlet:
    context-path: /

3:启动类

@EnableRetry
@SpringBootApplication
public class ReTryDemoApplication {
    
    

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

}

4:controller

@RestController
public class TestController {
    
    

    @Autowired
    TestRetryService testRetryServiceImpl;

    @GetMapping("/testRetry")
    public String testRetry() throws Exception {
    
    
        int code=0;

        int result = testRetryServiceImpl.dignifiedTest(code);
        return "result:"+result;
    }

}

5:service

public interface TestRetryService {
    
    
    int dignifiedTest(int code) throws Exception;
}

6:serviceImpl

@Service
public class TestRetryServiceImpl implements TestRetryService {
    
    
    @Override
    @Retryable(value = Exception.class,maxAttempts = 3,backoff = @Backoff(delay = 2000,multiplier = 1.5))
    public int dignifiedTest(int code) throws Exception{
    
    
        System.out.println("dignifiedTest被调用,时间:"+ LocalTime.now());
        if (code==0){
    
    
            throw new Exception("情况不对头!");
        }
        System.out.println("dignifiedTest被调用,情况对头了!");

        return 200;
    }

    @Recover
    public int recover(Exception e){
    
    
        System.out.println("回调方法执行!!!!");
        //记日志到数据库 或者调用其余的方法
        return 400;
    }

}

@Retryable:注解方式标记当前方法会使用重试机制
里面的 value:重试的触发机制,当遇到Exception异常的时候,触发;
maxAttempts:重试的次数(包括第一次调用,也就是说如果设置3次,调用一次后,如果一直失败触发重试,那么还当前方法还会调用2次);
delay:重试的延迟时间,也就是距离上一次重试方法调用的间隔,单位毫秒
multiplier:delay间隔时间的倍数,也就是说,第一次重试间隔如果是2000ms,那第二次重试的时候就是2000ms 乘以这个倍数1.5,就是3000ms;
maxDelay:重试次数之间的最大时间间隔,默认为0,即忽略,如果小于delay的设置,则默认为30000L
@Recover,也就是用注解方式标记当期方法为回调方法,可以看到传参里面写的是 Exception e,这个是作为回调的接头暗号(重试次数用完了,还是失败,我们抛出这个Exception e通知触发这个回调方法)
PS:该回调方法与重试方法写在同一个实现类里面。

猜你喜欢

转载自blog.csdn.net/qq_19891197/article/details/130845656