4

我正在尝试通过千分尺和普罗米修斯监控我的 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>

这是我期望从 micrometer-prometheus 指标中得到的

# 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 获得这些指标?

更新:根据 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

4

1 回答 1

2

这样的事情应该做。

@Bean
public AsyncRestTemplate asyncRestTemplate(RestTemplateBuilder builder){
  RestTemplate rest = builder.build();
  AsyncClientHttpRequestFactory requestFactory = (AsyncClientHttpRequestFactory) rest.getRequestFactory();
  return new AsyncRestTemplate(requestFactory, rest);
}

这将重用现有配置。这AsyncRestTemplate只是RestTemplate将它与 `TaskExecutor.

注意:请注意,自AsyncRestTemplateSpring 5 起已弃用,可能会在不久的将来某处被删除!

于 2019-10-30T08:41:35.143 回答