java.lang.IllegalStateException:没有为 loadBalancing 定义的假客户端。您是否忘记包含 spring-cloud-starter-netflix-ribbon?

2022-09-04 01:03:48

我遇到异常:

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>

我也在我的其他项目中使用假客户端,没有任何额外的功能区依赖性,并且这些都使用相同的spring-boot版本2.0.8

我已经为同一问题引用了其他堆栈溢出链接,他们要求添加功能区的其他依赖项。我尝试过在我的pom中添加相同的.xml但它没有帮助。

假装客户端接口是,

@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。


答案 1

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

Spring Cloud Loadbalancer是一个通用的抽象,可以完成我们过去在Netflix的Ribbon项目中所做的工作。Spring Cloud 仍然支持 Netflix Ribbon,但 Netflix Ribbons 的日子已经屈指可数了,就像 Netflix 微服务堆栈中的许多其他内容一样,因此我们提供了一个抽象来支持替代方案

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

(1) 弹簧云负载均衡器

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

答案 2

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

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

推荐