java.lang.IllegalStateException:没有为负载均衡定义 Feign 客户端。您是否忘记包含 spring-cloud-starter-netflix-ribbon?

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

我遇到异常:

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)

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

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

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

我已将其他堆栈溢出 link 转介给相同的问题,他们要求添加额外的功能区依赖项。我试过在我的 pom.xml 中添加相同的内容,但没有帮助。

FeignClient接口是,

@FeignClient(value = "user-service", decode404 = true)
public interface UserFeignClient {

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

主要class代码:

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

在您的 feignClient 界面中添加 ServiceName(即 feignName)及其 URL。

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

Feign Client 是一种工具,可以更轻松地调用其他服务的 Rest-Api,因为 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")
    
  2. 配置您的 application.properties 如下所示

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

您必须决定使用哪个客户端负载均衡器:(1)Spring Cloud Loadbalancer 或 (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

在这里查看: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

Ribbon

需要 Spring 启动版本 >= 2.0.0.RELEASE 和 < 2.4.0-M1

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

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

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