Java 如何根据属性文件更改外部客户端url?
我有一个假客户端,它向给定的url发送请求Java 如何根据属性文件更改外部客户端url?,java,spring,spring-boot,Java,Spring,Spring Boot,我有一个假客户端,它向给定的url发送请求 @FeignClient( name = "feign-client-name", url = "${feign.client.url}", configuration = FeignClientConfiguration.class) public interface SomeFeignClient { @GetMapping(SOME_GEP_MAPP
@FeignClient(
name = "feign-client-name",
url = "${feign.client.url}",
configuration = FeignClientConfiguration.class)
public interface SomeFeignClient {
@GetMapping(SOME_GEP_MAPPING_PATH)
Entity getEntity(String id);
}
如何更改外部客户端的url取决于internal
属性?我希望它以以下方式工作: 如果
internal
属性在上具有值,则外部客户端应使用内部url
值,在其他情况下应使用url
值
UPD:
可能的解决方案-使用弹簧轮廓
对于所有类型的与环境相关的属性,请使用Spring概要文件。与DEV env一样,您将拥有特定于DEV的属性,prod env、test env等的属性也是如此。更多信息:
如果您使用的是外国客户机,并且您有一台Eureka服务器,请尝试通过Eureka进行服务发现。仅为您正在查找的外部客户端传递服务名称。如果您的微服务在Eureka中注册,它将从那里发现,不需要硬编码URL。SpringCloud将Ribbon和Eureka集成在一起,以便在使用Feign时提供负载平衡的http客户端。更多信息:
我找到了没有Spring配置文件的解决方案。
此解决方案的主要思想是使用@conditionalnproperty
注释,防止bean创建依赖于特定属性。在这种情况下
首先,我们需要创建新的接口,例如SomeFeignClient
public interface SomeFeignClient {
Entity getEntity(String id);
}
其次,创建两个外部客户机,它们将扩展我们的界面,并使用@ConditionalOnProperty
注释对它们进行标记
@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);
}
@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);
};
}
如果你想为这个外国客户端添加请求拦截器,别忘了用@ConditionalOnBean
注释来标记它们
@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);
}
@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);
};
}