Resilience4j使用
介绍
Resilience4j是一款轻量级、易用、容错框架,专为 Java 8 和函数式编程而设计。
容错是指系统在部分组件(一个或多个)发生故障时仍能正常运作的能力。要具有这个能力,通常要包含断路器(CircuitBreaker)、并发调用隔离(Bulkhead)、限流(RateLimiter)、重试(Retry)、超时(Timeout)机制。
超时(Timeout)机制
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | @Bulkhead (name = BACKEND_A, type = Bulkhead.Type.THREADPOOL) @TimeLimiter (name = BACKEND_A) @CircuitBreaker (name = BACKEND_A, fallbackMethod = "futureFallback" ) public CompletableFuture<String> fluxTimeout2() { Random random = new Random(); int sleepTime = random.nextInt( 5000 ); try { Thread.sleep(sleepTime); } catch (InterruptedException ie) { throw new RuntimeException(ie); } return CompletableFuture.completedFuture( "Hello World from backend A" ); } private CompletableFuture<String> futureFallback(TimeoutException ex) { return CompletableFuture.completedFuture( "Recovered specific TimeoutException: " + ex.toString()); } |
设置超时时间3s,application.properties
1 2 3 | resilience4j.timelimiter.configs. default .cancel-running-future= false resilience4j.timelimiter.configs. default .timeout-duration=3s resilience4j.timelimiter.instances.backendA.base-config= default |
发起请求
http://127.0.0.1:8072/futureTimeout
1 2 3 4 5 6 7 8 | @GetMapping ( "futureTimeout" ) public String futureTimeout() throws ExecutionException, InterruptedException { long start = System.currentTimeMillis(); String flux = businessAService.fluxTimeout2().get(); long endTime = System.currentTimeMillis(); log.info( "-------futureTimeout, " +(endTime - start)+ "ms" ); return flux+ " " +(endTime - start)+ "ms" ; } |
1 2 3 | -------futureTimeout, 682ms -------futureTimeout, 3010ms //超时 -------futureTimeout, 1138ms |
重试(Retry)
1 2 3 4 5 6 7 8 9 10 11 12 13 | @GetMapping ( "/getInvoice" ) @Retry (name = "getInvoiceRetry" , fallbackMethod = "getInvoiceFallback" ) public String getInvoice() { log.info( "getInvoice() call starts here" ); ResponseEntity<String> entity= restTemplate.getForEntity( "http://127.0.0.1:8071/world" , String. class ); log.info( "Response :" + entity.getStatusCode()); return entity.getBody(); } public String getInvoiceFallback(Exception e) { log.info( "---RESPONSE FROM FALLBACK METHOD---" ); return "SERVICE IS DOWN, PLEASE TRY AFTER SOMETIME !!!" ; } |
发起请求
http://127.0.0.1:8072/getInvoice
1 2 3 4 | [ 2021 - 12 - 03 16 : 40 : 45 , 823 ] [http-nio- 8072 -exec- 3 ] [INFO] - c.f.s.c.RetryController 36 getInvoice() call starts here [ 2021 - 12 - 03 16 : 40 : 46 , 360 ] [http-nio- 8072 -exec- 3 ] [INFO] - c.f.s.c.RetryController 36 getInvoice() call starts here [ 2021 - 12 - 03 16 : 40 : 46 , 872 ] [http-nio- 8072 -exec- 3 ] [INFO] - c.f.s.c.RetryController 36 getInvoice() call starts here //重试3次 [ 2021 - 12 - 03 16 : 40 : 46 , 886 ] [http-nio- 8072 -exec- 3 ] [INFO] - c.f.s.c.RetryController 43 ---RESPONSE FROM FALLBACK METHOD--- |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗