服務提供者【test-provider8001】
Openfeign遠程調用服務提供者搭建
文章地址http://t.csdnimg.cn/06iz8
相關接口
測試遠程調用:http://localhost:8001/payment/index
服務消費者【test-consumer-resilience4j8004】
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:# 重試機制,此處backendA是自己定義的名稱,對應@Retry的nameretry:instances:# 實例名稱:自己定義的名稱,對應@TimeLimiter中的namebackendA:# 最大重試次數maxRetryAttempts: 3# 固定的重試間隔,10中之內重試三次waitDuration: 10s# 表示是否開啟指數退避抖動算法enableExponentialBackoff: true# 表示時間間隔乘數exponentialBackoffMultiplier: 2
OrderController【控制層】
/*** 測試重試機制** @return*/@GetMapping("/retry")@Retry(name = "backendA")//重試機制注解【會按照配置重試此接口】:name:對應application.yml的重試機制配置名稱public CompletableFuture<String> retry() {log.info("********* 進入方法 ******");//resilience4j一般用異步操作,此處使用lambda表達式CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync((Supplier<String>) () -> (paymentFeignService.paymentIndex()));log.info("********* 離開方法 ******");return completableFuture;}
相關接口
測試重試機制:
http://localhost:8004/order/retry