服務提供者
Openfeign遠程調用服務提供者搭建
文章地址http://t.csdnimg.cn/06iz8
PaymentController【控制層】
/*** 測試超時機制** @return*/@GetMapping("/timeout")public String TimeOut() {try {TimeUnit.SECONDS.sleep(5);} catch (InterruptedException e) {e.printStackTrace();}return "payment success";}
相關接口
測試超時機制:http://localhost:8001/payment/timeout
服務消費者
Openfeign遠程調用消費者搭建
?文章地址http://t.csdnimg.cn/06iz8
依賴
<!-- resilience4j -->
<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>
application.yml
# 超時機制
resilience4j:timelimiter:instances:delay:# 設置超時時間 2秒timeoutDuration: 2
PaymentFeignService【接口層】
/*** 支付服務-測試超時機制** @return*/@GetMapping("/payment/timeout")String paymentTimeout();
OrderController【控制層】
/*** 支付服務-測試超時降級** @return*/@GetMapping("/timeout")@TimeLimiter(name = "delay", fallbackMethod = "timeoutFallback")//超時降級注解:name:對應application.yml的超時降級配置名稱;fallbackMethod:超時服務降級方法public CompletableFuture<String> paymentTimeout() {log.info("********* 進入方法 ******");//resilience4j一般用異步操作,此處使用lambda表達式CompletableFuture<String> completableFuture =CompletableFuture.supplyAsync((Supplier<String>) () -> (paymentFeignService.paymentTimeout()));log.info("********* 離開方法 ******");return completableFuture;}/*** 超時服務降級方法** @param e* @return*/public CompletableFuture<ResponseEntity> timeoutFallback(Exception e) {e.printStackTrace();return CompletableFuture.completedFuture(ResponseEntity.ok("超時啦"));}
相關接口
測試服務調用:http://localhost:8004/order/index
測試超時機制:http://localhost:8004/order/timeout
?