SpringBoot 2.x (15):Actuator监控

时间:2022-12-30 23:15:50

Actuator监控:SpringBoot自带的,对生成环境进行监控的系统

使用:既然是监控,那就不能监控一个空项目

这里我使用SpringBoot整合MyBatis的Demo:

https://www.cnblogs.com/xuyiqing/p/10837299.html

依赖

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

然后直接启动项目即可

 

访问http://localhost:8080/actuator

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

显示可监控列表

 

访问http://localhost:8080/actuator/health

{"status":"UP"}

说明状态正常

 

进一步地,使用Actuator获取地JSON可以写前端页面进行显示,或者结合定时任务

比如定时访问http://localhost:8080/actuator/health,如果返回UP说明正常,然后进行其他操作

 

问题解决:访问http://localhost:8080/actuator/health显示404

原因:SpringBoot版本问题,SpringBoot1.x访问http://localhost:8080/health

 

其实这里获得的是较少的信息,更多的信息需要进行配置:

management.endpoints.web.exposure.include=*

 

现在访问:http://localhost:8080/actuator

{
    "_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": {
            "href": "http://localhost:8080/actuator/health",
            "templated": false
        },
        "health-component-instance": {
            "href": "http://localhost:8080/actuator/health/{component}/{instance}",
            "templated": true
        },
        "health-component": {
            "href": "http://localhost:8080/actuator/health/{component}",
            "templated": true
        },
        "conditions": {
            "href": "http://localhost:8080/actuator/conditions",
            "templated": false
        },
        "configprops": {
            "href": "http://localhost:8080/actuator/configprops",
            "templated": false
        },
        "env": {
            "href": "http://localhost:8080/actuator/env",
            "templated": false
        },
        "env-toMatch": {
            "href": "http://localhost:8080/actuator/env/{toMatch}",
            "templated": true
        },
        "info": {
            "href": "http://localhost:8080/actuator/info",
            "templated": false
        },
        "loggers": {
            "href": "http://localhost:8080/actuator/loggers",
            "templated": false
        },
        "loggers-name": {
            "href": "http://localhost:8080/actuator/loggers/{name}",
            "templated": true
        },
        "heapdump": {
            "href": "http://localhost:8080/actuator/heapdump",
            "templated": false
        },
        "threaddump": {
            "href": "http://localhost:8080/actuator/threaddump",
            "templated": false
        },
        "metrics-requiredMetricName": {
            "href": "http://localhost:8080/actuator/metrics/{requiredMetricName}",
            "templated": true
        },
        "metrics": {
            "href": "http://localhost:8080/actuator/metrics",
            "templated": false
        },
        "scheduledtasks": {
            "href": "http://localhost:8080/actuator/scheduledtasks",
            "templated": false
        },
        "httptrace": {
            "href": "http://localhost:8080/actuator/httptrace",
            "templated": false
        },
        "mappings": {
            "href": "http://localhost:8080/actuator/mappings",
            "templated": false
        }
    }
}

 

访问:http://localhost:8080/actuator/env

获得配置信息

 

访问:http://localhost:8080/actuator/beans

监控项目Spring中的beans

 

访问:http://localhost:8080/actuator/metrics

查看应用基本指标列表

 

通常不建议全部开启,因为不安全

所以,可以这样配置

#开启全部
management.endpoints.web.exposure.include=*
#开启某个
management.endpoints.web.exposure.include=metrics
#关闭某个
management.endpoints.web.exposure.exclude=metrics

 

SpringBoot的学习暂时结束了:

总结和未来规划:

学会了基本的配置、整合Mybatis、Redis、ActiveMQ、ElasticSearch等

技术栈的规划:

初级路线:Java基础->Java Webhtml+css+js,servlet,request+response,ajax,mysql+jdbc...)->SSM、SSH->SpringBoot->Spring Cloud

高级路线:Linux->源码阅读(Spring、JDK)->数据库优化(分库分表,慢查询,调优...)->缓存(Redis集群...)->HTTP/HTTPS协议优化

                 ->安全(XSS、DDOS、CSRF)->消息队列(ActiveMQ,Kafka,RocketMQ)->搜索框架(Lucene、Solr、ElasticSearch)->Docker、K8S

 

下面开始Spring Cloud的学习!