springboot 实时监控 spring-boot-starter-actuator 包

对java工程实时监控方式很多,本文主要讲在springboot框架中的监控。
springboot框架,自带了actuator监控,在pom中引入jar包即可,如下

1.引入jar
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

在2.0版本之后改动很大,我这里是用的2.0.2

2.启动项目后即可访问
http://localhost:8081/webproject/actuator/health
http://localhost:8081/webproject/actuator/info

如果想让url个性化一点,在 application.propertie
配置文件中 加入
management.endpoints.web.base-path=/jiankong
那么就可以访问
http://localhost:8081/webproject/jiankong/health

3.actuator 提供了很多api(称为:节点)
默认只开放了 health、info两个节点
如果需要公开所有 则在配置文件中继续加入
management.endpoints.web.exposure.include=*
其中 health方法 是不显示具体的内容的,如需要
则继续加入配置
management.endpoint.health.show-details=always

具体方法和返回内容 可以参考API
GET /autoconfig 查看自动配置的使用情况 true 
GET /configprops 查看配置属性,包括默认配置 true 
GET /beans 查看bean及其关系列表 true 
GET /dump 打印线程栈 true 
GET /env 查看所有环境变量 true 
GET /env/{name} 查看具体变量值 true 
GET /health 查看应用健康指标 false 
GET /info 查看应用信息 false 
GET /mappings 查看所有url映射 true 
GET /metrics 查看应用基本指标 true 
GET /metrics/{name} 查看具体指标 true 
POST /shutdown 关闭应用 true 
GET /trace 查看基本追踪信息 true 

4. 常用的查看具体指标的方法  /metrics/{name}
http://localhost:8081/webproject/actuator/metrics/http.server.requests
http://localhost:8081/webproject/actuator/metrics/jvm.memory.used
http://localhost:8081/webproject/actuator/metrics/jvm.threads.peak


等等,具体name 可以通过/metrics 获得,
其中 http.server.requests 是对所有请求的url的监控,次数,时间 等。
那么如果我们需要针对个别重点controller分别监控呢~~

5.分别监控,需要在controller上做些手脚
在RequestMapping 上面 加上注解 @Timed(value = "list.base", longTask = true)
value可以就用mapping 这样。
这个@Timed 注解 是 micrometer-core-1.0.3-sources.jar的 springboot监控本身用到的。
当访问过这个controller之后 就可以调用~ 注意还没有收到过请求的话是请求不到这个list.base的
http://localhost:8081/webproject/actuator/metrics/list.base

{
    "availableTags":[
        {
            "tag":"exception",
            "values":["None"]
        },
        {
            "tag":"method",
            "values":["POST"]
        },
        {
            "tag":"uri",
            "values":["/ListDataBase","root"]
        },
        {
            "tag":"status",
            "values":["200"]
        }
    ],
    "measurements":[
        {
            "statistic":"ACTIVE_TASKS",
            "value":0
        },
        {
            "statistic":"DURATION",
            "value":0
        },
        {
            "statistic":"COUNT",
            "value":1
        },
        {
            "statistic":"TOTAL_TIME",
            "value":0.306317164
        },
        {
            "statistic":"MAX",
            "value":0.306317164
        }
    ],
    "name":"list.base"
}

如果有兴趣研究更多。。。官网api
https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#production-ready-endpoints

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
spring-boot-starter-actuator是Spring Boot框架中的一个模块,它提供了一系列用于监控和管理应用程序的端点(endpoints),比如/health、/info、/metrics等。这些端点可以通过HTTP请求访问,返回应用程序的各种指标和状态信息。 spring-boot-starter-actuator的原理主要括以下几个方面: 1. 自动配置:Spring Boot框架提供了自动配置功能,可以根据应用程序的依赖项和配置文件来自动配置spring-boot-starter-actuator模块。 2. 端点映射:spring-boot-starter-actuator使用Spring MVC框架来处理HTTP请求。它通过端点映射(Endpoint Mapping)将HTTP请求映射到相应的端点处理器(Endpoint Handler)上。 3. 端点处理器:每个端点都有一个对应的处理器,用于处理HTTP请求并返回响应。端点处理器可以是自定义的Java类,也可以是Spring Boot框架提供的默认实现。 4. 数据源:spring-boot-starter-actuator会从应用程序的各种数据源中收集指标和状态信息,比如JVM内存使用情况、数据库连接池状态等。这些数据源可以是应用程序本身、第三方库、操作系统等。 5. 安全性:为了保护应用程序的安全性,spring-boot-starter-actuator提供了一些安全功能,比如基于角色的访问控制、IP地址过滤等。可以通过配置文件来配置这些安全功能。 总之,spring-boot-starter-actuator通过自动配置、端点映射、端点处理器、数据源和安全性等机制,实现了对应用程序的监控和管理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值