当前位置: 首页 > news >正文

踩坑Resilience4j @Bulkhead二

调整了@Bulkhead j加了type = Type.THREADPOOL,发现程序无法进入这个方法

    @CircuitBreaker(name = "licenseService", fallbackMethod = "buildFallbackLicenseList")@Bulkhead(name = "bulkheadLicenseService", fallbackMethod = "buildFallbackLicenseList", type = Type.THREADPOOL)@Retry(name = "retryLicenseService", fallbackMethod = "buildFallbackLicenseList")@RateLimiter(name = "licenseService", fallbackMethod = "buildFallbackLicenseList")public List<License> getLicensesByOrganization(String organizationId) {LOGGER.info("find all licenses belong to {}", organizationId);LicenseExample example = new LicenseExample();example.createCriteria().andOrganizationIdEqualTo(organizationId);return licenseMapper.selectByExample(example);}

debug发现io.github.resilience4j.spring6.bulkhead.configure.BulkheadAspect类里面对@Bulkheadtype属性进行了判断

    @Around(value = "matchAnnotatedClassOrMethod(bulkheadAnnotation)", argNames = "proceedingJoinPoint, bulkheadAnnotation")public Object bulkheadAroundAdvice(ProceedingJoinPoint proceedingJoinPoint,@Nullable Bulkhead bulkheadAnnotation) throws Throwable {Method method = ((MethodSignature) proceedingJoinPoint.getSignature()).getMethod();String methodName = method.getDeclaringClass().getName() + "#" + method.getName();if (bulkheadAnnotation == null) {bulkheadAnnotation = getBulkheadAnnotation(proceedingJoinPoint);}if (bulkheadAnnotation == null) { //because annotations wasn't foundreturn proceedingJoinPoint.proceed();}Class<?> returnType = method.getReturnType();String backend = spelResolver.resolve(method, proceedingJoinPoint.getArgs(), bulkheadAnnotation.name());if (bulkheadAnnotation.type() == Bulkhead.Type.THREADPOOL) {final CheckedSupplier<Object> bulkheadExecution =() -> proceedInThreadPoolBulkhead(proceedingJoinPoint, methodName, returnType, backend);return fallbackExecutor.execute(proceedingJoinPoint, method, bulkheadAnnotation.fallbackMethod(), bulkheadExecution);} else {io.github.resilience4j.bulkhead.Bulkhead bulkhead = getOrCreateBulkhead(methodName,backend);final CheckedSupplier<Object> bulkheadExecution = () -> proceed(proceedingJoinPoint, methodName, bulkhead, returnType);return fallbackExecutor.execute(proceedingJoinPoint, method, bulkheadAnnotation.fallbackMethod(), bulkheadExecution);}}

bulkheadAroundAdvice方法中使用if-else对type进行不同处理,type=Type.THREADPOOL时调用proceedInThreadPoolBulkhead方法

    private Object proceedInThreadPoolBulkhead(ProceedingJoinPoint proceedingJoinPoint,String methodName, Class<?> returnType, String backend) throws Throwable {if (logger.isDebugEnabled()) {logger.debug("ThreadPool bulkhead invocation for method {} in backend {}", methodName,backend);}ThreadPoolBulkhead threadPoolBulkhead = threadPoolBulkheadRegistry.bulkhead(backend);if (CompletionStage.class.isAssignableFrom(returnType)) {// threadPoolBulkhead.executeSupplier throws a BulkheadFullException, if the Bulkhead is full.// The RuntimeException is converted into an exceptionally completed futuretry {return threadPoolBulkhead.executeCallable(() -> {try {return ((CompletionStage<?>) proceedingJoinPoint.proceed()).toCompletableFuture().get();} catch (ExecutionException e) {throw new CompletionException(e.getCause());} catch (InterruptedException | CancellationException e) {throw e;} catch (Throwable e) {throw new CompletionException(e);}});} catch (BulkheadFullException ex){CompletableFuture<?> future = new CompletableFuture<>();future.completeExceptionally(ex);return future;}} else {throw new IllegalStateException("ThreadPool bulkhead is only applicable for completable futures ");}}

proceedInThreadPoolBulkhead方法判断返回值类型, 如果是CompletionStage及子类(比如CompletableFuture)就能处理,否则抛IllegalStateException
因为我们的返回值类型是List<License>,所以会进入else中抛出IllegalStateException


http://www.mrgr.cn/news/40662.html

相关文章:

  • C++继承实例讲解
  • 判断奇数id%2=1or id%2!=0
  • P3227 [HNOI2013] 切糕
  • 选择与运用合适工具提升编程效率的秘诀
  • 自闭症康复摘帽攻略:让孩子获得新生
  • 【css】如何设计出具有权威性的“机构”网页
  • Java面试——操作系统篇
  • MySQL基础篇 - 多表查询
  • 美本申请怎么填写课外活动?这些细节值得注意
  • 【AI知识点】点积相似性(dot-product similarity)
  • 数据库查询
  • 【Spine】引入PhotoshopToSpine脚本
  • Flowable之任务撤回(支持主流程、子流程相互撤回)
  • CMIS5.2_光模块切应用(Application Selection and Instantiation)
  • Elasticsearch:使用 LLM 实现传统搜索自动化
  • 位运算(5)_两数之和
  • 数据分析-30-电影死亡笔记中的数据分析思维
  • 【重学 MySQL】四十四、相关子查询
  • 【Java基础】Java面试基础知识QA(上)
  • 我的创作128天纪念日或者说自写博客以来的一些感悟