spring boot的actuator

actuator官方的介绍

Spring Boot includes a number of additional features to help you monitor and manage your application when it’s pushed to production. You can choose to manage and monitor your application using HTTP endpoints, with JMX or even by remote shell (SSH or Telnet). Auditing, health and metrics gathering can be automatically applied to your application.

actuator是一个强大功能,有助于对应用程序进行监视和管理,通过restful api请求来监管、审计、收集应用的运行情况

1.添加依赖

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

2.启动项目

用浏览器打开 http://localhost:8080/actuator

复制代码
{
    "_links": {
        "self": {
            "href": "http://localhost:8080/actuator",
            "templated": false
        },
        "health": {
            "href": "http://localhost:8080/actuator/health",
            "templated": false
        },
        "health-component": {
            "href": "http://localhost:8080/actuator/health/{component}",
            "templated": true
        },
        "health-component-instance": {
            "href": "http://localhost:8080/actuator/health/{component}/{instance}",
            "templated": true
        },
        "info": {
            "href": "http://localhost:8080/actuator/info",
            "templated": false
        }
    }
}
复制代码

 actuator暴露了三个简单的endpoint

开启所有接口,在application.properties中,添加

management.endpoints.web.exposure.include=*

刷新 http://localhost:8080/actuator页面,会出现很多endpoint

复制代码
{
    "_links": {
        "self": {
            "href": "http://localhost:8080/actuator",
            "templated": false
        },
        "auditevents": {
            "href": "http://localhost:8080/actuator/auditevents",
            "templated": false
        },
        "beans": {
            "href": "http://localhost:8080/actuator/beans",
            "templated": false
        },
        "caches-cache": {
            "href": "http://localhost:8080/actuator/caches/{cache}",
            "templated": true
        },
        "caches": {
            "href": "http://localhost:8080/actuator/caches",
            "templated": false
        },
        "health-component": {
            "href": "http://localhost:8080/actuator/health/{component}",
            "templated": true
        },
        "health-component-instance": {
            "href": "http://localhost:8080/actuator/health/{component}/{instance}",
            "templated": true
        },
        "health": {
            "href": "http://localhost:8080/actuator/health",
            "templated": false
        },
        ……
    }
}
复制代码

(1)/actuator/health 查看应用健康指标

{
    "status": "UP"
}

(2)/actuator/info查看应用的定制信息

  在配置文件中以 info 开头的配置信息

application.properties包含

info.app.name=abc
info.app.version=1.0.0

返回结果

{
    "app": {
        "name": "abc",
        "version": "1.0.0"
    }
}

(3)/actuator/metrics 查看应用基本指标

  返回actuator提供的所有metric的name

  /actuator/metrics/{name}查看具体指标

  使用 /actuator/metrics/jvm.memory.max,查看JVM最大内存

复制代码
{
    "name": "jvm.memory.max",
    "description": "The maximum amount of memory in bytes that can be used for memory management",
    "baseUnit": "bytes",
    "measurements": [
        {
            "statistic": "VALUE",
            "value": 5.583142911E9
        }
    ],
    "availableTags": [
        {
            "tag": "area",
            "values": [
                "heap",
                "nonheap"
            ]
        },
        {
            "tag": "id",
            "values": [
                "Compressed Class Space",
                "PS Survivor Space",
                "PS Old Gen",
                "Metaspace",
                "PS Eden Space",
                "Code Cache"
            ]
        }
    ]
}
复制代码

 (4)/actuator/beans 应用中所有Spring Beans的完整列表

复制代码
    "contexts": {
        "application": {
            "beans": {
                "spring.jpa-org.springframework.boot.autoconfigure.orm.jpa.JpaProperties": {
                    "aliases": [],
                    "scope": "singleton",
                    "type": "org.springframework.boot.autoconfigure.orm.jpa.JpaProperties",
                    "resource": null,
                    "dependencies": []
                },"uploadController": {
                    "aliases": [],
                    "scope": "singleton",
                    "type": "com.example.demo.controller.UploadController$$EnhancerBySpringCGLIB$$b6fa2feb",
                    "resource": "file [E:\\java\\demo\\target\\classes\\com\\example\\demo\\controller\\UploadController.class]",
                    "dependencies": []
                },
                ……
            },
            "parentId": null
        }
    }
}
复制代码

(5)/actuator/heapdump  dump 一份应用的 JVM 堆信息

  可以使用 JDK 自带的 Jvm 监控工具 VisualVM 打开该文件查看内存快照

 

 

(6)/actuator/scheduledtasks  应用中的定时任务信息

(7)/actuator/env 获取全部环境属性

  /actuator/env/{name} 获取特定的环境属性值

  /actuator/env/java.version  java版本

复制代码
{
    "property": {
        "source": "systemProperties",
        "value": "1.8.0_151"
    },
    "activeProfiles": [
        "dev"
    ],
    "propertySources": [
        {
            "name": "server.ports"
        },
        {
            "name": "servletConfigInitParams"
        },
        {
            "name": "servletContextInitParams"
        },
        {
            "name": "systemProperties",
            "property": {
                "value": "1.8.0_151"
            }
        },
        {
            "name": "systemEnvironment"
        },
        {
            "name": "random"
        },
        {
            "name": "applicationConfig: [classpath:/application.properties]"
        },
        {
            "name": "Management Server"
        }
    ]
}
复制代码

还有很多,可以去 https://docs.spring.io/spring-boot/docs/2.0.2.RELEASE/actuator-api/html/ 查看

 

posted @   慕尘  阅读(518)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
历史上的今天:
2016-09-18 如何用命令检查Linux服务器性能
2016-09-18 sphinx应用
点击右上角即可分享
微信分享提示