简体   繁体   English

使用 SimpleClientHttpRequestFactory 时,如何使自定义 RestTemplate 使用标准 Spring Boots 的 Prometheus RestTemplate 指标进行检测?

[英]How to make custom RestTemplate to instrument with standard Spring Boots' Prometheus RestTemplate metrics when using SimpleClientHttpRequestFactory?

According to the docs , the standard http client metrics are grabbed if the RestTemplate is created using RestTemplateBuilder and then injected.根据docs ,如果RestTemplate是使用RestTemplateBuilder创建然后注入的, RestTemplate标准的 http 客户端指标。 However, we have a custom RestTemplate bean initialization that uses no RestTeplateBuilder, but instead RestTemplate is initiated with new and the SimpleClientHttpRequestFactory is passed as the parameter.但是,我们有不使用RestTeplateBuilder定制RestTemplate豆初始化,而是RestTemplate与新的开始和SimpleClientHttpRequestFactory被作为参数传递。

@Bean(name = "RestTemplateWithoutTimeOut")
    public RestTemplate restTemplateWithoutTimeOut() {
        SimpleClientHttpRequestFactory simpleClientHttpRequestFactory = new SimpleClientHttpRequestFactory();
        int timeout = 5 * 60 * 1000;
        simpleClientHttpRequestFactory.setConnectTimeout(timeout);
        simpleClientHttpRequestFactory.setReadTimeout(timeout);
        RestTemplate restTemplate = new RestTemplate(simpleClientHttpRequestFactory);
        restTemplate.setErrorHandler(new ErrorHandler());
        List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();
        interceptors.add(new LoggingInterceptor());
        interceptors.add(new SecureInterceptor());
        return restTemplate;
    }

How can we inject metrics collector (the same metrics as are collected by default) for that kind of RestTemplate initialization?我们如何为这种RestTemplate初始化注入指标收集器(与默认收集的指标相同)?

The RestTemplateBuilder exposes methods for customizing essentially every knob that's available on RestTemplate . RestTemplateBuilder公开了用于自定义RestTemplate上可用的基本上每个旋钮的RestTemplate You can just call requestFactory and pass your customized factory, then build a RestTemplate instance that has all of the usual instrumentation.您只需调用requestFactory并传递您的自定义工厂,然后构建一个具有所有常用工具的RestTemplate实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Spring 中的 restTemplate 使用请求正文发出 GET 请求 - How to make a GET request with request body using restTemplate in Spring 使用spring-cloud-config-client时如何配置自定义RestTemplate? - How to configure a custom RestTemplate when using spring-cloud-config-client? 如何将Spring Cache Redis与自定义RestTemplate一起使用? - How to use Spring Cache Redis with a custom RestTemplate? 使用 Spring RestTemplate 时如何避免 [] 的双重编码? - How to avoid double-encoding of [] when using Spring RestTemplate? SimpleClientHttpRequestFactory 与 HttpComponentsClientHttpRequestFactory 的 Http 请求超时与 RestTemplate? - SimpleClientHttpRequestFactory vs HttpComponentsClientHttpRequestFactory for Http Request timeout with RestTemplate? 将RestTemplate与Spring for Android结合使用时如何在Abstract类中设置属性 - How to set a property in Abstract class when using RestTemplate with Spring for Android 使用Spring Oauth2RestTemplate时如何切换到服务用户 - How to switch to a service user when using Spring Oauth2RestTemplate 在Java中使用Spring RestTemplate时发生NullPointedException - NullPointedException when using Spring RestTemplate in Java 使用Spring时获取HttpMessageNotReadableException异常-RestTemplate - Get HttpMessageNotReadableException Exception when using Spring - RestTemplate 如何在Resttemplate Spring中做 - How to do in Resttemplate Spring
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM