赞
踩
最近在使用最新版本的nacos和feign的过程中,启动时遇到一个错误:
Caused by: java.lang.IllegalStateException: No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalancer?
百思不得其解,之前的spring cloud版本没有遇到这样的问题。花了将近几个小时,终于解决,记录一下,希望能给大家带来一点帮助。
项目中的父pom文件版本依赖管理如下:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.4</version> </parent> <properties> <spring-cloud.version>2020.0.2</spring-cloud.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>2.2.0.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <!--spring cloud --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
用的是最新版本的springboot 和springcloud,然后在子项目中,我用到了feign和nacos,依赖如下:
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
第一时间想到的,spring cloud和springboot版本不兼容。查看spring cloud官网,发现spring boot版本号和spring cloud版本号可以对应:
说明项目是没问题的,就上网查找了一下,结果参考了这两位仁兄说的:
在项目中加入eureka依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
再次启动,报错:
Field autoServiceRegistration in org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationAutoConfiguration required a single bean, but 2 were found:
- nacosAutoServiceRegistration: defined by method 'nacosAutoServiceRegistration' in class path resource [com/alibaba/cloud/nacos/registry/NacosServiceRegistryAutoConfiguration.class]
- eurekaAutoServiceRegistration: defined by method 'eurekaAutoServiceRegistration' in class path resource [org/springframework/cloud/netflix/eureka/EurekaClientAutoConfiguration.class]
Action:
Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed
这是因为项目中已经用到了nacos,再次引入eureka,会造成有两个注册中心的问题,说明这种解决方案无效。
既然之前的版本没问题,那我就降级回去不就完事了,于是乎,我把项目的父pom中spring cloud版本降级到Hoxton SR1,spring boot版本也随之降级到2.2.1,即:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.1.RELEASE</version> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>2.2.0.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <!--spring cloud --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Hoxton.SR1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
maven clean后再次启动,仍然报错:
错误信息:
Caused by: java.lang.IllegalStateException: No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalancer?
at org.springframework.cloud.openfeign.FeignClientFactoryBean.loadBalance(FeignClientFactoryBean.java:333) ~[spring-cloud-openfeign-core-3.0.2.jar:3.0.2]
at org.springframework.cloud.openfeign.FeignClientFactoryBean.getTarget(FeignClientFactoryBean.java:360) ~[spring-cloud-openfeign-core-3.0.2.jar:3.0.2]
at org.springframework.cloud.openfeign.FeignClientFactoryBean.getObject(FeignClientFactoryBean.java:339) ~[spring-cloud-openfeign-core-3.0.2.jar:3.0.2]
at org.springframework.cloud.openfeign.FeignClientsRegistrar.lambda$registerFeignClient$0(FeignClientsRegistrar.java:230) ~[spring-cloud-openfeign-core-3.0.2.jar:3.0.2]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.obtainFromSupplier(AbstractAutowireCapableBeanFactory.java:1231) ~[spring-beans-5.3.5.jar:5.3.5]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1173) ~[spring-beans-5.3.5.jar:5.3.5]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:564) ~[spring-beans-5.3.5.jar:5.3.5]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524) ~[spring-beans-5.3.5.jar:5.3.5]
... 34 common frames omitted
这次的错误信息跟第一次一样,可见降低版本这事已经行不通了,于是我复原了父pom。
在stackoverflow上看到了这两种解决方案:
因为不想在feign上加url,显得比较麻烦,于是我选择了第二种方案,加ribbon并配置了相关属性,启动还是报错:
Caused by: java.lang.IllegalStateException: No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalancer?
但是其中的第一种方案,我还是有点感兴趣,于是就根据第一种方案中的关键字spring-cloud-loadbalancer,搜索了一下。
继续搜索,发现如下结果:
根据这位大佬的说法,这是因为:
由于SpringCloud Feign在Hoxton.M2 RELEASED版本之后不再使用Ribbon而是使用spring-cloud-loadbalancer,所以不引入spring-cloud-loadbalancer会报错
解决方法
加入spring-cloud-loadbalancer依赖 并且在nacos中排除ribbon依赖,不然loadbalancer无效
问题突然有了点头目,于是乎,加入如下依赖:
//我是在这引入了spring-cloud-starter-alibaba-nacos-discovery <dependency> <groupId>com.oyz.gulimall</groupId> <artifactId>gulimall-common</artifactId> <version>0.0.1-SNAPSHOT</version> <exclusions> <exclusion> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </exclusion> </exclusions> </dependency> //加入spring-cloud-loadbalancer依赖 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-loadbalancer</artifactId> </dependency>
之后,再次启动项目:
问题完美解决,还是得关注问题报错点:Did you forget to include spring-cloud-starter-loadbalancer,一开始没有注意到这个点,浪费了许多时间。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。