我试图通过千分尺和普罗米修斯监控我的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获得这些指标?

更新:根据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组合在一起的包装。

注意:请注意,自Spring 5起,AsyncRestTemplate已被弃用,并可能在不久的将来被删除!

10-08 06:03