gpt4 book ai didi

java - 如何为同一个 Feign 客户端接口(interface)的不同配置文件切换 2 个注释

转载 作者:行者123 更新时间:2023-12-02 08:52:20 32 4
gpt4 key购买 nike

如何为同一个 Feign 客户端接口(interface)的不同配置文件切换 2 个注释?

我使用 Feign 客户端接口(interface),与负载均衡器 url 一起使用时,该接口(interface)具有以下代码。我将其称为 Non-Eureka 以供引用:

@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 注释处于 Activity 状态,否则非 Eureka 处于 Activity 状态。

我必须以某种方式使用单个接口(interface)名称来完成此操作,因为我像下面这样使用它:

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

如果以某种方式使用@Profile, @ConditionalOnProperty,请告诉我或其他任何有助于解决我的目的的事情。我正在使用 Spring-boot 2.x 和 Java 8

编辑请注意在 Eureka 案例中我正在使用 pathvalue属性,在非 Eureka 情况下,我使用 name 和 url 属性,这就是问题所在。

最佳答案

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

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

关于java - 如何为同一个 Feign 客户端接口(interface)的不同配置文件切换 2 个注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60695245/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com