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

springcloud-config客户端启用服务发现报错找不到bean EurekaHttpClient

背景

在对已有项目进行改造的时候,集成SpringConfigStarter,编写完bootstrap.yml,在idea 启动项中编辑并新增VM options -Dspring.cloud.config.discovery.enabled=true,该版本不加spring不会从configService获取信息,官方建议引入一个starter,但是在我的项目中并没有用处,点击运行,报错信息如下

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.netflix.discovery.shared.transport.EurekaHttpClient' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1717) ~[spring-beans-5.2.15.RELEASE.jar:5.2.15.RELEASE]at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1273) ~[spring-beans-5.2.15.RELEASE.jar:5.2.15.RELEASE]at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1227) ~[spring-beans-5.2.15.RELEASE.jar:5.2.15.RELEASE]at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:886) ~[spring-beans-5.2.15.RELEASE.jar:5.2.15.RELEASE]at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:790) ~[spring-beans-5.2.15.RELEASE.jar:5.2.15.RELEASE]... 30 common frames omitted

根据堆栈信息缺失EurekaHttpClient,很容易联想到是不是依赖问题,确认了pom.xml之后发现确实引入了

       <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-bus-amqp</artifactId></dependency>

环境如下
SpringBoot 版本2.3.12.Release
SpringCloud 版本 Hoxton.SR12
JDK 11

再次运行依然报错,然后根据报错堆栈找到,创建的beans
org.springframework.cloud.netflix.eureka.config.EurekaConfigServerBootstrapConfiguration#eurekaConfigServerInstanceProvider该bean的第一个参数就是EurekaHttpClient,也就是创建该bean的时候EurekaHttpClient并没有完成初始化,同时注意到该Configuration的是有初始化EurekaHttpClient的逻辑的

@Bean@ConditionalOnMissingBean(value = {EurekaClientConfig.class},search = SearchStrategy.CURRENT)public EurekaClientConfigBean eurekaClientConfigBean() {return new EurekaClientConfigBean();}@Bean@ConditionalOnMissingBean({EurekaHttpClient.class})@ConditionalOnProperty(prefix = "eureka.client",name = {"webclient.enabled"},matchIfMissing = true,havingValue = "false")public RestTemplateEurekaHttpClient configDiscoveryRestTemplateEurekaHttpClient(EurekaClientConfigBean config, Environment env) {return (RestTemplateEurekaHttpClient)(new RestTemplateTransportClientFactory()).newClient(new DefaultEndpoint(getEurekaUrl(config, env)));}...@Beanpublic ConfigServerInstanceProvider.Function eurekaConfigServerInstanceProvider(EurekaHttpClient client, EurekaClientConfig config) {return (serviceId) -> {if (log.isDebugEnabled()) {log.debug("eurekaConfigServerInstanceProvider finding instances for " + serviceId);}EurekaHttpResponse<Applications> response = client.getApplications(new String[]{config.getRegion()});List<ServiceInstance> instances = new ArrayList();if (this.isSuccessful(response) && response.getEntity() != null) {Applications applications = (Applications)response.getEntity();applications.shuffleInstances(config.shouldFilterOnlyUpInstances());List<InstanceInfo> infos = applications.getInstancesByVirtualHostName(serviceId);Iterator var8 = infos.iterator();while(var8.hasNext()) {InstanceInfo info = (InstanceInfo)var8.next();instances.add(new EurekaServiceInstance(info));}if (log.isDebugEnabled()) {log.debug("eurekaConfigServerInstanceProvider found " + infos.size() + " instance(s) for " + serviceId + ", " + instances);}return instances;} else {return instances;}};}

可以看到bean RestTemplateEurekaHttpClient 通过org.springframework.cloud.netflix.eureka.config.EurekaConfigServerBootstrapConfiguration#configDiscoveryRestTemplateEurekaHttpClient 方法进行初始化,但是该方法执行有两个条件,即eureka.client.webclient.enabled=false@ConditionalOnMissingBean({EurekaHttpClient.class})当前环境不存在EurekaHttpClient,可以确定的是我在bootstrap.yml 配置了 eureka.client.webclient.enabled=false 并检查了好几次,根据Spring容器的初始化规则按照同Configuration类 bean从上往下一次初始化的原则,容器并没有执行 RestTemplateEurekaHttpClient的初始化操作。E:/.m2/repository/org/springframework/cloud/spring-cloud-netflix-eureka-client/2.2.9.RELEASE/spring-cloud-netflix-eureka-client-2.2.9.RELEASE.jar!/org/springframework/cloud/netflix/eureka/config/EurekaConfigServerBootstrapConfiguration.class:66
在代码的这里断点端上,然后找一个可以拿到ApplicationContext的地方执行一下表达式
environment.getProperty("eureka.client.webclient.enabled") result为 true,为了避免EurekaHttpClient已存在导致的不执行,这里再验证一下是否存在这个bean

在这里插入图片描述
从结果来看,引起RestTemplateEurekaHttpClient初始化不执行的原因就是 eureka.client.webclient.enabled=false,然后再次返回检查了所有配置文件终于再bootstrap
.yml中的dev profile部分看到配置依然显示为true

spring:profiles:include:- defaultactive: dev
---
spring:profiles: defaultcloud:config:enabled: truediscovery:enabled: trueservice-id: YOUR_CONFIG-SERVICEprofile: ${spring.profiles.active}label: masterapplication:name: yourAppName
logging:file:path: ${spring.application.name}/logs/level:root: debug
eureka:instance:prefer-ip-address: true
---
spring:profiles: devsecurity:user:name: userpassword: 123456
eureka:client:webclient:enabled: truefetch-registry: trueservice-url:defaultZone: http://your_eureka_host/eurekainstance:metadata-map:user:name: ${spring.security.user.name}password: ${spring.security.user.password}

原因

因为本地bootstrap.yml 通过profiles 来区分不同的运行环境,导致我修改的一直修改的pro环境的配置,笑死。往往神奇的问题,都因为一些简单的原因导致的。记录排查过程,排解下郁闷☹

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 【打工日常】使用Prometheus+Grafana+Alertmanager+Webhook-dingtalk搭建监控平台
  • Air780EP模块 LuatOS开发-MQTT接入阿里云应用指南
  • 深入解析DDoS攻击:原理、危害与防御策略
  • 【python】sklearn基础教程及示例
  • 二叉树详解-第一篇 树以及二叉树的概念
  • Golang | Leetcode Golang题解之第273题整数转换英文表示
  • mongoose之http调试代码
  • 星环科技推出知识库产品 AI PC时代数据交互方式变革
  • 【开发实战】QT5 + OpenCV4 开发环境配置应用演示
  • js-vue中多个按钮状态选中类似于复选框与单选框实现
  • 硅纪元视角 | 语音克隆突破:微软VALL-E 2,Deepfake新纪元!
  • 夏老师小课堂(15)丨空心杯电机基础入门(上海鸣志电器)
  • 【Go系列】Go的UI框架GIO
  • SpringBoot集成Tomcat、DispatcherServlet
  • 【监控软件】Zabbix
  • 分享的文章《人生如棋》
  • 230. Kth Smallest Element in a BST
  • axios请求、和返回数据拦截,统一请求报错提示_012
  • javascript 总结(常用工具类的封装)
  • javascript从右向左截取指定位数字符的3种方法
  • k8s 面向应用开发者的基础命令
  • Linux链接文件
  • MaxCompute访问TableStore(OTS) 数据
  • Spring核心 Bean的高级装配
  • 从 Android Sample ApiDemos 中学习 android.animation API 的用法
  • 翻译:Hystrix - How To Use
  • 前端学习笔记之原型——一张图说明`prototype`和`__proto__`的区别
  • 说说动画卡顿的解决方案
  • 我从编程教室毕业
  • Python 之网络式编程
  • 阿里云重庆大学大数据训练营落地分享
  • ​​​​​​​​​​​​​​Γ函数
  • (2)空速传感器
  • (iPhone/iPad开发)在UIWebView中自定义菜单栏
  • (k8s)Kubernetes本地存储接入
  • (每日持续更新)jdk api之FileReader基础、应用、实战
  • (十)DDRC架构组成、效率Efficiency及功能实现
  • (一)Spring Cloud 直击微服务作用、架构应用、hystrix降级
  • (一一四)第九章编程练习
  • (最全解法)输入一个整数,输出该数二进制表示中1的个数。
  • ***检测工具之RKHunter AIDE
  • *Django中的Ajax 纯js的书写样式1
  • ./indexer: error while loading shared libraries: libmysqlclient.so.18: cannot open shared object fil
  • .h头文件 .lib动态链接库文件 .dll 动态链接库
  • .NET 5种线程安全集合
  • .NET Conf 2023 回顾 – 庆祝社区、创新和 .NET 8 的发布
  • .net core 微服务_.NET Core 3.0中用 Code-First 方式创建 gRPC 服务与客户端
  • .NET Framework杂记
  • .net 连接达梦数据库开发环境部署
  • .Net6使用WebSocket与前端进行通信
  • .Net的C#语言取月份数值对应的MonthName值
  • @ 代码随想录算法训练营第8周(C语言)|Day57(动态规划)
  • @EnableAsync和@Async开始异步任务支持
  • @基于大模型的旅游路线推荐方案
  • [.NET]桃源网络硬盘 v7.4