如何将多个URLs配置到@FeignClient中?

问题描述 投票:0回答:1
  • 我有这个 应用程序.propeties
service.path=http://localhost:8000

forex.service.name=forex-service
  • 我有一个配置:"FeignClient"
@FeignClient(name="${forex.service.name}", url="${service.path}")
public interface CurrencyExchangeServiceProxy {


  @GetMapping("/currency-exchange/from/{from}/to/{to}")
  CurrencyConversionBean retrieveExchangeValue(
          @PathVariable("from") String from, @PathVariable("to") String to);

我想设置多个URLS。例如,如果其中一个不可用,那么 "FeignClient "必须转到另一个。

谁有什么办法可以做到这一点?

spring-boot spring-cloud-feign
1个回答
0
投票

我决定问题如此。

  • 在所有的微服务中都需要删除依赖关系。spring-cloud-starter-netflix-eureka-client-----。 (我不使用名称服务器来发现客户端)。

    • 删除一个注释 @启用发现客户端. 在我的例子中,这个注解不需要。

    • 在一个介于客户端请求和其他微服务之间的微服务上,你需要定义。

    • application.properties

forex.service.name=forex-service

forex-service.ribbon.listOfServers=localhost:8000,localhost:8001,localhost:8002

当然,你必须在指定的URL上创建服务,并运行他们的服务。


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

              <dependency>
                  <groupId>org.springframework.cloud</groupId>
                  <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
              </dependency>
  • 代理Feign客户端
@FeignClient(name="${forex.service.name}")
@RibbonClient(name="${forex.service.name}")
public interface CurrencyExchangeServiceProxy {

  @GetMapping("/currency-exchange/from/{from}/to/{to}")
  CurrencyConversionBean retrieveExchangeValue(
          @PathVariable("from") String from, @PathVariable("to") String to);

只有当你使用嵌入式Tomcat(Srping boot 2.2.7)时,这个工作才会进行。

如果你定义了外部服务器(例如,Weblogic),你的请求将尝试在该服务器上转发。但你会得到错误。

© www.soinside.com 2019 - 2024. All rights reserved.