![](/img/trans.png)
[英]How to de-serialize a raw array of arrays from REST response using Springboot RestTemplate
[英]SpringBoot Response Time using RestTemplate
无论如何测量SpringBoot中restTemplate的响应时间。
我以这种方式调用 API,同步休息。 无论如何也要测量异步调用吗? 还是只是同步通话?
public void execute(Request request) {
this.restTemplate.postForObject(System.getenv("URL"), request, String.class);
}
我可以使用另一个框架来做其余的调用,不需要是restTemplate,我只想使用SpringBoot调用一个外部API,看看需要多长时间才能响应。
谢谢!
无论如何测量SpringBoot中restTemplate的响应时间。
是的,制作开始和结束时间戳并测量差异,例如
var start = Instant.now();
this.restTemplate.postForObject(System.getenv("URL"), request, String.class);
long duration = Duration.between(start, Instant.now()).toMillis();
但是,如果您想跟踪传出请求的响应时间,我会选择指标作为我首先查看的地方。
如果您像下面这样注册一个RestTemplate
@Bean
public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder) {
return restTemplateBuilder.build();
}
您将使用 Spring Boot 执行器免费获得传出请求指标http.client.requests
,例如使用 Prometheus 作为监控系统:
http_client_requests_seconds_max{clientName="stackoverflow.com",method="GET",outcome="SUCCESS",status="200",uri="/",} 0.398269736
无论如何也要测量异步调用吗?
是的,创建一个开始和结束时间戳,并测量异步调用的回调内部的差异。 使用HttpClient
和CompletableFuture
的示例:
var start = Instant.now();
var completableFuture = HttpClient.newBuilder()
.build()
.sendAsync(request, HttpResponse.BodyHandlers.ofString())
.thenAccept(response -> {
long duration = Duration.between(start, Instant.now()).toMillis();
log.info("{} {} took {} ms", request.method(), request.uri(), duration);
});
completableFuture.join();
给出GET https://stackoverflow.com/ took 395 ms
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.