請(qǐng)求重試
創(chuàng)建springboot項(xiàng)目,選擇web、eureka-client依賴,創(chuàng)建完成后添加resilience4j依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-spring-boot2</artifactId>
<version>1.2.0</version>
<exclusions>
<exclusion>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-circuitbreaker</artifactId>
</exclusion>
<exclusion>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-ratelimiter</artifactId>
</exclusion>
<exclusion>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-bulkhead</artifactId>
</exclusion>
<exclusion>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-timelimiter</artifactId>
</exclusion>
</exclusions>
</dependency>
注意:resilience4j-spring-boot2中包含了resilience4j中所有的功能,未配置的功能無(wú)法正常使用,所以要將其他組件先從依賴中排除。
新建yml文件,配置retry,并將該服務(wù)注冊(cè)到erueka
resilience4j:
retry:
retry-aspect-order: 399 # 表示retry優(yōu)先級(jí),一般retry優(yōu)先級(jí)高于限流和斷路器
backends:
retryA:
maxRetryAttempts: 5 # 重試次數(shù)
waitDuration: 500 # 重試等待時(shí)間
exponentialBackoffMultipliter: 1.1 # 間隔乘數(shù)
retryException:
- java.lang.RuntimeException
spring:
application:
name: resilience4j
server:
port: 5000
eureka:
client:
service-url:
defaultZone: http://localhost:1111/eureka
新建Service,調(diào)用provider中的接口,聲明使用重試策略的名稱
@Service
@Retry(name = "retryA")
public class HelloService {
@Autowired
RestTemplate restTemplate;
public String hello(){
return restTemplate.getForObject("http://localhost:1113/hello",String.class);
}
}
最后編寫(xiě)測(cè)試接口,進(jìn)行測(cè)試,可以看到方法共執(zhí)行了五次。
circuitbreaker
現(xiàn)在依賴中刪除對(duì)circuitbreaker組件的排除
<dependency>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-spring-boot2</artifactId>
<version>1.2.0</version>
<exclusions>
<exclusion>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-ratelimiter</artifactId>
</exclusion>
<exclusion>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-bulkhead</artifactId>
</exclusion>
<exclusion>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-timelimiter</artifactId>
</exclusion>
</exclusions>
</dependency>
在yml中配置circuitbreaker實(shí)例
circuitbreaker:
instances:
cbA:
ringBufferSizeInClosedState: 5
ringBufferSizeInHalfOpenState: 3
waitInterval: 5000
recordExceptions:
- org.springframework.web.client.HttpServerErrorException
在service中添加服務(wù)降級(jí)的方法,并在@CircuitBreaker注解中聲明斷路器的策略和服務(wù)降級(jí)返回的方法。
@Service
@CircuitBreaker(name = "cbA",fallbackMethod = "error")
public class HelloService {
@Autowired
RestTemplate restTemplate;
public String hello(){
return restTemplate.getForObject("http://localhost:1113/hello",String.class);
}
public String error(Throwable t){
return "error";
}
}
訪問(wèn)測(cè)試接口可以看到頁(yè)面返回到error,服務(wù)降級(jí)成功。
RateLimiter
RateLimiter作為一個(gè)限流工具,主要用于服務(wù)端,用來(lái)保護(hù)接口
在provider中添加resilience4j依賴
<dependency>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-spring-boot2</artifactId>
<version>1.2.0</version>
<exclusions>
<exclusion>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-circuitbreaker</artifactId>
</exclusion>
<exclusion>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-bulkhead</artifactId>
</exclusion>
<exclusion>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-timelimiter</artifactId>
</exclusion>
</exclusions>
</dependency>
在application.properties中配置RateLimiter
resilience4j.ratelimiter.limiters..rlA.limit-for-period=1
resilience4j.ratelimiter.limiters.rlA.limit-refresh-period=1s
resilience4j.ratelimiter.limiters..rlA.timeout-duration=1s
為了查看效果,在provider的接口中打印每個(gè)請(qǐng)求處理時(shí)間。通過(guò)@RateLimiter(name = "rlA")標(biāo)記該接口限流,限流策略為rlA
@Override
@RateLimiter(name = "rlA")
public String hello(){
String s = "hello cloud" + port;
System.out.println(new Date());
return s;
}
在resilience4j模塊的接口中多次發(fā)送請(qǐng)求進(jìn)行測(cè)試
public String hello(){
for (int i = 0; i < 3; i++) {
restTemplate.getForObject("http://localhost:1113/hello",String.class);
}
return "success";
}
可以看到限流生效
。