英文:
How to have 2 annotation toggled for different profile for the same Feign client Interface
问题
// 在同一个Feign客户端接口中使用不同的注解进行切换的方法
// 当使用负载均衡器URL时,使用以下代码。这里称为非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来控制它们,如果配置文件中的Profile名为"Eureka",则激活Eureka的注解,否则激活Non-Eureka的注解。
我必须在单个接口名称中解决这个问题,因为我在代码中使用它,如下所示:
private final DepositFeignClient depositFeignClient;
//其他代码
DepositResponse depResponse = depositFeignClient.getDepositDetails(accountNumber);
//其他代码
请告诉我是否可以通过@Profile
、@ConditionalOnProperty
或其他方式来实现我的目的。我正在使用Spring Boot 2.x和Java 8。
注意
请注意,在Eureka的情况下,我使用了path
和value
属性,而在非Eureka的情况下,我使用了name
和url
属性,这就是问题所在。
英文:
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:
@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);
}
On the other hand I use the following code when using Eureka and 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);
}
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:
private final DepositFeignClient depositFeignClient;
//other code
DepositResponse depResponse =
depositFeignClient.getDepositDetails(accountNumber);
//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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论