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

时间:2017-05-17 16:56:07

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

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

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

我在这里做错了什么?

My 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;
      }
   }
}

1 个答案:

答案 0 :(得分:0)

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

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

相关问题