提问者:小点点

java.lang.IllegalStateException:未定义用于负载平衡的Feign客户端。你忘了带上Spring云启动器netflix彩带了吗?


我遇到了例外:

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)

执行我的项目时。我的绒球.xml包含

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

我在我的其他项目中也使用了foreign客户端,没有任何附加的ribbon依赖性,这些都使用同一个spring-boot版本2.0.8

我已经为同一问题引用了其他堆栈溢出链接,他们要求添加功能区的其他依赖项。我尝试在我的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));
    }
}

我使用的是Sprint Boot 2.0.8版本。


共3个答案

匿名用户

对我来说,问题不在于在< code>@FeignClient注释中包含< code>url参数:

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

匿名用户

您必须决定使用哪个客户端负载均衡器:(1)Spring Cloud Loadbalancer或(2)功能区。

Spring Cloud负载均衡器是一个通用抽象,它可以完成我们过去对Netflix的Ribbon项目所做的工作。Spring Cloud仍然支持NetflixRibbon,但NetflixRibbon的日子已经屈指可数,就像Netflix微服务堆栈的其他许多日子一样,所以我们提供了一个抽象来支持替代方案

检查此处:https://spring.io/blog/2020/03/25/spring-tips-spring-cloud-loadbalancer

(1)Spring云负载均衡器:

spring:
  cloud:
    loadbalancer:
       ribbon:
        enable: false

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

(2)功能区:添加依赖项:

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

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

   spring:
      cloud:
        loadbalancer:
           ribbon:
            enable: true

匿名用户

丝带

需要Spring Boot版本