空的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>
  • 13
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值