概述
Spring Boot提供了Actuator功能,完成运行时的应用监控和管理功能。可以通过HTTP,JMX(Java管理扩展)以及SSH(远程脚本)来进行Spring Boot的应用监控和管理功能。
通过HTTP使用Actuator的监控和管理功能,那么要在pom.xml文件中引入以下依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
一、端点的分类与测试
Spring Boot提供了许多监控和管理功能的端点。根据端点的作用,可以将Spring Boot提供 的原生端点分为三大类。应用配置端点、度量指标端点、操作控制端点
1:端点的开启与暴露
新建一个Spring Boot Web应用 运行主程序 然后通过访问http://localhost:8080/acutator来查看默认暴露的端点
发现Spring Boot默认暴露了health和info两个端点
在配置文件application.properties中添加以下代码可以查看所有暴露的端点
management.endpoints.web.exposure.include=*
二、应用配置端点的测试
1:conditions
用于获取应用的自动化配置报告,其中包括所有自动化配置的候选项,同时还列出了每个候选项自动化配置的各个先决条件是否满足
访问http://localhost:8080/acutator/conditions即可测试
2:beans
该端点用来获取应用上下文中创建的所有Bean,启动并暴露该端点后。可通过http://localhost:8080/beans访问
3:configprops
该端点用来获取应用中配置的属性信息报告,prefix属性代表属性的配置前缀,properties代表各个属性的名称和值 通过http://localhost:8080/acutator/configprops测试访问
4:env
该端点用于获取应用所有可用的环境属性报告。
通过http://localhost:8080/acutato/env访问
5:mappings
该端点用来返回所有Spring MVC的控制器映射关系报告 可通过
http://localhost:8080/acutator/mappings访问
6:info
该端点用来返回一些自定义的信息,在默认情况下,该端点只会返回一个空的json内容
通过http://localhost:8080/acutator/info测试访问
三、度量指标端点的测试
通过度量指标端点可获取应用程序运行过程中用于监控的度量指标
1:metrics
该端点用来返回当前应用的各类重要度量指标
通过http://localhost:8080/acutator/metrics访问
2:health
该端点哦你过来获取应用的各类健康指标信息。通过
http://localhost:8080/acutator/health访问 up表示健康 down表示异常
3:threaddump
该端点用来暴露程序运行中的线程信息。 通过
http://localhost:8080/acutator/threaddump测试访问
4:httptrace
该端点用来返回基本的HTTP跟踪信息。可通过
http://localhost:8080/acutator/httptrace访问 保留最近的100条请求信息
5:scheduledtasks
该端点统计应用程序中调度的任务 可通过
http://localhost:8080/acutator/scheduledtasks访问测试
四、操作控制端点的测试
操作控制类端点拥有更强大的控制能力,如果使用它们,需要通过属性来配置开启,在原生端点中,只提供了一个用来关闭应用的端点:shutdown 可以通过如下配置开启它
management.endpoint.shutdown.enabled=true
五、自定义端点
当Spring Boot提供的端点不能满足我们的需求时,就需要自定义 一个端点对应用进行监控
Spring Boot提供了注解@Endpoint定义一个端点类,并在端点类的方法上使用@ReadOperation注解来显示监控信息,使用@WriteOperation来动态更新监控信息
下面通过实战讲解如何自定义一个端点,并且显示数据源相关信息
1:创建Spring Boot Web应用
2:修改pom.xml
<?xml version="1.0" encoding="UTF-8"?>
-<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
-<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath/>
<!-- lookup parent from repository -->
</parent>
<groupId>com.ch</groupId>
<artifactId>ch10_2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ch10_2</name>
<description>Demo project for Spring Boot</description>
-<properties>
<java.version>11</java.version>
</properties>
-<dependencies>
-<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
-<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
-<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.45</version>
</dependency>
-<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
-<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
-<build>
-<plugins>
-<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3:配置数据源
修改application.properties文件
###
##数据源信息配置
###
#数据库地址
spring.datasource.url=jdbc:mysql://localhost:3306/springbootjpa?characterEncoding=utf8
#数据库用户名
spring.datasource.username=root
#数据库密码
spring.datasource.password=root
#数据库驱动
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#输出的JSON字符串格式更美观
spring.jackson.serialization.indent-output=true
#暴露所有端点,当然包括data-source,我们也可以只暴露data-source端点
management.endpoints.web.exposure.include=*
#将详细健康信息显示给所有用户
management.endpoint.health.show-details=always
4:自定义端点
package com.ch.ch10_2.endPoint;
import java.util.HashMap;
import java.util.Map;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.boot.actuate.endpoint.annotation.WriteOperation;
import org.springframework.stereotype.Component;
import com.zaxxer.hikari.HikariConfigMXBean;
import com.zaxxer.hikari.HikariDataSource;
import com.zaxxer.hikari.HikariPoolMXBean;
//注册为端点,id不能使用驼峰法(dataSource),需要以-分割
@Endpoint(id = "data-source")
@Component
public class DataSourceEndpoint {
//HikariDataSource提供多个监控信息
HikariDataSource ds;
public DataSourceEndpoint(HikariDataSource ds) {
this.ds = ds;
}
@ReadOperation
public Map<String, Object> info() {
Map<String, Object> map = new HashMap<String, Object>();
//连接池配置
HikariConfigMXBean configBean = ds.getHikariConfigMXBean();
map.put("max", configBean.getMaximumPoolSize());
//连接池运行状态
HikariPoolMXBean mxBean = ds.getHikariPoolMXBean();
map.put("active", mxBean.getActiveConnections());
map.put("idle", mxBean.getIdleConnections());
//连接池无连接时,等待获取连接的线程个数
map.put("wait", mxBean.getThreadsAwaitingConnection());
return map;
}
@WriteOperation
public void setMax(int max) {
ds.getHikariConfigMXBean().setMaximumPoolSize(max);
}
}
5:暴露端点
在application.properties中添加以下代码
management.endpoints.web.exposure.include=*
6:测试端点
运行主类 然后访问http://localhost:8080/actuator/date-source即可
最后
以上就是要减肥眼睛为你收集整理的使用Actuator通过HTTP监控Spring Boot应用(附源码)一、端点的分类与测试二、应用配置端点的测试 1:conditions三、度量指标端点的测试四、操作控制端点的测试五、自定义端点的全部内容,希望文章能够帮你解决使用Actuator通过HTTP监控Spring Boot应用(附源码)一、端点的分类与测试二、应用配置端点的测试 1:conditions三、度量指标端点的测试四、操作控制端点的测试五、自定义端点所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复