1. 在项目pom中加入Actuator依赖
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zjw</groupId>
<artifactId>spring-boot-study</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.16.RELEASE</version>
</parent>
<dependencies>
<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>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
</project>
2. 在application.yml中加入Actuator端口等信息
spring:
application:
name: spring-boot-study
management:
port: 9000
security:
enabled: false
SpringBoot 1.5.x之后默认开启了Actuator的安全认证,为了在浏览器上展示效果,不做验证,所以将management.security.enabled置为false.
Actuator提供了13个API接口,用于监控运行状态的SpringBoot的状态,如下:
类型 | API接口 | 描述 |
---|---|---|
GET | http://localhost:9000/autoconfig | 自动配置报告,记录了哪些自动配置条件通过了,哪些没有通过 |
GET | http://localhost:9000/configprops | 描述配置属性如何注入Bean |
GET | http://localhost:9000/beans | 描述应用程序上下文中的全部Bean,以及他们之间的关系 |
GET | http://localhost:9000/dump | 获取线程活动的快照 |
GET | http://localhost:9000/env | 获取全部环境变量 |
GET | http://localhost:9000/env/{name} | 根据名称获取指定的环境变量 |
GET | http://localhost:9000/health | 应用程序健康指数 |
GET | http://localhost:9000/info | 获取应用程序的信息 |
GET | http://localhost:9000/mappings | 描述全部的URI路径,以及他们和控制器(包括actuator端点)的映射关系 |
GET | http://localhost:9000/metrics | 获取应用程序的指标信息,例如内存使用情况,HTTP请求数量等信息 |
GET | http://localhost:9000/metrics/{name} | 获取指定指标 |
GET | http://localhost:9000/trace | 提供基本的HTTP请求跟踪信息 |
POST | http://localhost:9000/shutdown | 关闭应用程序,如果要使这个功能生效,需要在application.yml中打开配置项endpoints.shutdown.enabled为true |
3. 使用ssh连接Actuator
使用shell连接Actuator需要在Pom中加入如下依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-remote-shell</artifactId>
</dependency>
然后重启springboot项目,会在控制台中打印出连接密码:
Using default password for shell access: 0bc9bb95-fcdc-4da2-8aae-2cd245f7894f
可以使用ssh进行连接,如图所示:
连接上shell之后,可以通过终端查看Actuator的各个端点,SpringBoot提供了四个特有的shell命令,如表格所示:
命令 | 说明 |
---|---|
beans | 列出SpringBoot上下文中所有bean的信息 |
endpoint | 调用Actuator端点 |
metrics | SpringBoot的指标信息 |
autoconfig | 自动配置说明报告 |