[英]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.