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

user4811324 picture user4811324 · Sep 2, 2020 · Viewed 10.6k times · Source

I am getting exception:

FactoryBean threw exception on object creation; nested exception is java.lang.IllegalStateException: No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-netflix-ribbon?
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:178)
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:101)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1654)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getObjectForBeanInstance(AbstractAutowireCapableBeanFactory.java:1174)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:257)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:224)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveNamedBean(DefaultListableBeanFactory.java:1012)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:338)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:333)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1105)
    at com.hsbc.gbgcf.spartan.referencedatabase.UserRegistrationApplication.main(UserRegistrationApplication.java:57)

when executing my project. My pom.xml contains

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

I am using feign client in other projects of mine as well without any additional dependency of ribbon and these are working with same spring-boot version 2.0.8

I have referred other stack overflow link for same issue and they have asked to add additional dependency of ribbon. I have tried adding same in my pom.xml but it didn't helped.

FeignClient interface is,

@FeignClient(value = "user-service", decode404 = true)
public interface UserFeignClient {

    @PostMapping("/do-something")
    void doSomething();
}

Main class code:

@Configuration
@EnableAspectJAutoProxy
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication(scanBasePackages = {"test.user"})
public class UserRegistrationApplication{
 public static void main(String[] args) {
        UserFeignClient userFeignClient = applicationContext.getBean(UserFeignClient.class);
        userFeignClient.doSomething();
        System.exit(SpringApplication.exit(applicationContext));
    }
}

I am using Sprint Boot version 2.0.8.

Answer

Ualter Jr. picture Ualter Jr. · Oct 6, 2020

You have to decide, which client load balancer to use: (1)Spring Cloud Loadbalancer or (2)Ribbon.

Spring Cloud Loadbalancer is a generic abstraction that can do the work that we used to do with Netflix’s Ribbon project. Spring Cloud still supports Netflix Ribbon, but Netflix Ribbons days are numbered, like so much else of the Netflix microservices stack, so we’ve provided an abstraction to support an alternative

Check here: https://spring.io/blog/2020/03/25/spring-tips-spring-cloud-loadbalancer

(1) Spring Cloud Load Balancer:

spring:
  cloud:
    loadbalancer:
       ribbon:
        enable: false

# And... inform the "url" attribute at FeignClient
@FeignClient(name = "student", url = "student") 

(2) Ribbon: Add the dependency:

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

# And (optionally)... @application.yaml

   spring:
      cloud:
        loadbalancer:
           ribbon:
            enable: true