当前位置: 首页 > 知识库问答 >
问题:

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

冯永长
2023-03-14

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

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

我做错了什么?

我的假客户:

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

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

}

在引导中。yml:

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

如何在Spring Boot应用程序中配置Faigns客户端:

@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个答案

于正志
2023-03-14

实现您需求的最简单方法是。。

在代码中,删除与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

像您那样定义ribbonServerListbean是实现这一点的另一种方法,我不知道为什么您的代码没有运行。在我的例子中,类似于您的代码工作得很好。但是有一个更简单的方法,所以请尝试一下。

 类似资料:
  • 我在ubuntu中使用sudo-apt-get安装了ApacheTomcat7。我已经使用Windows将apache tomcat 7添加到eclipse中 无法在localhost配置 /Servers/Tomcatv7.0服务器上加载Tomcat服务器配置。配置可能已损坏或不完整。 我提供的tomcat7路径是/usr/share/tomcat7

  • 错误: > 连客户端都没有启动 http://localhost:8080/ 将spring.config.import=configServer:属性添加到您的配置中。如果不需要配置,则添加spring.config.import=optional:configserver:。要禁用此检查,请设置spring.cloud.config.enabled=false或spring.cloud.con

  • 我可以在我自己的电脑上使用这个程序,但是我不能在服务器上使用。 服务器使用最高权限管理员打开程序。 具有的服务器WCF HTTP激活功能。NET4。5号门开着。 服务器endpoint地址使用"http://localhost",如下所示 endpoint地址="http://localhost"绑定="basicHttpBind"bindingConfiguration="NewBinding0

  • 当我试图启动SpringBoot主应用程序时,出现以下异常。为什么我会得到这个特例。 异常: Spring boot主java类

  • 我正在关注应用程序中的条目。 我有下面的spring云服务器应用程序代码。 我收到以下错误。 启动ApplicationContext时出错。要显示条件报告,请在启用“调试”的情况下重新运行应用程序。2021 02月24日01:39:52.356错误20804---[restartedMain]o.s.b.d.LoggingFailureAnalysisReporter: 应用程序无法启动 描述:

  • 我们来看看如何配置服务器端的 SSH 访问。 本例中,我们将使用 authorized_keys 方法来对用户进行认证。 同时我们假设你使用的操作系统是标准的 Linux 发行版,比如 Ubuntu。 首先,创建一个操作系统用户 git,并为其建立一个 .ssh 目录。 $ sudo adduser git $ su git $ cd $ mkdir .ssh && chmod 700 .ssh