CompletableFuture 与 Openfeign 一起使用的问题
- 问题描述
- 问题解决
问题描述
- 直接错误信息
Could not find class [org.springframework.boot.autoconfigure.condition.OnPropertyCondition]
java.util.concurrent.CompletionException: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration c lass [org.springframework.cloud.loadbalancer.annotation.LoadBalancerClientConfiguration]; nested exception is java.lang.IllegalArgumentException: Could not find class [org.springframework.boot.autoconfigure.condition.OnPropertyCondition]
at java.base/java.util.concurrent.CompletableFuture.encodeThrowable(CompletableFuture.java:314) ~[na:na]
at java.base/java.util.concurrent.CompletableFuture.completeThrowable(CompletableFuture.java:319) ~[na:na]
at java.base/java.util.concurrent.CompletableFuture A s y n c R u n . r u n ( C o m p l e t a b l e F u t u r e . j a v a : 1739 ) [ n a : n a ] a t j a v a . b a s e / j a v a . u t i l . c o n c u r r e n t . C o m p l e t a b l e F u t u r e AsyncRun.run(CompletableFuture.java:1739) ~[na:na] at java.base/java.util.concurrent.CompletableFuture AsyncRun.run(CompletableFuture.java:1739) [na:na]atjava.base/java.util.concurrent.CompletableFutureAsyncRun.exec(CompletableFuture.java:1728) ~[na:na]
at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:290) ~[na:na]
at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1020) ~[na:na]
at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1656) ~[na:na]
at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1594) ~[na:na]
at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:183) ~[na:na]
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [org.springframework.cloud .loadbalancer.annotation.LoadBalancerClientConfiguration]; nested exception is java.lang.IllegalArgumentException: Could not find class [ org.springframework.boot.autoconfigure.condition.OnPropertyCondition]
- 直接上伪代码
CompletableFuture.runAsync(() -> {// feign 调用 });
问题解决
这个问题其实由来已久,Spring Cloud 官方对其已有说明。
官方认为此bug优先级并不高,官方不会花精力解决;但是对社区是开放的,社区开发者可以提PR。
目前有开发者提出了一些折衷的解决方案。
- 由于此问题与 ForkJoin 线程池底层实现有关,可以为 CompletableFuture 指定自定义线程池
CompletableFuture.runAsync(() -> {// feign 调用}, customThreadPool);