如何为同一Feign客户端接口的不同配置拥有两个注释开关

huangapple go评论207阅读模式
英文:

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

问题

  1. // 在同一个Feign客户端接口中使用不同的注解进行切换的方法
  2. // 当使用负载均衡器URL时,使用以下代码。这里称为非Eureka的参考示例:
  3. @FeignClient(name = "DEPOSIT-FEIGN-CLIENT", url = "${DEPOSIT-DATA-URL}")
  4. public interface DepositFeignClient {
  5. @RequestMapping(method = RequestMethod.GET, value = "/path/to/api/{accountNumber}",
  6. produces = MediaType.APPLICATION_JSON_VALUE,
  7. consumes = MediaType.APPLICATION_JSON_VALUE)
  8. DepositResponse getDepositDetails(@PathVariable(value = "accountNumber") String accountNumber);
  9. }
  10. // 当使用Eureka和Spring Cloud Gateway时,使用以下代码:
  11. @FeignClient(value = "ABCD-GATEWAY", path = "${DEPOSIT-EUREKA-APPNAME}")
  12. public interface DepositFeignClient {
  13. @RequestMapping(method = RequestMethod.GET, value = "/path/to/api/{accountNumber}",
  14. produces = MediaType.APPLICATION_JSON_VALUE,
  15. consumes = MediaType.APPLICATION_JSON_VALUE)
  16. DepositResponse getDepositDetails(@PathVariable(value = "accountNumber") String accountNumber);
  17. }

现在我的要求是通过类似于Spring配置文件中的Profile来控制它们,如果配置文件中的Profile名为"Eureka",则激活Eureka的注解,否则激活Non-Eureka的注解。

我必须在单个接口名称中解决这个问题,因为我在代码中使用它,如下所示:

  1. private final DepositFeignClient depositFeignClient;
  2. //其他代码
  3. DepositResponse depResponse = depositFeignClient.getDepositDetails(accountNumber);
  4. //其他代码

请告诉我是否可以通过@Profile@ConditionalOnProperty或其他方式来实现我的目的。我正在使用Spring Boot 2.x和Java 8。

注意
请注意,在Eureka的情况下,我使用了pathvalue属性,而在非Eureka的情况下,我使用了nameurl属性,这就是问题所在。

英文:

How to have 2 annotation toggled for different profile for the same Feign client Interface?

I use a Feign Client Interface which has the following code when used with load balancer url. This I call as Non-Eureka for reference:

  1. @FeignClient(name = "DEPOSIT-FEIGN-CLIENT", url = "${DEPOSIT-DATA-URL}")
  2. public interface DepositFeignClient {
  3. @RequestMapping(method = RequestMethod.GET, value = "/path/to/api/{accountNumber}", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
  4. DepositResponse getDepositDetails(@PathVariable(value = "accountNumber") String accountNumber);
  5. }

On the other hand I use the following code when using Eureka and Spring Cloud Gateway:

  1. @FeignClient(value = "ABCD-GATEWAY", path = "${DEPOSIT-EUREKA-APPNAME}")
  2. public interface DepositFeignClient {
  3. @RequestMapping(method = RequestMethod.GET, value = "/path/to/api/{accountNumber}", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
  4. DepositResponse getDepositDetails(@PathVariable(value = "accountNumber") String accountNumber);
  5. }

Now my requirement is to control them through something like a Spring Profile, so that if the profile name = "Eureka" the Eureka annotation is active and Non-Eureka is active otherwise.

I have to do it somehow in a single Interface name since I use it like the following:

  1. private final DepositFeignClient depositFeignClient;
  2. //other code
  3. DepositResponse depResponse =
  4. depositFeignClient.getDepositDetails(accountNumber);
  5. //other code

Please let me know if somehow using @Profile, @ConditionalOnProperty or anything else would help solve my purpose. I am using Spring-boot 2.x and Java 8

Edit
Kindly note in Eureka case I am using path and value attributes and in non-Eureka case I am using name and url attributes and that is the problem.

答案1

得分: 1

你可以尝试创建2个application-{profileName}.properties文件,根据需要设置参数,然后在活动配置文件中运行。

该文章的第8节对您也可能有帮助:https://www.baeldung.com/spring-profiles

英文:

You should try creating 2 application-{profileName}.properites files, setting the parameters as required, then running with that profile active.

Section 8 of this article may also be of use to you: https://www.baeldung.com/spring-profiles

huangapple
  • 本文由 发表于 2020年3月16日 00:43:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/60695245.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定