JAVA全系列 教程
3762个小节阅读:7092.7k
目录
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
xxxxxxxxxx
<dependencies>
<!-- 引入Eureka 客户端依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- 引入服务调用依赖 OpenFigen -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
</dependency>
<!-- actuator监控信息完善 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-spring-cloud2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-circuitbreaker-resilience4j</artifactId>
</dependency>
</dependencies>
xxxxxxxxxx
# 超时机制
resilience4j:
timelimiter:
instances:
delay:
# 设置超时时间 5秒
timeoutDuration: 2
xxxxxxxxxx
/**
* 超时服务
* @return
*/
@GetMapping("timeout")
public String timeout(){
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "success";
}
xxxxxxxxxx
@GetMapping("/timeout")
@TimeLimiter(name = "delay",fallbackMethod = "timeoutfallback")
public CompletableFuture<String> timeout() {
log.info("********* 进入方法 ******");
//异步操作
CompletableFuture<String> completableFuture = CompletableFuture
.supplyAsync((Supplier<String>) () -> (paymentFeignService.timeout()));
log.info("********* 离开方法 ******");
return completableFuture;
}
xxxxxxxxxx
/**
* 超时服务降级方法
* @param e
* @return
*/
public CompletableFuture<ResponseEntity> timeoutfallback(Exception e){
e.printStackTrace();
return CompletableFuture.completedFuture(ResponseEntity.ok("超时啦"));
}