resilience4j github地址 https://github.com/resilience4j/resilience4j
中文文档地址 https://github.com/lmhmhl/Resilience4j-Guides-Chinese
pom.xml引入resilience4j包
<?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">
<parent>
<artifactId>tl-demo</artifactId>
<groupId>com.tl</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.tl</groupId>
<artifactId>resilience4j</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!--springboot-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/io.github.resilience4j/resilience4j-spring-boot2 -->
<dependency>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-spring-boot2</artifactId>
<version>1.7.0</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</resources>
</build>
</project>配置文件application.properties
#===================================限流begin===================================
#每秒允许的最大请求数
resilience4j.ratelimiter.instances.demoRateLimiter.limit-for-period=5
#刷新周期为1秒
resilience4j.ratelimiter.instances.demoRateLimiter.limit-refresh-period=1s
#超时时间为0,表示立即返回结果
resilience4j.ratelimiter.instances.demoRateLimiter.timeout-duration=0
#===================================限流end===================================
#===================================超时begin===================================
#超时时间为2秒
resilience4j.timelimiter.instances.slowTimeLimiter.timeout-duration=1s
#===================================超时end===================================controller测试代码
package com.tl.resilience4j.controller;
import io.github.resilience4j.ratelimiter.annotation.RateLimiter;
import io.github.resilience4j.timelimiter.annotation.TimeLimiter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.concurrent.CompletableFuture;
@RestController
@RequestMapping("/api")
@Slf4j
public class DemoController {
/**
* 限流示例
*
* 测试地址,如果1秒钟内请求次数超过5次将返回fallbackMethod中的结果
* http://localhost:8080/api/hello
*
* @return
*/
@RateLimiter(name = "demoRateLimiter",fallbackMethod = "helloFailBack")
@GetMapping("/hello")
public String sayHello(){
log.info("do sayHello ======= ");
return "hello world";
}
/**
* 超时示例
*
* 测试正常请求 http://localhost:8080/api/slow?time=1
* 测试超时请求 http://localhost:8080/api/slow?time=3
*
* @return
*/
@GetMapping("/slow")
@TimeLimiter(name="slowTimeLimiter",fallbackMethod = "slowFailBack")
public CompletableFuture<String> slowMethod(int time){
log.info("Executing slow endpoint");
return CompletableFuture.supplyAsync(() ->{
try {
//模拟耗时操作
Thread.sleep(1000 * time);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "slowMethod completed";
});
}
public String helloFailBack(Throwable t){
log.error("helloFailBack method called due to rate limiting", t);
return "Too many requests, please try again later";
}
public CompletableFuture slowFailBack(Throwable t){
log.error("slowFailBack method called due to timeout", t);
//return "Operation timed out, please try again later";
return CompletableFuture.supplyAsync(() ->{
return "Operation timed out, please try again later";
});
}
}