'java.lang.IllegalStateException: No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-netflix-ribbon?
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.
Solution 1:[1]
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
Solution 2:[2]
For me, the problem was not include url
parameter in @FeignClient
annotation:
@FeignClient(name = "microservice-common", url = "${microservice-common.url}")
Solution 3:[3]
Ribbon
Requires Spring Boot Version >= 2.0.0.RELEASE and < 2.4.0-M1
Solution 4:[4]
Use spring-cloud-starter-loadbalancer
dependency instead of spring-cloud-starter-netflix-ribbon
.
Solution 5:[5]
Add ServiceName(i.e feignName) with its URL in your feignClient interface.
@FeignClient(name = "user-service", url = "feignUrl", decode404 = true)
public interface UserFeignClient {
@PostMapping("/do-something")
void doSomething();
}
Feign Client is tool for making easier call to Rest-Api of other service where as Ribbon is used mostly for load balancing.
If you want to use ribbon
- Add this dependency to your pom.xml
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </dependency>
FeignClient interface
@FeignClient(name="ServiceName") @RibbonClient(name="ServiceName")
configure your application.properties like given below
ServiceName.ribbon.listOfServers=http://localhost:8000,http://localhost:8001
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | ℛɑƒæĿᴿᴹᴿ |
Solution 3 | Abduqodir Ubaydullayev |
Solution 4 | Dmitriy Popov |
Solution 5 | Harshal Patil |