Spring获取Http响应时间和状态原理
2017-07-31 11:55
1481 查看
spring-actuate是spring的监控模块,默认开启很监控下,其中就有个http的响应时间和状态。
在maven的pom中引入
启动项目,访问 http://localhost:prot/metrics 可以看到的结果中
“counter.status.200.metrics”:24 表示/metrics接口响应200状态24次
“gauge.response.metrics”:2.0 表示/metrics接口响应时间为2ms
之后通过字符串替换,将key改成自己想要的格式
最后,处理时长会存为Gauge类型,响应状态次数会存为Counter类型,Gauge存在实现了GaugeService接口的类中,Counter存在实现了CounterService接口的类中。这些类会在MetricsFilter实例化的时候传入
http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#production-ready 官方文档
在maven的pom中引入
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> <version>1.5.3.RELEASE</version> </dependency>
启动项目,访问 http://localhost:prot/metrics 可以看到的结果中
“counter.status.200.metrics”:24 表示/metrics接口响应200状态24次
“gauge.response.metrics”:2.0 表示/metrics接口响应时间为2ms
之后通过字符串替换,将key改成自己想要的格式
那么actuate又是如何获取到接口的响应时间和状态?
在sprint-actuate里有个MetricsFilter类,继承了OncePerRequestFilter,从名字上可以看出,这个拦截器会在每次请求执行一次,在请求处理前记录开始时间,请求处理后记录结束时间和响应状态final class MetricsFilter extends OncePerRequestFilter { protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException { StopWatch stopWatch = this.createStopWatchIfNecessary(request); String path = (new UrlPathHelper()).getPathWithinApplication(request); int status = HttpStatus.INTERNAL_SERVER_ERROR.value(); try { chain.doFilter(request, response); status = this.getStatus(response); } finally { if (!request.isAsyncStarted()) { if (response.isCommitted()) { status = this.getStatus(response); } stopWatch.stop(); request.removeAttribute(ATTRIBUTE_STOP_WATCH); this.recordMetrics(request, path, status, stopWatch.getTotalTimeMillis()); } } } ... }
最后,处理时长会存为Gauge类型,响应状态次数会存为Counter类型,Gauge存在实现了GaugeService接口的类中,Counter存在实现了CounterService接口的类中。这些类会在MetricsFilter实例化的时候传入
MetricsFilter(CounterService counterService, GaugeService gaugeService, MetricFilterProperties properties) { this.counterService = counterService; this.gaugeService = gaugeService; this.properties = properties; }
http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#production-ready 官方文档
相关文章推荐
- Python使用pycurl获取http的响应时间
- 获取http响应时间,pycurl,python
- 在线HTTP速度测试(响应时间测试)及浏览器兼容测试
- Jmeter Beanshell 获取响应信息头部时间,更改时间格式用于JDBC查询条件
- Spring中配置获取HttpServletRequest的环境
- 获取站点的各类响应时间
- [置顶] 上一篇jmeter 获取响应时间的优化篇,增加JDBC返回查询以及响应断言
- 自动获取页面当前日期时间 http://www.datejs.com/
- 响应http报文中的Date属性与cookie过期时间的关系
- 获取HTTP 头中响应请求文件的大小
- httppp 1.4.0 发布,HTTP响应时间监控
- HTTP响应时间监控 - httppp
- 使用spring-boot-starter-actuator http://localhost:8080/info无法获取到相应信息
- C#如何用WebClient动态提交文件至Web服务器和设定Http响应超时时间
- cocos2dx通过http获取网络时间校对
- http(17):请求头响应头键值对含义、设置请求头数据,获取请求头数据,获取响应头数据
- 使用HttpURLConnection开发自动发送Get、Post请求并获取响应
- curl获取响应时间
- Spring架构获取事务执行时间
- 【小技巧】spring-mvc中其他获取HttpServletRequest request方法