Spring Boot 技术探索

Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can "just run".

36、Spring Boot之Actuator监控

平台环境:

名称

版本号

Mac OS X

10.15.2

JDK

1.8.0_201

Apache Maven

3.6.0

IntelliJ IDEA

2019.3 (Ultimate Edition)

Spring Boot

2.2.2.RELEASE

 

  什么是Actuator监控?

  Actuator是负责监控应用的各项静态和动态变量的Spring Boot组件。

  如何使用?

  使用只需要引入spring-boot-starter-actuator一个依赖就够了。引入web与thymeleaf的目的是为了制作一个页面,列出所有的健康检查项目。

 

DEMO

pom.xml

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


    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

 

application.properties

# 项目版本信息
info.app.name=springBootDemo36
info.app.version=0.0.1-SNAPSHOT
info.app.test=test


# 打开所有监控项目
management.endpoints.web.exposure.include=*


# 只打开部分监控项目
#management.endpoints.web.exposure.exclude=beans,trace


# 修改默认监控项目为
#management.endpoints.web.base-path=/manage


# 显示详细的健康检查信息
management.endpoint.health.show-details=always


# 关闭特定的健康检查指标
#management.health.redise.enabled=false


# 启用"停止Spring Boot应用"的功能
management.endpoint.shutdown.enabled=true

 

启动工程,可以打开浏览器访问试一下。

http://127.0.0.1:8080/actuator/

{"_links":{"self":{"href":"http://127.0.0.1:8080/actuator","templated":false},"beans":{"href":"http://127.0.0.1:8080/actuator/beans","templated":false},"caches-cache":{"href":"http://127.0.0.1:8080/actuator/caches/{cache}","templated":true},"caches":{"href":"http://127.0.0.1:8080/actuator/caches","templated":false},"health-path":{"href":"http://127.0.0.1:8080/actuator/health/{*path}","templated":true},"health":{"href":"http://127.0.0.1:8080/actuator/health","templated":false},"info":{"href":"http://127.0.0.1:8080/actuator/info","templated":false},"conditions":{"href":"http://127.0.0.1:8080/actuator/conditions","templated":false},"shutdown":{"href":"http://127.0.0.1:8080/actuator/shutdown","templated":false},"configprops":{"href":"http://127.0.0.1:8080/actuator/configprops","templated":false},"env":{"href":"http://127.0.0.1:8080/actuator/env","templated":false},"env-toMatch":{"href":"http://127.0.0.1:8080/actuator/env/{toMatch}","templated":true},"loggers-name":{"href":"http://127.0.0.1:8080/actuator/loggers/{name}","templated":true},"loggers":{"href":"http://127.0.0.1:8080/actuator/loggers","templated":false},"heapdump":{"href":"http://127.0.0.1:8080/actuator/heapdump","templated":false},"threaddump":{"href":"http://127.0.0.1:8080/actuator/threaddump","templated":false},"metrics":{"href":"http://127.0.0.1:8080/actuator/metrics","templated":false},"metrics-requiredMetricName":{"href":"http://127.0.0.1:8080/actuator/metrics/{requiredMetricName}","templated":true},"scheduledtasks":{"href":"http://127.0.0.1:8080/actuator/scheduledtasks","templated":false},"mappings":{"href":"http://127.0.0.1:8080/actuator/mappings","templated":false}}}

 

创建Thymeleaf文件

路径:src/main/resources/templates/

index.html

<!DOCTYPE html>
<html lang="zh-cn" xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/web/thymeleaf/layout">
<head>
    <meta charset="UTF-8">
    <title>Spring Boot Actuator</title>
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="/bootstrap/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="/bootstrap/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="/bootstrap/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
    <script src="/bootstrap/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</head>
<body>
    <p>Endpoints清单</p>
    <table class="table table-sm">
        <thead>
            <tr>
                <th>方法</th>
                <th>路径</th>
                <th>描述</th>
                <th>查看路径</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>GET</td>
                <td>/auditevents</td>
                <td>显示应用暴露的审计事件(如认证进入、订单失败)</td>
                <td><a href="/actuator/auditevents" target="_blank">/actuator/auditevents</a></td>
            </tr>
            <tr>
                <td>GET</td>
                <td>/beans</td>
                <td>描述应用程序上下文里全部的 Bean 以及它们的关系</td>
                <td><a href="/actuator/beans" target="_blank">/actuator/beans</a></td>
            </tr>
            <tr>
                <td>GET</td>
                <td>/conditions</td>
                <td>就是1.0的/autoconfig,提供一份自动配置生效的条件情况,记录哪些自动配置条件通过了,哪些没通过</td>
                <td><a href="/actuator/conditions" target="_blank">/actuator/conditions</a></td>
            </tr>
            <tr>
                <td>GET</td>
                <td>/configprops</td>
                <td>描述配置属性(包含默认值)如何注入 Bean</td>
                <td><a href="/actuator/configprops" target="_blank">/actuator/configprops</a></td>
            </tr>
            <tr>
                <td>GET</td>
                <td>/env</td>
                <td>获取全部环境属性</td>
                <td><a href="/actuator/env" target="_blank">/actuator/env</a></td>
            </tr>
            <tr>
                <td>GET</td>
                <td>/env/{name}</td>
                <td>根据名称获取特定的环境属性值</td>
                <td><a href="/actuator/env/{name}" target="_blank">/actuator/env/java.vm.version</a></td>
            </tr>
            <tr>
                <td>GET</td>
                <td>/flyway</td>
                <td>提供一份 Flyway 数据库迁移信息</td>
                <td><a href="/actuator/flyway" target="_blank">/actuator/flyway</a></td>
            </tr>
            <tr>
                <td>GET</td>
                <td>/health</td>
                <td>报告应用程序的健康指标,这些值由HealthIndicator的实现类提供</td>
                <td><a href="/actuator/health" target="_blank">/actuator/health</a></td>
            </tr>
            <tr>
                <td>GET</td>
                <td>/httptrace</td>
                <td>显示 HTTP 足迹,最近 100 个 HTTP request/repsponse</td>
                <td><a href="/actuator/httptrace" target="_blank">/actuator/httptrace</a></td>
            </tr>
            <tr>
                <td>GET</td>
                <td>/info</td>
                <td>获取应用程序的定制信息,这些信息由 info 打头的属性提供</td>
                <td><a href="/actuator/info" target="_blank">/actuator/info</a></td>
            </tr>
            <tr>
                <td>GET</td>
                <td>/loggers</td>
                <td>显示和修改配置的 loggers</td>
                <td><a href="/actuator/loggers" target="_blank">/actuator/loggers</a></td>
            </tr>
            <tr>
                <td>GET</td>
                <td>/liquidbase</td>
                <td>显示 Liquibase 数据库迁移的纤细信息</td>
                <td><a href="/actuator/liquidbase" target="_blank">/actuator/liquidbase</a></td>
            </tr>
            <tr>
                <td>GET</td>
                <td>/metrics</td>
                <td>报告各种应用程序度量信息,比如内存用量和 HTTP 请求计数</td>
                <td><a href="/actuator/metrics" target="_blank">/actuator/metrics</a></td>
            </tr>
            <tr>
                <td>GET</td>
                <td>/metrics/{name}</td>
                <td>报告指定名称的应用程序度量值</td>
                <td><a href="/actuator/metrics/{name}" target="_blank">/actuator/metrics/mem.free</a></td>
            </tr>
            <tr>
                <td>GET</td>
                <td>/mappings</td>
                <td>描述全部的 URI 路径,以及它们和控制器(包含 Actuator 端点)的映射关系</td>
                <td><a href="/actuator/mappings" target="_blank">/actuator/mappings</a></td>
            </tr>
            <tr>
                <td>GET</td>
                <td>/scheduledtasks</td>
                <td>展示应用中的定时任务信息</td>
                <td><a href="/actuator/scheduledtasks" target="_blank">/actuator/scheduledtasks</a></td>
            </tr>
            <tr>
                <td>GET</td>
                <td>/sessions</td>
                <td>如果我们使用了 Spring Session 展示应用中的 HTTP Sessions 信息</td>
                <td><a href="/actuator/sessions" target="_blank">/actuator/sessions</a></td>
            </tr>
            <tr>
                <td>POST</td>
                <td>/shutdown</td>
                <td>关闭应用程序,要求 endpoints.shutdown.enabled 设置为 true</td>
                <td>curl -X POST "http://localhost:8080/actuator/shutdown"</td>
            </tr>
            <tr>
                <td>GET</td>
                <td>/threaddump</td>
                <td>获取线程活动的快照</td>
                <td><a href="/actuator/threaddump" target="_blank">/actuator/threaddump</a></td>
            </tr>
        </tbody>
    </table>
    <br />
    <p>如果您的应用程序是web应用程序(Spring MVC、Spring WebFlux或Jersey),则可以使用以下Endpoints:</p>
    <table class="table table-sm">
        <thead>
            <tr>
                <th>方法</th>
                <th>路径</th>
                <th>描述</th>
                <th>查看路径</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>GET</td>
                <td>/heapdump</td>
                <td>dump一份应用的JVM堆信息</td>
                <td><a href="/actuator/heapdump" target="_blank">/actuator/heapdump</a></td>
            </tr>
            <tr>
                <td>GET</td>
                <td>/jolokia</td>
                <td>展示JMX beans(仅当Jolokia配置到classpath的情况下可用。不可用于WebFlux)</td>
                <td><a href="/actuator/jolokia" target="_blank">/actuator/jolokia</a></td>
            </tr>
            <tr>
                <td>GET</td>
                <td>/logfile</td>
                <td>返回 log file 中的内容(如果 logging.file 或者 logging.path 被设置</td>
                <td><a href="/actuator/logfile" target="_blank">/actuator/logfile</a></td>
            </tr>
            <tr>
                <td>GET</td>
                <td>/prometheus</td>
                <td>Exposes metrics in a format that can be scraped by a Prometheus server.</td>
                <td><a href="/actuator/prometheus" target="_blank">/actuator/prometheus</a></td>
            </tr>
        </tbody>
    </table>
</body>
</html>

 

创建MainController

package com.example.demo.controller;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;


@Controller
public class MainController
{
    @RequestMapping("/")
    public String toIndex()
    {
        return "index";
    }
}

 

大部分Endpoints只需要浏览器路径访问就可以查看信息。但这里有2个特殊的需要单独说一下。

 

Heapdump

http://127.0.0.1:8080/actuator/heapdump

这个路径访问之后会下载一个文件,名为heapdump.dms

这个文件需要使用jvisualvm打开,文件在JDK根目录/Contents/Home/bin/jvisualvm

例如:

/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/bin/jvisualvm

 

运行后,使用此软件打开dms文件,即可查看JVM堆信息。

界面如下:

 

Shutdown

通过访问URL路径的方式停止Spring Boot应用,但此URL只支持POST形式请求。

wangdeMacBook-Pro:~ wang$ curl -X POST "http://localhost:8080/actuator/shutdown"
{"message":"Shutting down, bye..."}

 

 

参考文档

https://docs.spring.io/spring-boot/docs/2.0.0.RC1/reference/htmlsingle/#production-ready

 

Bootstrap Thumbnail Second
MySQL

MySQL is the world's most popular open source database.

GO

Bootstrap Thumbnail Third
算法基础

本书介绍了什么是计算机算法,如何描述它们,以及如何来评估它们。

GO