【问题标题】:Unable to configure @FeignClient with list of servers无法使用服务器列表配置 @FeignClient
【发布时间】:2017-10-17 06:25:13
【问题描述】:

我无法为@FeignClient 配置要使用的服务器列表。我正在使用 Spring Cloud Netflix,但此特定服务 (foo-service) 未在 Eureka 注册。为此,我需要在 YML 文件中为 foo-service 配置服务器列表。

但是,listOfServers 永远不会被读取,因此操作失败,因为 Feign/Ribbon 没有单个服务器可供使用。

我在这里做错了什么?

我的 Feign 客户:

@FeignClient(name="foo-service")
public interface FooFeignClient {

   @RequestMapping(value = "/perform-check", method = POST)
   ResponseEntity<FooResponse> performCheck(FooRequest fooRequest);

}

在 bootstrap.yml 中:

foo-service:
   ribbon:
      eureka:
         enabled: false
      listOfServers: foobox1,foobox2,foobox3

在 Spring Boot 应用中如何配置 Feign 客户端:

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableHazelcastClient
@EnableFeignClients
@RibbonClients({
   @RibbonClient(name = "foo-service", configuration = MyApp.FooServiceRibbonConfig.class)
})
public class MyApp {

   public static void main(String[] args) {
      SpringApplication.run(MyApp.class, args);
   }

   ....

   @Configuration
   static class FooServiceRibbonConfig {

      @Bean
      @ConditionalOnMissingBean
      public IClientConfig ribbonClientConfig() {
         DefaultClientConfigImpl config = new DefaultClientConfigImpl();
         config.loadProperties("foo-service");
         return config;
      }

      @Bean
      ServerList<Server> ribbonServerList(IClientConfig config) {
         ConfigurationBasedServerList serverList = new ConfigurationBasedServerList();
         serverList.initWithNiwsConfig(config);
         return serverList;
      }
   }
}

【问题讨论】:

  • listOfServers 区分大小写,应为ListOfServers
  • @spencergibb ListOfServers 也不起作用。我试图听从这里的建议github.com/spring-cloud/spring-cloud-netflix/issues/325
  • 您需要提供一个重现问题的示例项目。
  • @spencergibb 我还是和作者一样的问题。如果可以的话,这里是一个重现错误的示例项目:github.com/RaviH/spring-cloud-feign-demo 当您执行 DemoServiceTest 时应该会看到此消息:Load balancer does not have available server for client: demoservice

标签: spring-cloud spring-cloud-netflix netflix-feign netflix-ribbon


【解决方案1】:

满足您需求的最简单方法是..

在您的代码中,删除与FooServiceRibbonConfig 相关的所有代码,如下所示。

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableHazelcastClient
@EnableFeignClients
})
public class MyApp {

   public static void main(String[] args) {
      SpringApplication.run(MyApp.class, args);
   }
   ....
}

然后像下面这样更改您的个人资料文件。

foo-service:
   ribbon:
      NIWSServerListClassName: com.netflix.loadbalancer.ConfigurationBasedServerList
      listOfServers: foobox1,foobox2,foobox3

像您一样定义ribbonServerList bean 是实现此目的的另一种方法,我不确定您的代码为什么没有运行。就我而言,像你这样的类似代码效果很好。但是有一个更简单的方法,所以请尝试一下。

【讨论】:

  • 我得到了同样的例外:java.lang.RuntimeException: com.netflix.client.ClientException: Load balancer does not have available server for client: foo-service。就好像根本没有读取YML文件一样。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-16
  • 2018-08-24
  • 1970-01-01
  • 1970-01-01
  • 2016-11-06
  • 2021-06-15
  • 1970-01-01
相关资源
最近更新 更多