我正在尝试通过千分尺和Prometheus监视我的Spring Boot应用程序的其余模板指标。当我使用由ResttemplateBuilder构建的Resttemplate并用于调用另一个api时,它确实获得了预期的http.client.requests指标。但是对于AsyncResttemplate,当我使用AsyncResttemplate创建并使用它调用另一个api时,它没有提供任何http.client.requests指标。
这是我创建AsyncResttemplate bean时的代码
@Bean
public AsyncRestTemplate asyncRestTemplate(){
return new AsyncRestTemplate();
}
这是我异步调用另一个api时的代码
public ListenableFuture async() {
ListenableFuture<ResponseEntity<AccountResponse>> accountResponseList = asyncRestTemplate.exchange(accountUrl, HttpMethod.GET, new HttpEntity<>(new HttpHeaders()), AccountResponse.class);
accountResponseList.addCallback(new ListenableFutureCallback<ResponseEntity<AccountResponse>>() {
@Override
public void onSuccess(ResponseEntity<AccountResponse> accountResponseResponseEntity) {
System.out.println("Success");
}
@Override
public void onFailure(Throwable throwable) {
System.out.println("Failure");
}
});
return accountResponseList;
}
这些是在pom.xml中导入的相关依赖项
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
这是我期望从微米-普罗米修斯量度中得到的
# HELP http_client_requests_seconds Timer of RestTemplate operation
# TYPE http_client_requests_seconds summary
http_client_requests_seconds_count{application="${spring.application.name}",clientName="localhost",method="GET",status="200",uri="/getAllAccount",} 1.0
http_client_requests_seconds_sum{application="${spring.application.name}",clientName="localhost",method="GET",status="200",uri="/getAllAccount",} 0.0242929
# HELP http_client_requests_seconds_max Timer of RestTemplate operation
# TYPE http_client_requests_seconds_max gauge
http_client_requests_seconds_max{application="${spring.application.name}",clientName="localhost",method="GET",status="200",uri="/getAllAccount",} 0.0242929
以上指标来自ResttemplateBuilder,是否也可以通过AsyncRestTemplate获得这些指标?
[Update:从M. Deinum的建议中,我将bean更改为
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder){
return builder.setConnectTimeout(Duration.ofSeconds(500)).build();
}
@Bean
public AsyncRestTemplate asyncRestTemplate(RestTemplate restTemplate){
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
SimpleAsyncTaskExecutor asyncTaskExecutor = new SimpleAsyncTaskExecutor();
asyncTaskExecutor.setConcurrencyLimit(10);
requestFactory.setTaskExecutor(asyncTaskExecutor);
return new AsyncRestTemplate(requestFactory, restTemplate);
}
但仍然无法从异步调用中获取任何http.client.requests
类似的事情应该做。
@Bean
public AsyncRestTemplate asyncRestTemplate(RestTemplateBuilder builder){
RestTemplate rest = builder.build();
AsyncClientHttpRequestFactory requestFactory = (AsyncClientHttpRequestFactory) rest.getRequestFactory();
return new AsyncRestTemplate(requestFactory, rest);
}
这将重用现有配置。 AsyncRestTemplate
只是将RestTemplate
与`TaskExecutor结合起来的包装。
NOTE:请注意,自Spring 5起,AsyncRestTemplate
已被弃用,并可能在不久的将来被删除!