英文:
How can I change feign client url according to property file?
问题
@FeignClient(
name = "feign-client-name",
url = "${feign.client.url}",
configuration = FeignClientConfiguration.class)
public interface SomeFeignClient {
@GetMapping(SOME_GEP_MAPPING_PATH)
Entity getEntity(String id);
}
feign:
client:
url: https://url-to-service.com
token: secret_token
internal-url: https://url-to-internal-service.com
internal: on
@RequiredArgsConstructor
public class FeignClientConfiguration {
private final FeignProperties properties;
@Bean
public RequestInterceptor requestInterceptor() {
return template -> {
template.header(HttpHeaders.AUTHORIZATION, "Token " + properties.getToken());
template.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
template.header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE);
};
}
}
如何根据 internal
属性更改 Feign 客户端的 URL?
我希望它按照以下方式工作:
如果 internal
属性的值为 on
,Feign 客户端应该使用 internal-url
的值,否则使用 url
的值。
UPD:
可能的解决方案 - 使用Spring配置文件。
英文:
I have a Feign client that send request to given url
@FeignClient(
name = "feign-client-name",
url = "${feign.client.url}",
configuration = FeignClientConfiguration.class)
public interface SomeFeignClient {
@GetMapping(SOME_GEP_MAPPING_PATH)
Entity getEntity(String id);
}
<!-- end snippet -->
feign:
client:
url: https://url-to-service.com
token: secret_token
internal-url: https://url-to-internal-service.com
internal: on
<!-- end snippet -->
@RequiredArgsConstructor
public class FeignClientConfiguration {
private final FeignProperties properties;
@Bean
public RequestInterceptor requestInterceptor() {
return template -> {
template.header(HttpHeaders.AUTHORIZATION, "Token " + properties.getToken());
template.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
template.header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE);
};
}
}
How can I change the url for feign client depends on internal
property?<br>
I want it work in following way:
if internal
property has value on
feign client should use internal-url
value and url
value in other case
UPD:<br>
Possible solution -to use Spring Profiles.
答案1
得分: 1
我已找到无需使用Spring配置文件的解决方案。
这个解决方案的主要思想是使用@ConditionalOnProperty
注解,它可以根据特定的属性来阻止Bean的创建。在这种情况下,
首先,我们需要创建一个新的接口,例如SomeFeignClient
public interface SomeFeignClient {
Entity getEntity(String id);
}
其次,创建两个Feign客户端,它们将扩展我们的接口并使用@ConditionalOnProperty
注解标记它们
@ConditionalOnProperty(prefix="feign.client", name="internal", havingValue="true")
@FeignClient(...你的配置在这里...)
public interface SomeFeignClientFirst extends SomeFeignClient {
@Override
Entity getEntity(String id);
}
@ConditionalOnProperty(prefix="feign.client", name="internal", havingValue="false")
@FeignClient(...你的配置在这里...)
public interface SomeFeignClientSecond extends SomeFeignClient {
@Override
Entity getEntity(String id);
}
如果要为其中某个Feign客户端添加请求拦截器,请不要忘记使用@ConditionalOnBean
注解来标记它们
@RequiredArgsConstructor
public class FeignClientConfiguration {
private final FeignProperties properties;
@ConditionalOnBean(SomeFeignClientFirst.class)
@Bean
public RequestInterceptor requestInterceptor() {
return template -> {
template.header(HttpHeaders.AUTHORIZATION, "Token " + properties.getToken());
template.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
template.header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE);
};
}
}
这些是你提供的代码部分的翻译。
英文:
I have found solution without Spring profiles.<br>
The main idea of this soultion is to use @ConditionalOnProperty
annotation that prevent bean creation depends on particular property. In this case
Firstly we need to create new Interface called for example SomeFeignClient
public interface SomeFeignClient {
Entity getEntity(String id);
}
Secondly create 2 Feign clients that will extend our interface and mark them using @ConditionalOnProperty
annotation
@ConditionalOnProperty(prefix="feign.client", name="internal", havingValue="true")
@FeignClient(...your configurations here...)
public interface SomeFeignClientFirst extends SomeFeignClient {
@Override
Entity getEntity(String id);
}
@ConditionalOnProperty(prefix="feign.client", name="internal", havingValue="false")
@FeignClient(...your configurations here...)
public interface SomeFeignClientSecond extends SomeFeignClient {
@Override
Entity getEntity(String id);
}
If you want to add request interceptor for some of this feign client dont forget to mark them using @ConditionalOnBean
annotation
@RequiredArgsConstructor
public class FeignClientConfiguration {
private final FeignProperties properties;
@ConditionalOnBean(SomeFeignClientFirst.class)
@Bean
public RequestInterceptor requestInterceptor() {
return template -> {
template.header(HttpHeaders.AUTHORIZATION, "Token " + properties.getToken());
template.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
template.header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE);
};
}
答案2
得分: 0
-
对于所有与环境相关的属性,请使用Spring配置文件。例如,对于开发环境,您将有特定于开发环境的属性,类似地,对于生产环境、测试环境等也是如此。更多信息:使用Spring配置文件
-
当您使用Feign客户端并且拥有Eureka服务器时,请尝试通过Eureka进行服务发现。仅为您要查找的Feign客户端传递服务名称。如果您的微服务在Eureka中注册了,它将从那里找到,无需硬编码URL。Spring Cloud集成了Ribbon和Eureka,以在使用Feign时提供负载均衡的HTTP客户端。更多信息:Spring Cloud OpenFeign
英文:
-
For all kind of environment related properties, use Spring profiles. Like for DEV env you will have dev specific properties, similarly for prod env, test env etc. More info : Using Spring Profiles
-
When you are using Feign Client, and you have an Eureka Server, try to do the service discovery via Eureka. Pass the service name only for Feign Client that you are looking for. If your Microservice is registered in Eureka, it will find from there, no need for hard-coded URLs. Spring Cloud integrates Ribbon and Eureka to provide a load balanced http client when using Feign. More Info : Spring-Cloud-OpenFeign
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论