An annotation of SpringBoot, an elegant implementation of the retry mechanism retry

1: POM configuration

<?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 configuration

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

3: Start class

@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: Annotation method to mark that the current method will use the retry mechanism.
Value inside: The retry trigger mechanism, which is triggered when an Exception is encountered;
maxAttempts: The number of retries (including the first call, that is, if Set 3 times, after calling once, if it keeps failing to trigger retry, then the current method will be called twice);
delay: the delay time of retry, that is, the interval from the last retry method call, in milliseconds
multiplier: The multiple of the delay interval, that is, if the first retry interval is 2000ms, then the second retry is 2000ms multiplied by this multiple of 1.5, which is 3000ms; maxDelay: the maximum time interval between
retries , the default is 0, that is, ignored, if it is less than the delay setting, the default is 30000L
@Recover, that is, mark the current method as a callback method with annotations, you can see that Exception e is written in the passed parameter, which is used as a callback Connector code (if the number of retries is exhausted and still fails, we throw this Exception e notification to trigger this callback method)
PS: This callback method and the retry method are written in the same implementation class.

Guess you like

Origin blog.csdn.net/qq_19891197/article/details/130845656