SpringBoot应用监控
Springboot应用监控
使用Actuator实现监控
应用端点信息介绍
/info 应用基本信息
/health 健康度信息
/metrics 运行指标
/env 环境变量信息
/loggers 日志相关
/dump 线程相关信息
/trace 请求调用轨迹
ID | 描述 | 是否需要鉴权 |
---|---|---|
actuator | 为其他端点提供“发现页面”。要求Spring HATEOAS在classpath路径上。 | 需要 |
auditevents | 陈列当前应用程序的审计事件信息。 | 需要 |
autoconfig | 展示自动配置信息并且显示所有自动配置候选人以及他们“被不被”应用的原因。 | 需要 |
beans | 显示应用程序中所有Spring bean的完整列表。 | 需要 |
configprops | 显示所有配置信息。 | 需要 |
dump | dump所有线程。 | 需要 |
env | 陈列所有的环境变量。 | 需要 |
flyway | Shows any Flyway database migrations that have been applied. | 需要 |
health | 显示应用程序运行状况信息 | 不需要 |
info | 显示应用信息。 | 不需要 |
loggers | 显示和修改应用程序中的loggers配置。 | 需要 |
liquibase | 显示已经应用的任何Liquibase数据库迁移。 | 需要 |
metrics | 显示当前应用程序的“指标”信息。 | 需要 |
mappings | 显示所有@RequestMapping 的url整理列表。 | 需要 |
shutdown | 关闭应用(默认情况下不启用)。 | 需要 |
trace | 显示跟踪信息(默认最后100个HTTP请求)。 | 需要 |
一、应用端点
1、pom.xml
spring-boot-starter-actuator
spring-boot-starter-security
<?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.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>org.lc</groupId>
<artifactId>actuator</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>actuator</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</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-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2、security安全配置
httpBasic: 使用httpBasic认证
@Configuration
public class SecurtyConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
// 请求匹配其他所有端点的请求
http.requestMatcher(EndpointRequest.toAnyEndpoint())
// 开启所有请求授权
.authorizeRequests()
// 其他请求必须要有admin角色
.anyRequest().hasRole("admin")
.and()
// 使用httpBasic认证
.httpBasic();
}
}
3、properties配置
#关闭默认的配置(即 关闭所有默认开启的端口),一般没有必要配置
#management.endpoints.enabled-by-default=false
#是否开启远程应用关闭功能 (默认一般只有这个是关闭的)
#post:http://localhost:8080/actuator/shutdown 即可关闭应用
#但是这样不够安全 需要进行配置security
management.endpoint.shutdown.enabled=true
#虽然默认开启了很多端点 但是都没有暴露
#暴露所有端点
management.endpoints.web.exposure.include=*
#配置用户名密码角色
spring.security.user.name=lc
spring.security.user.password=123
spring.security.user.roles=admin
#配置访问的基础路径
#默认为/actuator
management.endpoints.web.base-path=/lc
#默认为/actuator/helath
#health路径的重新配置
management.endpoints.web.path-mapping.health=/lc-health
#允许的域(默认* 所有)
#自定义运行请求的地址http://localhost:8081
management.endpoints.web.cors.allowed-origins=http://localhost:8081
#允许的请求方法类型
#默认为* 所有
management.endpoints.web.cors.allowed-methods=GET,POST
4、访问测试
①访问health
默认:GET http://localhost:8080/actuaotr/health
GET
http://localhost:8080/lc/lc-health
选择:Authorization->Basic Auth
Username:lc password:123
②访问beans
默认:GET http://localhost:8080/actuaotr/beans
GET
http://localhost:8080/lc/beans
选择:Authorization->Basic Auth
Username:lc password:123
二、 health端点
#关闭默认的配置(即 关闭所有默认开启的端口),一般没有必要配置
#management.endpoints.enabled-by-default=false
#是否开启远程应用关闭功能 (默认一般只有这个是关闭的)
#post:http://localhost:8080/actuator/shutdown 即可关闭应用
#但是这样不够安全 需要进行配置security
management.endpoint.shutdown.enabled=true
#虽然默认开启了很多端点 但是都没有暴露
#暴露所有端点
management.endpoints.web.exposure.include=*
#配置用户名密码角色
spring.security.user.name=lc
spring.security.user.password=123
spring.security.user.roles=admin
#配置访问的基础路径
#默认为/actuator
#management.endpoints.web.base-path=/lc
#默认为/actuator/helath
#health路径的重新配置
#management.endpoints.web.path-mapping.health=/lc-health
#允许的域(默认* 所有)
#自定义运行请求的地址http://localhost:8081
management.endpoints.web.cors.allowed-origins=http://localhost:8081
#允许的请求方法类型
#默认为* 所有
management.endpoints.web.cors.allowed-methods=GET,POST
#验证后展示健康信息
management.endpoint.health.show-details=when_authorized
当配置其他应用时,如redis,mongo,mysql等都会有健康信息
GET
http://localhost:8080/actuaotr/health
{
"status": "UP",
"components": {
"diskSpace": {
"status": "UP",
"details": {
"total": 492657700864,
"free": 265716948992,
"threshold": 10485760
}
},
"ping": {
"status": "UP"
}
}
}
三、info端点
1、自定义信息
①通过配置文件配置
#关闭默认的配置(即 关闭所有默认开启的端口),一般没有必要配置
#management.endpoints.enabled-by-default=false
#是否开启远程应用关闭功能 (默认一般只有这个是关闭的)
#post:http://localhost:8080/actuator/shutdown 即可关闭应用
#但是这样不够安全 需要进行配置security
management.endpoint.shutdown.enabled=true
#虽然默认开启了很多端点 但是都没有暴露
#暴露所有端点
management.endpoints.web.exposure.include=*
#配置用户名密码角色
spring.security.user.name=lc
spring.security.user.password=123
spring.security.user.roles=admin
#配置访问的基础路径
#默认为/actuator
#management.endpoints.web.base-path=/lc
#默认为/actuator/helath
#health路径的重新配置
#management.endpoints.web.path-mapping.health=/lc-health
#允许的域(默认* 所有)
#自定义运行请求的地址http://localhost:8081
management.endpoints.web.cors.allowed-origins=http://localhost:8081
#允许的请求方法类型
#默认为* 所有
management.endpoints.web.cors.allowed-methods=GET,POST
#验证后展示健康信息
management.endpoint.health.show-details=when_authorized
#配置应用信息
info.app.encoding=@project.build.sourceEncoding@
info.app.java.source=@java.version@
info.app.java.target=@java.version@
info.author.name=lc
info.author.address=www.louchen.top
GET:
http://localhost:8080/actuator/info
{
"app": {
"encoding": "UTF-8",
"java": {
"source": "1.8.0_191",
"target": "1.8.0_191"
}
},
"author": {
"name": "lc",
"address": "www.louchen.top"
}
}
②通过代码配置
会覆盖在properties的配置
@Component
public class LcInfo implements InfoContributor {
@Override
public void contribute(Info.Builder builder) {
Map<String,Object> info=new HashMap<>();
info.put("email", "421192425@qq.com");
builder.withDetail("author", info);
}
}
GET
http://localhost:8080/actuaotr/health
{
"app": {
"encoding": "UTF-8",
"java": {
"source": "1.8.0_191",
"target": "1.8.0_191"
}
},
"author": {
"email": "421192425@qq.com"
}
}
2、git信息
①创建该项目所在的版本库
git init-->git add . -->git commit -m '信息'
②安装git信息插件
<build>
<plugins>
<!--添加git插件 获取git版本信息-->
<plugin>
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
③生成git.properties
maven->plugins->git-commit-id
点击:git-commit-id:revision
在target->classes下生成:git.properties
#Generated by Git-Commit-Id-Plugin
#Wed Apr 08 16:33:41 GMT+08:00 2020
git.branch=master
git.build.host=LAPTOP-LJJM9R29
git.build.time=2020-04-08T16\:33\:41+0800
git.build.user.email=421192425@qq.com
git.build.user.name=louchen97
git.build.version=0.0.1-SNAPSHOT
git.closest.tag.commit.count=
git.closest.tag.name=
git.commit.id=5a55c763ecfb66fff1986fce9880663a72ab7e88
git.commit.id.abbrev=5a55c76
git.commit.id.describe=5a55c76-dirty
git.commit.id.describe-short=5a55c76-dirty
git.commit.message.full=创建
git.commit.message.short=创建
git.commit.time=2020-04-08T16\:30\:36+0800
git.commit.user.email=421192425@qq.com
git.commit.user.name=louchen97
git.dirty=true
git.local.branch.ahead=NO_REMOTE
git.local.branch.behind=NO_REMOTE
git.remote.origin.url=Unknown
git.tags=
git.total.commit.count=1
④主配置文件
#关闭默认的配置(即 关闭所有默认开启的端口),一般没有必要配置
#management.endpoints.enabled-by-default=false
#是否开启远程应用关闭功能 (默认一般只有这个是关闭的)
#post:http://localhost:8080/actuator/shutdown 即可关闭应用
#但是这样不够安全 需要进行配置security
management.endpoint.shutdown.enabled=true
#虽然默认开启了很多端点 但是都没有暴露
#暴露所有端点
management.endpoints.web.exposure.include=*
#配置用户名密码角色
spring.security.user.name=lc
spring.security.user.password=123
spring.security.user.roles=admin
#配置访问的基础路径
#默认为/actuator
#management.endpoints.web.base-path=/lc
#默认为/actuator/helath
#health路径的重新配置
#management.endpoints.web.path-mapping.health=/lc-health
#允许的域(默认* 所有)
#自定义运行请求的地址http://localhost:8081
management.endpoints.web.cors.allowed-origins=http://localhost:8081
#允许的请求方法类型
#默认为* 所有
management.endpoints.web.cors.allowed-methods=GET,POST
#验证后展示健康信息
management.endpoint.health.show-details=when_authorized
#配置应用信息
info.app.encoding=@project.build.sourceEncoding@
info.app.java.source=@java.version@
info.app.java.target=@java.version@
info.author.name=lc
info.author.address=www.louchen.top
#git信息配置
#simple(默认)展示一些简单信息
#full展示所有git信息
management.info.git.mode=full
GET: http://localhost:8080/actuator/info
{
"app": {
"encoding": "UTF-8",
"java": {
"source": "1.8.0_191",
"target": "1.8.0_191"
}
},
"author": {
"email": "421192425@qq.com"
},
"git": {
"build": {
"host": "LAPTOP-LJJM9R29",
"version": "0.0.1-SNAPSHOT",
"time": "2020-04-08T08:33:41Z",
"user": {
"name": "louchen97",
"email": "421192425@qq.com"
}
},
"branch": "master",
"commit": {
"message": {
"short": "创建",
"full": "创建"
},
"id": {
"describe": "5a55c76-dirty",
"abbrev": "5a55c76",
"describe-short": "5a55c76-dirty",
"full": "5a55c763ecfb66fff1986fce9880663a72ab7e88"
},
"time": "2020-04-08T08:30:36Z",
"user": {
"email": "421192425@qq.com",
"name": "louchen97"
}
},
"closest": {
"tag": {
"name": "",
"commit": {
"count": ""
}
}
},
"local": {
"branch": {
"ahead": "NO_REMOTE",
"behind": "NO_REMOTE"
}
},
"dirty": "true",
"remote": {
"origin": {
"url": "Unknown"
}
},
"tags": "",
"total": {
"commit": {
"count": "1"
}
}
}
}
3、项目构建信息
maven-->Plugins-->spring-boot
点击:spring-boot:build-info
在classes下的META-INF下生成build-info.properties
build.artifact=actuator
build.group=org.lc
build.name=actuator
build.time=2020-04-08T09\:33\:21.830Z
build.version=0.0.1-SNAPSHOT
GET http://localhost:8080/actuator/info
{
"app": {
"encoding": "UTF-8",
"java": {
"source": "1.8.0_191",
"target": "1.8.0_191"
}
},
"author": {
"email": "421192425@qq.com"
},
"git": {
"build": {
"host": "LAPTOP-LJJM9R29",
"version": "0.0.1-SNAPSHOT",
"time": "2020-04-08T08:33:41Z",
"user": {
"name": "louchen97",
"email": "421192425@qq.com"
}
},
"branch": "master",
"commit": {
"message": {
"short": "创建",
"full": "创建"
},
"id": {
"describe": "5a55c76-dirty",
"abbrev": "5a55c76",
"describe-short": "5a55c76-dirty",
"full": "5a55c763ecfb66fff1986fce9880663a72ab7e88"
},
"time": "2020-04-08T08:30:36Z",
"user": {
"email": "421192425@qq.com",
"name": "louchen97"
}
},
"closest": {
"tag": {
"name": "",
"commit": {
"count": ""
}
}
},
"local": {
"branch": {
"ahead": "NO_REMOTE",
"behind": "NO_REMOTE"
}
},
"dirty": "true",
"remote": {
"origin": {
"url": "Unknown"
}
},
"tags": "",
"total": {
"commit": {
"count": "1"
}
}
},
"build": {
"version": "0.0.1-SNAPSHOT",
"artifact": "actuator",
"name": "actuator",
"group": "org.lc",
"time": "2020-04-08T09:33:21.830Z"
}
}