无法使用服务器列表配置@FeignClient

Unable to configure @FeignClient with list of servers

我无法使用要使用的服务器列表配置 @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 引导应用程序中配置 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;
      }
   }
}

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

在您的代码中,删除所有与 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 是实现此目的的另一种方法,我不确定为什么您的代码不是 运行。就我而言,与您类似的代码运行良好。但是有一个更简单的方法,所以请尝试一下。