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

解决Spring Cloud Gateway中使用CompletableFuture.supplyAsync()执行Feign调用报错

报错背景描述

组件版本信息:

  • Spring Cloud:2021.0.5
  • Spring Cloud Alibaba:2021.0.5.0
  • Nacos:2.2.3

项目采用基于Spring Cloud Alibaba + Nacos的微服务架构,生产环境部署时服务部署到阿里云ACK容器集群中,并使用阿里云MSE云原生网关作为接入层。
出于成本考虑,在测试环境并未直接采购阿里云ACK集群和MSE云原生网关,所以使用Spring Cloud Gateway作为测试环境的网关服务。生产环境部署时,在MSE云原生网关处对请求接口做了全局鉴权,因此也需要在Spring Cloud Gateway服务做相同的事情,如下所示:
接口请求流程

当网关接收到客户端请求服务A的接口时,先要到鉴权服务校验请求参数是否正确(如:对于需要登录才能访问的接口,必须携带正确的token参数)。如果鉴权失败,在网关处直接将错误信息响应给客户端;只有鉴权成功后,网关再将请求转发给服务A,并将服务A的响应结果返回给客户端。

由于网关和服务A都注册到了同一个Nacos注册中心,因此网关可以通过Feign框架发起基于HTTP协议的RPC调用,添加如下依赖配置:

<!--配置feign-->
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency><!-- 为Feign提供负载均衡 -->
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>

另外,在发起对鉴权服务的HTTP接口调用时,还需要使用“异步转同步”的思路:

// 异步转同步:调用feign服务接口
RpcResult resultVO = this.syncInvoke(CompletableFuture.supplyAsync(() -> authRemoteClient.validateToken(os, token, appVersion)));/*** 将异步调用转换为同步调用* @param future {@link CompletableFuture}* @return 同步返回结果*/
private RpcResult syncInvoke(CompletableFuture<ResponseEntity<String>> future) {if (future == null) {return RpcResult.error();}try {ResponseEntity<String> response = future.get();String body = response.getBody();RpcResult result = JsonUtil.fromJson(body, RpcResult.class);if (result.isError()) {// 如果token校验不通过,直接返回校验失败的信息return result;}// 校验token通过,将用户uid返回HttpHeaders headers = response.getHeaders();String uid = headers.getFirst("uid");RpcResult rpcResult = RpcResult.success();rpcResult.setData(uid);return rpcResult;} catch (Exception e) {logger.info("同步调用执行出错:{}", e.getMessage(), e);throw new RuntimeException(e);}
}

运行时调用鉴权服务会发生如下报错:

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]  

问题解决

经过检索后得知,这是一个已知的BUG,详见:Use classloader from class

解决办法:

将调用方法CompletableFuture.supplyAsync(Supplier<U> supplier)改为调用CompletableFuture.supplyAsync(Supplier<U> supplier,Executor executor),主动传递一个线程池参数即可。

// 异步执行线程池
private Executor asynchronousExecutor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>(EXECUTOR_QUEUE_SIZE),new AbortPolicy());// 异步转同步:调用feign服务接口
CompletableFuture future = CompletableFuture.supplyAsync(() -> authRemoteClient.validateToken(os, token, appVersion), asynchronousExecutor);
RpcResult resultVO = this.syncInvoke(future);

【参考】
Spring Cloud 疑难杂症之 CompletableFuture 与 Openfeign 一起使用的问题

http://www.rkmt.cn/news/70380.html

相关文章:

  • anything
  • 从vw/vh到clamp(),前端响应式设计的痛点与进化 - 实践
  • 10413_基于Springboot的智慧养老院管理系统
  • 【Unity URP】Rendering Debugger和可视化MipMap方案
  • 2025–2030 年最紧缺的八大 IC 岗位
  • Firefox 禁用按下 Alt 显示菜单
  • ReAct+LangGraph:构建智能AI Agent的完整指南(建议收藏) - 详解
  • 第七天项目
  • 2025煤炭氟氯测定仪TOP5权威推荐:精准检测选对品牌,奥
  • [豪の算法奇妙冒险] 代码随想录算法训练营第十四天 | 翻转二叉树、对称二叉树、二叉树的最大深度、二叉树的最小深度
  • JAX 训练加速指南:8 个让 TPU 满跑的工程实战习惯
  • 251202 模拟测 总结
  • 2025年中国温度传感器主流品牌五大推荐:看哪家品牌适合实验
  • 递归算法设计与实现 - Invinc
  • 惊呆了!这个小脚本竟然同时搞定计算、进制转换和BMI计算
  • 深入解析:苹果企业签名流程
  • Scrum 冲刺博客_1
  • Scrum 冲刺博客_2
  • Scrum1 冲刺博客
  • mysqld_multi方式,多启动mysql
  • 2025年聚酰亚胺棒定制生产厂家排名,看哪家技术水平高?
  • 第6篇:Alpha阶段Day6冲刺日志
  • 第7篇:Alpha阶段Day7冲刺日志
  • 2025年口碑好的停经架配件批发厂家推荐,看看哪家价格实惠
  • LangChain 提示模板注入漏洞详解:通过属性访问实现攻击
  • 第2篇:Alpha阶段Day2冲刺日志
  • 第1篇:Alpha阶段Scrum冲刺启动
  • 2025年AI企业培训专业推荐榜:实战导向培训指南
  • 2025年AI企业培训推荐榜:覆盖实战导向的数字化转型,精选优质机构供参考
  • 2025上海素食餐厅必吃榜:五家食材新鲜、口味地道的素食馆推荐