为什么80%的码农都做不了架构师?>>>   

本文主要研究下resilience4j的基本功能

maven

        <dependency><groupId>io.github.resilience4j</groupId><artifactId>resilience4j-circuitbreaker</artifactId><version>0.13.0</version></dependency><dependency><groupId>io.github.resilience4j</groupId><artifactId>resilience4j-ratelimiter</artifactId><version>0.13.0</version></dependency><dependency><groupId>io.github.resilience4j</groupId><artifactId>resilience4j-retry</artifactId><version>0.13.0</version></dependency><dependency><groupId>io.github.resilience4j</groupId><artifactId>resilience4j-bulkhead</artifactId><version>0.13.0</version></dependency><dependency><groupId>io.github.resilience4j</groupId><artifactId>resilience4j-circularbuffer</artifactId><version>0.13.0</version></dependency><dependency><groupId>io.github.resilience4j</groupId><artifactId>resilience4j-timelimiter</artifactId><version>0.13.0</version></dependency>

CircuitBreaker

    @Testpublic void testCircuitBreaker(){// Create a CircuitBreaker (use default configuration)CircuitBreakerConfig circuitBreakerConfig = CircuitBreakerConfig.custom().enableAutomaticTransitionFromOpenToHalfOpen().build();CircuitBreaker circuitBreaker = CircuitBreaker.of("backendName",circuitBreakerConfig);String result = circuitBreaker.executeSupplier(() -> backendService.doSomethingWithArgs("world"));System.out.println(result);}

CircuitBreaker主要是实现针对接口异常的断路统计以及断路处理

Timelimiter

    @Testpublic void testTimelimiter(){ExecutorService executorService = Executors.newSingleThreadExecutor();TimeLimiterConfig config = TimeLimiterConfig.custom().timeoutDuration(Duration.ofMillis(600)).cancelRunningFuture(true).build();TimeLimiter timeLimiter = TimeLimiter.of(config);Supplier<Future<String>> futureSupplier = () -> {return executorService.submit(backendService::doSomethingThrowException);};Callable<String> restrictedCall = TimeLimiter.decorateFutureSupplier(timeLimiter,futureSupplier);Try.of(restrictedCall::call).onFailure(throwable -> System.out.println("We might have timed out or the circuit breaker has opened."));}

主要是实现超时的控制

Bulkhead

    /*** A Bulkhead can be used to limit the amount of parallel executions*/@Testpublic void testBulkhead(){Bulkhead bulkhead = Bulkhead.of("test", BulkheadConfig.custom().maxConcurrentCalls(1).build());Supplier<String> decoratedSupplier = Bulkhead.decorateSupplier(bulkhead, backendService::doSomethingSlowly);IntStream.rangeClosed(1,2).parallel().forEach(i -> {String result = Try.ofSupplier(decoratedSupplier).recover(throwable -> "Hello from Recovery").get();System.out.println(result);});}

Bulkhead目前来看是用来控制并行(parallel)调用的次数

RateLimiter

    @Testpublic void testRateLimiter(){// Create a custom RateLimiter configurationRateLimiterConfig config = RateLimiterConfig.custom().timeoutDuration(Duration.ofMillis(100)).limitRefreshPeriod(Duration.ofSeconds(1)).limitForPeriod(1).build();// Create a RateLimiterRateLimiter rateLimiter = RateLimiter.of("backendName", config);// Decorate your call to BackendService.doSomething()Supplier<String> restrictedSupplier = RateLimiter.decorateSupplier(rateLimiter, backendService::doSomething);IntStream.rangeClosed(1,5).parallel().forEach(i -> {Try<String> aTry = Try.ofSupplier(restrictedSupplier);System.out.println(aTry.isSuccess());});}

用来做流控

Fallback

    @Testpublic void testFallback(){// Execute the decorated supplier and recover from any exceptionString result = Try.ofSupplier(() -> backendService.doSomethingThrowException()).recover(throwable -> "Hello from Recovery").get();System.out.println(result);}@Testpublic void testCircuitBreakerAndFallback(){CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("backendName");Supplier<String> decoratedSupplier = CircuitBreaker.decorateSupplier(circuitBreaker, backendService::doSomethingThrowException);String result = Try.ofSupplier(decoratedSupplier).recover(throwable -> "Hello from Recovery").get();System.out.println(result);}

fallback基本上是高可用操作的标配

Retry

    @Testpublic void testRetry(){CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("backendName");// Create a Retry with at most 3 retries and a fixed time interval between retries of 500msRetry retry = Retry.ofDefaults("backendName");// Decorate your call to BackendService.doSomething() with a CircuitBreakerSupplier<String> decoratedSupplier = CircuitBreaker.decorateSupplier(circuitBreaker, backendService::doSomething);// Decorate your call with automatic retrydecoratedSupplier = Retry.decorateSupplier(retry, decoratedSupplier);// Execute the decorated supplier and recover from any exceptionString result = Try.ofSupplier(decoratedSupplier).recover(throwable -> "Hello from Recovery").get();System.out.println(result);}

retry用来控制重试

小结

resilience4j是一款受hystrix启发的容错组件,提供了如下几款核心组件:

  • resilience4j-circuitbreaker: Circuit breaking
  • resilience4j-ratelimiter: Rate limiting
  • resilience4j-bulkhead: Bulkheading
  • resilience4j-retry: Automatic retrying (sync and async)
  • resilience4j-cache: Response caching 这里主要演示了关于circuitbreaker、ratelimiter、bulkhead、retry以及timelimiter。其特色就是使用装饰者模式,可以多个功能组合在一起。其他的话,timelimiter的使用比起hystrix稍微费劲些。

doc

  • Fault tolerance library designed for functional programming

转载于:https://my.oschina.net/go4it/blog/1843081

resilience4j小试牛刀相关推荐

  1. Node.js开发WEB项目后端接口API,基于mysql5.7数据库(小试牛刀)

    项目结构 main.js(入口文件,开启9999端口监听,实现RESTful风格接口访问) const express = require("express"); const ap ...

  2. mongoose小试牛刀

    参考: Mongoose Networking Library Documentation 正文: 先展示一下小试牛刀的结果吧- 一. 首先在程序默认指定的端口上开启服务,在后台运行 ./http_c ...

  3. 聊聊resilience4j的CircuitBreakerStateMachine

    序 本文主要研究一下resilience4j的CircuitBreakerStateMachine CircuitBreakerStateMachine resilience4j-circuitbre ...

  4. Sentinel 与 Hystrix、resilience4j 的对比

    https://github.com/alibaba/Sentinel/wiki/Guideline:-%E4%BB%8E-Hystrix-%E8%BF%81%E7%A7%BB%E5%88%B0-Se ...

  5. CSS学习18之小试牛刀

    小试牛刀 到现在为止,我们已经将css要学习的初级中级高级知识都已经学完了,接下来我们就应该对自己做一个小测验了,以下就是我的小测验,模拟qq会员官网的头部栏! 素材 bg.png背景图 logo.p ...

  6. Android开发之Retrofit小试牛刀

    感觉好久没有写Android的文章了,囧囧囧!因为Retrofit实在是太火了, 火得我一直跃跃欲试,但是由于种种原因吧,一直都没有用过.周末闲来无事,利用一个以前开发中用过的服务器API来小试牛刀一 ...

  7. MongoDB 小试牛刀

    MongoDb是不错的NoSQL数据库之一,NoSQL 的意思是Not Only SQL,比关系型数据库MySQL.SQL Server等更易于扩展,更多的应用于海量数据的存储,且数据可以弹性跨LAN ...

  8. 深度学习和目标检测系列教程 2-300:小试牛刀,使用 ImageAI 进行对象检测

    @Author:Runsen 对象检测是一种属于更广泛的计算机视觉领域的技术.它处理识别和跟踪图像和视频中存在的对象.目标检测有多种应用,如人脸检测.车辆检测.行人计数.自动驾驶汽车.安全系统等.Im ...

  9. C#位图BitArray 小试牛刀

    前面聊了布隆过滤器,回归认识一下位图BitMap,阅读前文的同学应该发现了布隆过滤器本身就是基于位图,是位图的一种改进. 难缠的布隆过滤器,这次终于通透了 位图 先看一个问题, 假如有1千万个整数,整 ...

最新文章

  1. 第163天:js面向对象-对象创建方式总结
  2. 从FASTQ到BAM经历了什么?
  3. TIOBE 9月编程语言排行榜
  4. Django 3.2.5博客开发教程:基础配置
  5. 【学习笔记】同余最短路
  6. 图的四种最短路径算法
  7. 海量数据库解决方案2011030101
  8. 打破多项世界记录  双11背后最大的力量是技术
  9. 互联网晚报 | 9月30日 星期四 | 五菱与B站跨界合作;支付宝向“云闪付”开放支付场景;平安健康险上线“药划算”...
  10. linux下用mail发送邮件
  11. java中的BigInteger
  12. 【图文详解】用Eclipse创建Maven Web项目
  13. 从日志中截取某个时间段的日志分析
  14. HDFS Archival Storage
  15. Qt: json对象转格式化字符串
  16. javase学习——面向对象(下)
  17. 独家对话阿里副总裁李飞飞:数据库的进化之路
  18. gis热力专题图制作
  19. 02年六代雅阁的整备质量_雅阁对不起,我不能爱你。
  20. JAVA计算机毕业设计房屋租赁管理系统Mybatis+系统+数据库+调试部署

热门文章

  1. 首届“AIIA杯”人工智能巡回赛起航,5大赛区邀你来战
  2. SpringBoot多线程环境下,解决多个定时器冲突问题
  3. 让人头痛的大事务问题到底要如何解决?
  4. 星巴克不使用两阶段提交
  5. 监控神器Prometheus用不对,也就是把新手村的剑
  6. 面试官:你知道Java中的回调机制吗?
  7. 5 款非常好用的开源 Docker 工具,get一波~
  8. 你真的会写单例模式吗?
  9. 写给新手:2021版调参上分手册!
  10. 一文详尽系列之EM算法