Spring cloud Nacos and feign report errors: Java lang.IllegalStateException: No Feign Client for loadBalancing defined

Posted by lisawebs on Tue, 08 Mar 2022 20:32:23 +0100

Problem description

Recently, during the process of using the latest version of nacos and feign, an error was encountered during startup:

Caused by: java.lang.IllegalStateException: No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalancer?


Puzzled, the previous version of spring cloud did not encounter such a problem. It took nearly several hours to finally solve it. I hope it can bring you some help.

Project environment

The parent pom file version dependency management in the project is as follows:

 <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>

The latest versions of springboot and springcloud are used. In the subproject, feign and nacos are used. The dependencies are as follows:

 <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>

Solution

1. Introduce eureka dependency - invalid

First thought, spring cloud and springboot versions are incompatible. Check the official website of spring cloud and find that the spring boot version number and spring cloud version number can correspond to each other:

It shows that there is no problem with the project, so I searched the Internet, and the results refer to what the two brothers said:

Add eureka dependency to the project:

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

Start again and report an error:

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



This is because nacos has been used in the project. The re introduction of eureka will cause the problem of two registries, indicating that this solution is invalid.

2. Reduce spring cloud version - invalid

Since there is no problem with the previous version, I will downgrade it back, so I downgrade the spring cloud version in the parent pom of the project to Hoxton SR1, and the spring boot version will also be downgraded to 2.2.1, that is:

 <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>


After maven clean is started again, an error is still reported:


Error message:

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

The error message this time is the same as the first time. It can be seen that reducing the version is no longer feasible, so I restored the parent pom.

3. Add ribbon - invalid

You can see these two solutions on stackoverflow:

Because I don't want to add a url to feign, it seems more troublesome, so I chose the second scheme, add ribbon and configure relevant properties, start or report an error:

Caused by: java.lang.IllegalStateException: No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalancer?

However, I was still a little interested in the first scheme, so I searched according to the keyword spring cloud loadbalancer in the first scheme.

4. Add spring cloud loadbalancer and exclude ribbon dependency in nacos -- problem solved

Continue searching in the search and find the following results:

According to the boss, this is because:

Because spring cloud feign is in Hoxton After the M2 released version, the Ribbon is no longer used, but the spring cloud loadbalancer is used. Therefore, if the spring cloud loadbalancer is not introduced, an error will be reported
resolvent
Add spring cloud loadbalancer dependency and exclude ribbon dependency in nacos, otherwise loadbalancer is invalid

The problem suddenly got a little head, so we added the following dependencies:

//I introduced spring cloud starter Alibaba Nacos discovery here
<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>
//Add spring cloud loadbalancer dependency


<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-loadbalancer</artifactId>
</dependency>

After that, start the project again:

To solve the problem perfectly, we still have to pay attention to the problem and report the wrong point: did you forget to include spring cloud starter loadbalancer. At the beginning, we didn't notice this point and wasted a lot of time.

Reference link:

  1. Caused by: java.lang.IllegalStateException: No Feign Client for loadBalancing defined
  2. stackoverflow
  3. SpringCloud OpenFeign reports an error
  4. No Feign Client for loadBalancing defined

Topics: Java Spring Spring Boot Back-end Spring Cloud