我有三个微服务,它们都需要使用一个名为“common”的库。我需要在共享库中添加新的Spring配置文件。问题是微服务A不关心新代码,也不想被强制添加配置以便运行应用程序。我需要一种方法,只为微服务B和C编写程序来加载配置。
共享库中的新配置:
@Configuration
public class HttpConnectionConfiguration {
@Value("${http.connect.timeout}")
private int httpConnectTimeout;
@Value("${http.connect.request.timeout}")
private int httpConnectRequestTimeout;
@Bean
public RestTemplate restTemplate() {
RestTemplate restTemplate = new RestTemplate();
restTemplate.setRequestFactory(getClientHttpRequestFactory());
return restTemplate;
}
private ClientHttpRequestFactory getClientHttpRequestFactory() {
RequestConfig config = RequestConfig.custom()
.setConnectTimeout(httpConnectTimeout)
.setConnectionRequestTimeout(httpConnectRequestTimeout)
.build();
CloseableHttpClient client = HttpClientBuilder
.create()
.useSystemProperties()
.setDefaultRequestConfig(config)
.build();
return new HttpComponentsClientHttpRequestFactory(client);
}
}
应用程序.yaml文件中的微服务B和C的配置:
http:
connect:
timeout: 5000
request:
timeout: 5000
微服务 B 和 C 启动正常,但 A 报错如下:Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'http.connect.timeout' in value "${http.connect.timeout}
在不提供假值的情况下,最好的解决方法是什么?
Bean method 'restTemplate' in 'HttpConnectionConfiguration' not loaded because @ConditionalOnProperty (http.connect.timeout) did not find property 'http.connect.timeout'
。 - maloney