繁体   English   中英

如何为同一 Feign 客户端界面的不同配置文件切换 2 个注释

[英]How to have 2 annotation toggled for different profile for the same Feign client Interface

如何为同一 Feign 客户端界面的不同配置文件切换 2 个注释?

我使用了一个 Feign Client Interface,它在与负载均衡器 url 一起使用时具有以下代码。 我称之为非尤里卡以供参考:

@FeignClient(name = "DEPOSIT-FEIGN-CLIENT", url = "${DEPOSIT-DATA-URL}")
public interface DepositFeignClient {

    @RequestMapping(method = RequestMethod.GET, value = "/path/to/api/{accountNumber}", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
    DepositResponse getDepositDetails(@PathVariable(value = "accountNumber") String accountNumber);

}

另一方面,我在使用 Eureka 和 Spring Cloud Gateway 时使用以下代码:

@FeignClient(value = "ABCD-GATEWAY", path = "${DEPOSIT-EUREKA-APPNAME}")
public interface DepositFeignClient {

    @RequestMapping(method = RequestMethod.GET, value = "/path/to/api/{accountNumber}", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
    DepositResponse getDepositDetails(@PathVariable(value = "accountNumber") String accountNumber);

}

现在我的要求是通过类似 Spring Profile 的东西来控制它们,以便如果配置文件名称 = "Eureka",则 Eureka 注释处于活动状态,否则非尤里卡处于活动状态。

我必须以某种方式在单个接口名称中执行此操作,因为我使用它如下:

private final DepositFeignClient depositFeignClient;
//other code
DepositResponse depResponse =
                    depositFeignClient.getDepositDetails(accountNumber);
//other code

请让我知道以某种方式使用@Profile, @ConditionalOnProperty或其他任何东西是否有助于解决我的目的。 我正在使用 Spring-boot 2.x 和 Java 8

编辑请注意,在尤里卡的情况下,我使用的是pathvalue属性,而在非尤里卡的情况下,我使用的是名称和 url 属性,这就是问题所在。

您应该尝试创建 2 个application-{profileName}.properites文件,根据需要设置参数,然后在该配置文件处于活动状态的情况下运行。

本文第 8 节也可能对您有用: https : //www.baeldung.com/spring-profiles

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM