Spring Boot指标监控与健康检查

236 阅读4分钟

Spring Boot指标监控与健康检查

博主交流君羊;723749901【暗号67】

Actuator

  Spring Boot Actuator 可以帮助你监控和管理 Spring Boot 应用,比如健康检查、审计、统计和HTTP追踪等。所有的这些特性可以通过 JMX 或者 HTTP endpoints 来获得。

创建项目

请添加图片描述 请添加图片描述 请添加图片描述 请添加图片描述   创建 Spring Boot 项目,选择 Spring Boot Actuator 组件。

请添加图片描述

POM

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo-actuator</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo-actuator</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

        <!-- spring boot test 依赖 -->
        <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>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

监控设置

  执行器端点允许您监视应用程序并与之交互。Spring Boot 包含许多内置的端点,允许您添加自己的端点。例如,health 提供基本的应用程序健康信息。

  可以启用或禁用每个端点。这将控制是否创建端点及其 bean 存在于应用程序上下文中。要实现远程访问,还必须通过 JMX 或 HTTP 公开端点。大多数应用程序选择 HTTP,其中端点的 ID 和前缀 /actuator 被映射到一个 URL。例如,默认情况下,健康端点 health 映射到 /actuator/health

  下列端点与具体技术无关:

IDDescription
auditeventsExposes audit events information for the current application. Requires an AuditEventRepository bean.
beansDisplays a complete list of all the Spring beans in your application.
显示应用程序中所有Spring Bean的完整列表。
cachesExposes available caches.
公开可用的缓存。
conditionsShows the conditions that were evaluated on configuration and auto-configuration classes and the reasons why they did or did not match.
configpropsDisplays a collated list of all @ConfigurationProperties.
显示所有配置信息。
envExposes properties from Spring’s ConfigurableEnvironment.
陈列所有的环境变量。
flywayShows any Flyway database migrations that have been applied. Requires one or more Flyway beans.
healthShows application health information.
显示应用程序的健康信息。
httptraceDisplays HTTP trace information (by default, the last 100 HTTP request-response exchanges). Requires an HttpTraceRepository bean.
显示HTTP跟踪信息(默认情况下,最后100个HTTP请求-响应交换)。需要一个HttpTraceRepository bean。
infoDisplays arbitrary application info.
显示应用程序信息。
integrationgraphShows the Spring Integration graph. Requires a dependency on spring-integration-core.
loggersShows and modifies the configuration of loggers in the application.
显示和修改应用程序中的 loggers 配置。
liquibaseShows any Liquibase database migrations that have been applied. Requires one or more Liquibase beans.
metricsShows ‘metrics’ information for the current application.
显示当前应用程序的“指标”信息。
mappingsDisplays a collated list of all @RequestMapping paths.
显示所有 @RequestMapping 的 url 整理列表。
scheduledtasksDisplays the scheduled tasks in your application.
sessionsAllows retrieval and deletion of user sessions from a Spring Session-backed session store. Requires a Servlet-based web application using Spring Session.
shutdownLets the application be gracefully shutdown. Disabled by default.
关闭应用程序(默认情况下不启用)。
threaddumpPerforms a thread dump.

  如果您的应用程序是 web 应用程序(Spring MVC、Spring WebFlux或Jersey),您还可以使用以下附加端点:

IDDescription
heapdumpReturns an hprof heap dump file.
jolokiaExposes JMX beans over HTTP (when Jolokia is on the classpath, not available for WebFlux). Requires a dependency on jolokia-core.
logfileReturns the contents of the logfile (if logging.file.name or logging.file.path properties have been set). Supports the use of the HTTP Range header to retrieve part of the log file’s content.
prometheusExposes metrics in a format that can be scraped by a Prometheus server. Requires a dependency on micrometer-registry-prometheus.

配置文件

# 度量指标监控与健康检查
management:
  endpoints:
    web:
      base-path: /actuator    # 访问端点根路径,默认为 /actuator
      exposure:
        include: '*'          # 需要开启的端点,值得注意的是即使配置了 '*',shutdown 端点也不会开启还需要额外配置
        exclude: env,caches   # 不需要开启的端点
  endpoint:
    shutdown:
      enabled: true           # 开启 shutdown

启动类

package com.example.demoactuator;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoActuatorApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoActuatorApplication.class, args);
    }

}

启动信息

  通过控制台打印信息可以得知开启了多少访问端点。

请添加图片描述

访问

请添加图片描述

POM

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo-admin-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo-admin-server</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-boot-admin.version>2.2.1</spring-boot-admin.version>
    </properties>

    <dependencies>
        <!-- spring boot admin server 依赖 -->
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-server</artifactId>
        </dependency>
        <!-- spring boot web 依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- spring boot test 依赖 -->
        <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>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>de.codecentric</groupId>
                <artifactId>spring-boot-admin-dependencies</artifactId>
                <version>${spring-boot-admin.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

配置文件

server:
  port: 8769

启动类

  启动类需要添加 @EnableAdminServer 注解。

package com.example.demoadminserver;

import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableAdminServer
public class DemoAdminServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoAdminServerApplication.class, args);
    }

}

搭建客户端

创建项目

  创建 Spring Boot 项目,选择 Spring Boot Admin Client 组件。

  本文中不再创建,使用刚才的 Actuator 项目当作客户端使用。

POM

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo-actuator</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo-actuator</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-boot-admin.version>2.2.1</spring-boot-admin.version>
    </properties>

    <dependencies>
        <!-- spring boot admin client 依赖 -->
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-client</artifactId>
        </dependency>
        <!-- spring boot actuator 依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!-- spring boot web 依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- spring boot test 依赖 -->
        <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>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>de.codecentric</groupId>
                <artifactId>spring-boot-admin-dependencies</artifactId>
                <version>${spring-boot-admin.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

配置文件

# 度量指标监控与健康检查
management:
  endpoints:
    web:
      base-path: /actuator    # 访问端点根路径,默认为 /actuator
      exposure:
        include: '*'          # 需要开启的端点,值得注意的是即使配置了 '*',shutdown 端点也不会开启还需要额外配置
        exclude: env,caches   # 不需要开启的端点
  endpoint:
    shutdown:
      enabled: true           # 开启 shutdown

# 指定服务端的访问地址
spring:
  boot:
    admin:
      client:
        url: http://localhost:8769

博主交流君羊;723749901【暗号67】