空的springboot项目导入nacos 配置中心和注册中心依赖后报错,提示 org.apache.http.impl.client.HttpClientBuilder 这个类找不到 问题的解决

简介: 空的springboot项目导入nacos 配置中心和注册中心依赖后报错,提示 org.apache.http.impl.client.HttpClientBuilder 这个类找不到 问题的解决

0. 开发环境

SpringBoot 2.3.3.RELEASE

SpringCloud Hoxton.SR10

SpringCloudAlibaba 2.2.3.RELEASE

SpringcloudAlibabaNacos 2.2.2.RELEASE

1. 报错内容

报错过长,只留下了重要的一些报错语句

java.lang.IllegalStateException: Error processing condition on org.springframework.cloud.commons.httpclient.HttpClientConfiguration$ApacheHttpClientConfiguration.apacheHttpClientBuilder
  at org.springframework.boot.autoconfigure.condition.SpringBootCondition.matches(SpringBootCondition.java:60) ~[spring-boot-autoconfigure-2.3.3.RELEASE.jar:2.3.3.RELEASE]
  ......
Caused by: java.lang.IllegalStateException: @ConditionalOnMissingBean did not specify a bean using type, name or annotation and the attempt to deduce the bean's type failed
  at org.springframework.boot.autoconfigure.condition.OnBeanCondition$Spec.validate(OnBeanCondition.java:487) ~[spring-boot-autoconfigure-2.3.3.RELEASE.jar:2.3.3.RELEASE]
  ......
  ... 18 common frames omitted
Caused by: org.springframework.boot.autoconfigure.condition.OnBeanCondition$BeanTypeDeductionException: Failed to deduce bean type for org.springframework.cloud.commons.httpclient.HttpClientConfiguration$ApacheHttpClientConfiguration.apacheHttpClientBuilder
  ......
  ... 20 common frames omitted
Caused by: java.lang.ClassNotFoundException: org.apache.http.impl.client.HttpClientBuilder
  ......
  ... 22 common frames omitted
2021-03-15 07:34:52.548  WARN 11915 --- [           main] o.s.boot.SpringApplication               : Unable to close ApplicationContext
java.lang.IllegalStateException: Failed to introspect Class [org.springframework.cloud.commons.httpclient.HttpClientConfiguration$ApacheHttpClientConfiguration] from ClassLoader [sun.misc.Launcher$AppClassLoader@18b4aac2]
  at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:481) ~[spring-core-5.2.8.RELEASE.jar:5.2.8.RELEASE]
  ......
Caused by: java.lang.NoClassDefFoundError: org/apache/http/impl/client/HttpClientBuilder
  at java.lang.Class.getDeclaredMethods0(Native Method) ~[na:1.8.0_241]
  ......
  ... 21 common frames omitted
Caused by: java.lang.ClassNotFoundException: org.apache.http.impl.client.HttpClientBuilder
  at java.net.URLClassLoader.findClass(URLClassLoader.java:382) ~[na:1.8.0_241]
  ......
  ... 25 common frames omitted
Process finished with exit code 1

经分析发现似乎是httpclient 的报错, 但是心想,我并没有引入这个依赖啊,那么在我没有导入nacos依赖之前,springboot是可以跑起来的,说明可能是nacos 的注册中心和配置中心依赖了httpclient.


2. 报错分析

由于报错项目是springcloud 项目,所以结构比较复杂,于是又新建了一个干净的springboot 项目来引入nacos 的依赖,结构如下:

pom.xml 核心内容如下

<parent>
    <artifactId>spring-boot-dependencies</artifactId>
    <groupId>org.springframework.boot</groupId>
    <version>2.3.3.RELEASE</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <version>2.3.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>2.3.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        <version>2.2.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        <version>2.2.3.RELEASE</version>
    </dependency>
</dependencies>

发现居然没有报错!!!! 很神奇,于是找到依赖

发现 在 nacos 的 注册中心有引入 ribbon 的 一个 httpclient 依赖,而依赖最后依赖的就是 4.5.12 版本的 httpclient



发现这个依赖下确实有这个类,类比的,来分析报错的那个springcloud 项目


这是springcloud 中的依赖,我们发现在这里有两个不同版本的httpclient, 盲猜是低版本的依赖中没有那个类所以与springboot 项目不兼容。打开目录结构果然没有发现 httpClientBuilder 这个类。 至此就破案了,原来是maven从父工程引入的时候,引入了在前边的 4.2.1 版本的依赖而出现不兼容情况(由于jar包多,所以不确定到底是哪个jar引入了这个低版本的)

3. 解决方案

我们在 父工程中显示声明 httpclient 的版本即可

<properties>
   <httpclient.version>4.5</httpclient.version>
</properties>
<dependencyManagement>
   <dependencies>
       <dependency>
           <groupId>org.apache.httpcomponents</groupId>
           <artifactId>httpclient</artifactId>
           <version>${httpclient.version}</version>
       </dependency>
   </dependencies>
</dependencyManagement>
相关文章
|
26天前
|
人工智能 Java API
Nacos 3.1.0 正式发布,支持 A2A 注册中心与 MCP 注册协议增强
3.1.0 发布核心全新功能-Agent 注册中心,助力构建基于 A2A 协议的多 Agent 协作的AI应用,同时 MCP 注册中心适配最新 MCP 官方注册中心协议及升级优化多项核心功能。
421 10
|
10月前
|
负载均衡 Kubernetes 网络协议
注册中心如何选型?Eureka、Zookeeper、Nacos怎么选
这是小卷对分布式系统架构学习的第9篇文章,继续探讨注册中心的原理及选型。文章详细介绍了Eureka、Nacos的工作机制与特点,并对比了Eureka、Nacos、Consul和Zookeeper在一致性协议、健康检查、负载均衡等方面的差异。最后根据不同的应用场景给出了注册中心的选型建议,帮助读者理解如何选择最适合的注册中心。
801 100
|
7月前
|
缓存 安全 Java
深入解析HTTP请求方法:Spring Boot实战与最佳实践
这篇博客结合了HTTP规范、Spring Boot实现和实际工程经验,通过代码示例、对比表格和架构图等方式,系统性地讲解了不同HTTP方法的应用场景和最佳实践。
688 5
|
8月前
|
缓存 Java 应用服务中间件
微服务——SpringBoot使用归纳——Spring Boot集成Thymeleaf模板引擎——依赖导入和Thymeleaf相关配置
在Spring Boot中使用Thymeleaf模板,需引入依赖`spring-boot-starter-thymeleaf`,并在HTML页面标签中声明`xmlns:th=&quot;http://www.thymeleaf.org&quot;`。此外,Thymeleaf默认开启页面缓存,开发时建议关闭缓存以实时查看更新效果,配置方式为`spring.thymeleaf.cache: false`。这可避免因缓存导致页面未及时刷新的问题。
350 0
|
11月前
|
Java Maven
Maven编译报错:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.13.0:compile 解决方案
在执行Maven项目中的`install`命令时,遇到编译插件版本不匹配的错误。具体报错为:`maven-compiler-plugin:3.13.0`要求Maven版本至少为3.6.3。解决方案是将Maven版本升级到3.6.3或降低插件版本。本文详细介绍了如何下载、解压并配置Maven 3.6.3,包括环境变量设置和IDEA中的Maven配置,确保项目顺利编译。
12278 5
Maven编译报错:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.13.0:compile 解决方案
|
存储 缓存 负载均衡
Nacos注册中心
Nacos注册中心
258 1
Nacos注册中心
|
Java 网络架构 Kotlin
kotlin+springboot入门级别教程,教你如何用kotlin和springboot搭建http
本文是一个入门级教程,介绍了如何使用Kotlin和Spring Boot搭建HTTP服务,并强调了Kotlin的空安全性特性。
349 8
kotlin+springboot入门级别教程,教你如何用kotlin和springboot搭建http
|
12月前
|
缓存 Java 网络安全
Solr7.4.0报错org.apache.solr.common.SolrException
在使用 Solr 7.4.0 时,`org.apache.solr.common.SolrException` 错误可能由多种原因引起,包括配置文件错误、核心未加载、权限问题、Java 环境问题、依赖库冲突和网络配置问题。通过检查配置文件、确保正确的权限、验证Java环境、解决依赖库冲突和检查网络配置,可以有效解决这些问题。结合日志文件、管理控制台和调试模式,可以快速定位和解决错误,确保 Solr 系统的稳定运行。
540 1
|
Java Maven Android开发
eclipse如何导入springboot项目
本文介绍了如何在Eclipse中导入Spring Boot项目。
441 1
eclipse如何导入springboot项目

热门文章

最新文章

推荐镜像

更多