ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java-SpringBoot中Rest Api的计数器指标

2019-11-10 16:00:56  阅读:609  来源: 互联网

标签:spring-boot rest performancecounter spring java


最好的方法是计算一次在春季启动中API响应其成功/失败响应被触发的次数.

我的想法是,在应用程序启动和调用api时,使用post构造启动一个新线程,然后对每个新的唯一请求使用计数器服务,以计算该特定请求触发了多少个api以及其中有多少成功或失败.

如果你们有什么建议,推荐一些新方法.

解决方法:

您不必从头开始创建,只需使用内置了此功能的Spring Boot Actuator.只需将以下依赖项添加到您的项目中:

pom.xml:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

执行器暴露出例如/metrics端点,您可以在此处找到所有响应的计数器-每个计数器均以counter.status.{{status_code}}.{{endpoint}}开头.

例如,我有一个带有/ hello端点的简单演示应用程序.每当我将此端点度量标准称为counter.status.200.hello都会增加.当我访问/ metrics端点时,我会看到类似以下内容:

{
  "mem": 586013,
  "mem.free": 324496,
  "processors": 8,
  "instance.uptime": 144496,
  "uptime": 149950,
  "systemload.average": 0.52,
  "heap.committed": 522240,
  "heap.init": 251904,
  "heap.used": 197743,
  "heap": 3580928,
  "nonheap.committed": 64960,
  "nonheap.init": 2496,
  "nonheap.used": 63773,
  "nonheap": 0,
  "threads.peak": 42,
  "threads.daemon": 25,
  "threads.totalStarted": 54,
  "threads": 27,
  "classes": 8938,
  "classes.loaded": 8938,
  "classes.unloaded": 0,
  "gc.ps_scavenge.count": 8,
  "gc.ps_scavenge.time": 54,
  "gc.ps_marksweep.count": 2,
  "gc.ps_marksweep.time": 65,
  "gauge.response.star-star.favicon.ico": 1,
  "counter.status.200.star-star": 2,
  "counter.status.304.star-star": 2,
  "counter.status.200.metrics.name": 2,
  "counter.status.200.star-star.favicon.ico": 5,
  "gauge.response.star-star": 3,
  "gauge.response.hello": 5,
  "gauge.response.metrics.name": 3,
  "counter.status.200.hello": 2,
  "counter.status.404.metrics.name": 1,
  "httpsessions.max": -1,
  "httpsessions.active": 0,
  "datasource.primary.active": 0,
  "datasource.primary.usage": 0
}

我还可以访问单个指标,例如/metrics/counter.status.200.hello,仅返回:

{
  "counter.status.200.hello": 2
}

默认情况下,/ metrics端点是安全的.对于您的本地开发人员,您可以添加以下内容:

management:
  security:
    enabled: false

到您的application.yml或

management.security.enabled=false

如果您使用application.properties的话.

标签:spring-boot,rest,performancecounter,spring,java
来源: https://codeday.me/bug/20191110/2013678.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有