加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

java – 测量Spring RestTemplate HTTP请求时间

发布时间:2020-12-15 01:49:21 所属栏目:大数据 来源:网络整理
导读:我想测量RestTemplate.getForObject调用的HTTP GET请求的时间,而不需要解析响应所需的时间.所以只是远程HTTP调用所需的时间.我已经尝试过设置ClientHttpRequestInterceptor,但我不认为这是正确的方法,因为时间似乎是错误的: public class PerfRequestSyncIn

我想测量RestTemplate.getForObject调用的HTTP GET请求的时间,而不需要解析响应所需的时间.所以只是远程HTTP调用所需的时间.我已经尝试过设置ClientHttpRequestInterceptor,但我不认为这是正确的方法,因为时间似乎是错误的:

public class PerfRequestSyncInterceptor implements ClientHttpRequestInterceptor {
private Logger log = Logger.getLogger(this.getClass());

@Override
  public ClientHttpResponse intercept(HttpRequest request,byte[] body,ClientHttpRequestExecution execution) throws IOException {

    long start = System.nanoTime();
    ClientHttpResponse resp = execution.execute(request,body);

    log.debug("remote request time: "
            + ((System.nanoTime() - start) * Math.pow(10,-9)));
    return resp;
  }
}

呼叫:

RestTemplate rest = new RestTemplate();
List

如何衡量RestTemplate HTTP请求的时间?

最佳答案
您可以使用AOP和Spring内置的PerformanceMonitorInterceptor.您需要正确定义要拦截哪种calss的方法,然后才能进行测量.您可以配置如下:


(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读