写点什么

Spring Boot 指标监控与健康检查

用户头像
偏执
关注
发布于: 1 小时前

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


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


<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。


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


ID Descriptionauditevents Exposes audit events information for the current application. Requires an AuditEventRepository bean.beans Displays a complete list of all the Spring beans in your application.显示应用程序中所有 Spring Bean 的完整列表。caches Exposes available caches.公开可用的缓存。conditions Shows the conditions that were evaluated on configuration and auto-configuration classes and the reasons why they did or did not match.configprops Displays a collated list of all @ConfigurationProperties.显示所有配置信息。env Exposes properties from Spring’s ConfigurableEnvironment.陈列所有的环境变量。flyway Shows any Flyway database migrations that have been applied. Requires one or more Flyway beans.health Shows application health information.显示应用程序的健康信息。httptrace Displays HTTP trace information (by default, the last 100 HTTP request-response exchanges). Requires an HttpTraceRepository bean.显示 HTTP 跟踪信息(默认情况下,最后 100 个 HTTP 请求-响应交换)。需要一个 HttpTraceRepository bean。info Displays arbitrary application info.显示应用程序信息。integrationgraph Shows the Spring Integration graph. Requires a dependency on spring-integration-core.loggers Shows and modifies the configuration of loggers in the application.显示和修改应用程序中的 loggers 配置。liquibase Shows any Liquibase database migrations that have been applied. Requires one or more Liquibase beans.metrics Shows ‘metrics’ information for the current application.显示当前应用程序的“指标”信息。mappings Displays a collated list of all @RequestMapping paths.显示所有 @RequestMapping 的 url 整理列表。scheduledtasks Displays the scheduled tasks in your application.sessions Allows retrieval and deletion of user sessions from a Spring Session-backed session store. Requires a Servlet-based web application using Spring Session.shutdown Lets the application be gracefully shutdown. Disabled by default.关闭应用程序(默认情况下不启用)。threaddump Performs a thread dump.  如果您的应用程序是 web 应用程序(Spring MVC、Spring WebFlux 或 Jersey),您还可以使用以下附加端点:


ID Descriptionheapdump Returns an hprof heap dump file.jolokia Exposes JMX beans over HTTP (when Jolokia is on the classpath, not available for WebFlux). Requires a dependency on jolokia-core.logfile Returns 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.prometheus Exposes 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 # 访问端点根路径,默认为 /actuatorexposure: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;


@SpringBootApplicationpublic class DemoActuatorApplication {


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


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


访问  比如想查看项目启动加载了多少配置信息,   下图只展示了部分内容:


比如想查看项目启动注入了多少 bean 对象   下图只展示了部分内容:


Spring Boot Admin  我们可以结合第三方组件实现可视化的项目监控比如:Spring Boot Admin。


搭建服务端创建项目


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


POM


<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@EnableAdminServerpublic class DemoAdminServerApplication {


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


}复制代码访问


搭建客户端创建项目


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


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


POM


<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 # 访问端点根路径,默认为 /actuatorexposure:include: '' # 需要开启的端点,值得注意的是即使配置了 '',shutdown 端点也不会开启还需要额外配置 exclude: env,caches # 不需要开启的端点 endpoint:shutdown:enabled: true # 开启 shutdown

指定服务端的访问地址

spring:boot:admin:client:url: http://localhost:8769 复制代码应用详情


至此 Spring Boot 指标监控与健康检查的知识点就讲解结束了。非常感谢你可以认真学习至此,加油少年~


exclude: env,caches   # 不需要开启的端点
复制代码


用户头像

偏执

关注

还未添加个人签名 2021.07.23 加入

还未添加个人简介

评论

发布
暂无评论
Spring Boot指标监控与健康检查