是否有任何方法来测量restTemplate在SpringBoot中的响应时间。
我用这种方式调用API,一个同步rest。是否也有测量异步调用的方法?还是只打同步电话?
public void execute(Request request) {
this.restTemplate.postForObject(System.getenv("URL"), request, String.class);
}我可以使用另一个框架进行rest调用,不需要是restTemplate,我只想使用SpringBoot调用外部API,并查看响应所需的时间。
谢谢!
发布于 2022-07-22 21:55:47
无论如何,
是用来测量restTemplate在SpringBoot中的响应时间的。
是的,做一个开始和一个完成时间戳,并测量差异,例如
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执行器免费获得传出请求度量http.client.requests,例如使用Prometheus作为监控系统:
http_client_requests_seconds_max{clientName="stackoverflow.com",method="GET",outcome="SUCCESS",status="200",uri="/",} 0.398269736
也有测量异步调用的方法吗?
是的,创建一个开始和一个finish时间戳,并测量异步调用中回调内部的差异。使用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。
https://stackoverflow.com/questions/73058684
复制相似问题