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

huangapple go评论84阅读模式
英文:

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

问题

我遇到了异常:

FactoryBean在创建对象时抛出异常;嵌套异常是java.lang.IllegalStateException:未定义用于负载均衡的Feign客户端。您是否忘记包含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)

在执行我的项目时。我的pom.xml文件包含:

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

我还在我的其他项目中使用了Feign客户端,而没有附加任何ribbon的其他依赖项,这些项目都在相同的spring-boot版本2.0.8下工作。

我已经查阅了其他堆栈溢出链接以解决相同的问题,他们要求添加ribbon的额外依赖。我尝试将相同的内容添加到我的pom.xml中,但没有帮助。

FeignClient接口如下:

@FeignClient(value = "user-service", decode404 = true)
public interface UserFeignClient {
    @PostMapping("/do-something")
    void doSomething();
}

主类代码如下:

@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));
    }
}

我正在使用Spring Boot版本2.0.8。

英文:

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

&lt;dependency&gt;
	&lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
	&lt;artifactId&gt;spring-cloud-starter-openfeign&lt;/artifactId&gt;
  &lt;/dependency&gt;

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 = &quot;user-service&quot;, decode404 = true)
public interface UserFeignClient {

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

Main class code:

@Configuration
@EnableAspectJAutoProxy
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication(scanBasePackages = {&quot;test.user&quot;})
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.

答案1

得分: 23

对我来说,问题不是在@FeignClient注解中包含url参数:

@FeignClient(name = "microservice-common", url = "${microservice-common.url}")
英文:

For me, the problem was not include url parameter in @FeignClient annotation:

@FeignClient(name = &quot;microservice-common&quot;, url = &quot;${microservice-common.url}&quot;)

答案2

得分: 12

(1) Spring Cloud Load Balancer:

spring:
  cloud:
    loadbalancer:
       ribbon:
        enable: false

# 并且... 在 FeignClient 中指定 "url" 属性
@FeignClient(name = "student", url = "student")

(2) Ribbon:
添加依赖项:

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

# 并且(可选地)... 在 application.yaml 中
   spring:
      cloud:
        loadbalancer:
           ribbon:
            enable: true
英文:

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 &quot;url&quot; attribute at FeignClient
@FeignClient(name = &quot;student&quot;, url = &quot;student&quot;) 

(2) Ribbon:
Add the dependency:

&lt;dependency&gt;
	&lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
	&lt;artifactId&gt;spring-cloud-starter-netflix-ribbon&lt;/artifactId&gt;
&lt;/dependency&gt;

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

   spring:
      cloud:
        loadbalancer:
           ribbon:
            enable: true

答案3

得分: 5

> 带带

需要 Spring Boot 版本 >= 2.0.0.RELEASE 且 < 2.4.0-M1

英文:

> Ribbon

Requires Spring Boot Version >= 2.0.0.RELEASE and < 2.4.0-M1

答案4

得分: 4

使用 spring-cloud-starter-loadbalancer 依赖替代 spring-cloud-starter-netflix-ribbon

英文:

Use spring-cloud-starter-loadbalancer dependency instead of spring-cloud-starter-netflix-ribbon.

答案5

得分: 1

添加 ServiceName(即feignName),以及它在feignClient接口中的URL。

@FeignClient(name = "user-service", url = "feignUrl", decode404 = true)
public interface UserFeignClient {
    @PostMapping("/do-something")
    void doSomething();
}

Feign客户端是用于更轻松地调用其他服务的Rest API的工具,而Ribbon主要用于负载平衡。

如果你想要使用Ribbon:

  1. 在你的pom.xml中添加以下依赖:
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
  1. FeignClient接口
@FeignClient(name="ServiceName")
@RibbonClient(name="ServiceName")
  1. 像下面这样配置你的 application.properties 文件
ServiceName.ribbon.listOfServers=http://localhost:8000,http://localhost:8001
英文:

Add ServiceName(i.e feignName) with its URL in your feignClient interface.

@FeignClient(name = &quot;user-service&quot;, url = &quot;feignUrl&quot;, decode404 = true)
public interface UserFeignClient {
    @PostMapping(&quot;/do-something&quot;)
    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

  1. Add this dependency to your pom.xml

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

  1. FeignClient interface

    @FeignClient(name=&quot;ServiceName&quot;)
    @RibbonClient(name=&quot;ServiceName&quot;)
    
  2. configure your application.properties like given below

> ServiceName.ribbon.listOfServers=http://localhost:8000,http://localhost:8001

huangapple
  • 本文由 发表于 2020年9月2日 17:44:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/63702885.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定