Reputation:
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.
Upvotes: 29
Views: 50445
Reputation: 871
For me, the problem was not include url
parameter in @FeignClient
annotation:
@FeignClient(name = "microservice-common", url = "${microservice-common.url}")
Upvotes: 33
Reputation: 71
Use spring-cloud-starter-loadbalancer
dependency instead of spring-cloud-starter-netflix-ribbon
.
Upvotes: 4
Reputation: 838
Ribbon
Requires Spring Boot Version >= 2.0.0.RELEASE and < 2.4.0-M1
Upvotes: 5
Reputation: 2438
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
Upvotes: 15
Reputation: 31
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
<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
Upvotes: 1