SpringBoot系列: Actuator监控
Sprng Boot 2 actuator变动加大, 网上很多资料都都已经过期.
============================
配置项
============================
在 application.properties 配置文件, actuator 的设置项 management.endpoints(设置 actuator 全局级的属性) 和 management.endpoint(设置 具体endpoint 属性) 开头.
----------------------
全局级的控制
----------------------
#定制管理功能的 port, 如果端口为 - 代表不暴露管理功能 over HTTP
management.server.port=
# 设定 /actuator 入口路径
management.endpoints.web.base-path=/actuator
# 所有endpoint缺省为禁用状态
management.endpoints.enabled-by-default=false
# 暴露所有的endpoint, 但 shutdown 需要显示enable才暴露, * 表示全部, 如果多个的话,用逗号隔开
management.endpoints.web.exposure.include=*
# 排除暴露 loggers和beans endpoint
management.endpoints.web.exposure.exclude=loggers,beans
# 定制化 health 端点的访问路径
management.endpoints.web.path-mapping.health=healthcheck
----------------------
endpoint 级别的控制
----------------------
所有的endpoint都有 enabled 属性, 可以按需开启或关闭特定端点.
#启用 shutdown
management.endpoint.shutdown.enabled=true
----------------------
重点关注 health 端点一些配置项
----------------------
management.endpoint.health.enabled=true
#show-details属性的取值有: never/always/when-authorized, 默认值是 never
management.endpoint.health.show-details=always
#增加磁盘空间health 统计, 还有其他health indicator
management.health.diskspace.enabled=true
----------------------
actuator 缺省的设置
----------------------
缺省 actuator 的根路径为 /actuator
缺省仅开放 health 和 info, 其他都不开放.
有些 endpoint 是GET, 有些是POST方法, 比如 health 为GET, shutdown 为 POST, 从SpringBoot程序启动日志中, 可以看出到底有哪些endpoint被开启.
----------------------
endpoint 清单
----------------------
actuator 支持的所有 endpoint, 可以查官网 https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-endpoints.html
下面是一些重要的端点,(以访问路径列出):
/actuator: actuator 严格地说不能算 endpoint, /actuator 返回要给 endpoint 的清单.
/actuator/health: 展现系统运行状态, 在和spring cloud consul集成时候, 就使用该端点检查Spring应用的状态.
/actuator/metric/: 显示更全面的系统指标
/actuator/configprops:展现 SpringBoot 配置项
/actuator/env: 显示系统变量和SpringBoot应用变量, actuator 非常贴心, 如果属性名包含 password/secret/key 这些关键词, 对应的属性值将用 * 号代替.
/actuator/httptrace: 显示最近100条 request-response 信息
/actuator/autoconfig: 生成Spring boot自动化配置报告, 该报告非常有用, 说明如下: Spring Boot项目倾向于使用很多auto config技术, 包括散落在很多config java类. 我们在开发过程中偶尔会遇到, 为什么我的配置没起作用这样的问题. 这时候查看 /actuator/autoconfig 的报告非常有用, 它会告诉你哪些自动化装配成功了,哪些没有成功.
/actuator/beans: 该端点可以获取 application context 中创建的所有 bean, 并列出它们的scope 和 type 等详细信息
/actuator/mappings: 该端点列出了所有 controller 的路由信息.
============================
简单示例
============================
pom 加上 actuator 依赖, 注意不加 spring-boot-starter-security.
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
application.properties 内容如下,
management.endpoints.enabled-by-default=true
management.endpoints.web.exposure.include=*
management.endpoints.web.exposure.exclude=loggers,beans management.endpoint.health.show-details=always
management.health.diskspace.enabled=true
management.health.enabled=true
该项目不需要手写任何 java 代码的情况下, 已有了监控检查功能, 访问 http://localhost:8080/actuator/health 即可.
============================
加上 spring-boot-starter-security 安全控制
============================
接上面示例project, 增加了 pring-boot-starter-security 依赖项.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
增加了 pring-boot-starter-security 依赖项后, 再访问 health 端点将会跳转到一个 login 界面, 实际情况往往是我们总是希望 health 能被自由地访问到, 比如被监控系统访问. 有两个做法:
1.粗暴的方式,直接在主程序类上禁止 Security 的 auto configuration, 注解为 @SpringBootApplication(exclude = { SecurityAutoConfiguration.class })
2. 创建一个基于 WebSecurityConfigurerAdapter 的配置类, 在其鉴权configure()方法中, 开启对特定 actuator 端点的访问许可.
@Configuration
public class ActuatorSecurityConfig extends WebSecurityConfigurerAdapter { /*
This spring security configuration does the following 1. Restrict access to the Shutdown endpoint to the ACTUATOR_ADMIN role.
2. Allow access to all other actuator endpoints.
3. Allow access to static resources.
4. Allow access to the home page (/).
5. All other requests need to be authenticated.
5. Enable http basic authentication to make the configuration complete.
You are free to use any other form of authentication.
*/ @Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.requestMatchers(EndpointRequest.to(ShutdownEndpoint.class)).hasRole("ACTUATOR_ADMIN")
.requestMatchers(EndpointRequest.toAnyEndpoint()).permitAll()
.requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll()
.antMatchers("/").permitAll()
.antMatchers("/**").authenticated()
.and()
.httpBasic();
}
}
在 application.properties 先简单设置spring security 相关属性, 然后运行程序, 访问 http://localhost:8080/actuator/health 进行测试.
# Spring Security Default user name and password
spring.security.user.name=actuator
spring.security.user.password=actuator
spring.security.user.roles=ACTUATOR_ADMIN
============================
参考
============================
Spring Boot Actuator: Health check, Auditing, Metrics gathering and Monitoring
https://www.callicoder.com/spring-boot-actuator/
springboot(十九):使用Spring Boot Actuator监控应用
http://www.ityouknow.com/springboot/2018/02/06/spring-boot-actuator.html
Spring Boot Actuator监控端点小结
http://blog.didispace.com/spring-boot-actuator-1/
老司机的应用级监控——spring actuator
https://www.jianshu.com/p/c043d3c71f47
Spring Boot Actuator in Spring Boot 2.0
https://dzone.com/articles/spring-boot-actuator-in-spring-boot-20
SpringBoot-Actuator-加SpringSecurity验证
https://blog.csdn.net/goldenfish1919/article/details/78130516
SpringBoot系列: Actuator监控的更多相关文章
- SpringBoot集成Actuator监控管理
1.说明 本文详细介绍Spring Boot集成Actuator监控管理的方法, 基于已经创建好的Spring Boot工程, 然后引入Actuator依赖, 介绍监控管理相关功能的使用. Sprin ...
- Spring Boot 之:Actuator 监控
在 Spring Boot 2.x 中为了安全,Actuator 只开放了两个端点 /actuator/health 和 /actuator/info.可以在配置文件中设置打开. Actuator 默 ...
- SpringBoot系列九:SpringBoot服务整合(整合邮件服务、定时调度、Actuator监控)
声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.概念:SpringBoot 服务整合 2.背景 在进行项目开发的时候经常会遇见以下的几个问题:需要进行邮件发送.定时的任务调 ...
- SpringBoot系列——admin服务监控
前言 springboot项目部署起来后,如何实时监控项目的运行状况呢?本文记录使用springboot-admin对服务进行监控. springboot-admin介绍:https://codece ...
- Springboot 系列(九)使用 Spring JDBC 和 Druid 数据源监控
前言 作为一名 Java 开发者,相信对 JDBC(Java Data Base Connectivity)是不会陌生的,JDBC作为 Java 基础内容,它提供了一种基准,据此可以构建更高级的工具和 ...
- SpringBoot 2.x (15):Actuator监控
Actuator监控:SpringBoot自带的,对生成环境进行监控的系统 使用:既然是监控,那就不能监控一个空项目 这里我使用SpringBoot整合MyBatis的Demo: https://ww ...
- SpringBoot系列之集成Druid配置数据源监控
SpringBoot系列之集成Druid配置数据源监控 继上一篇博客SpringBoot系列之JDBC数据访问之后,本博客再介绍数据库连接池框架Druid的使用 实验环境准备: Maven Intel ...
- SpringBoot要点之使用Actuator监控
Actuator是Springboot提供的用来对应用系统进行自省和监控的功能模块,借助于Actuator开发者可以很方便地对应用系统某些监控指标进行查看.统计等. 在pom文件中加入spring-b ...
- springboot(十九)使用actuator监控应用【转】【补】
springboot(十九)使用actuator监控应用 微服务的特点决定了功能模块的部署是分布式的,大部分功能模块都是运行在不同的机器上,彼此通过服务调用进行交互,前后台的业务流会经过很多个微服务的 ...
随机推荐
- Hdoj 1213.How Many Tables 题解
Problem Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. ...
- 【BZOJ5289】[HNOI2018]排列(贪心)
[BZOJ5289][HNOI2018]排列(贪心) 题面 BZOJ 洛谷 题解 这个限制看起来不知道在干什么,其实就是找到所有排列\(p\)中,\(p_k=x\),那么\(k<j\),其中\( ...
- 51NOD1174 区间最大数 && RMQ问题(ST算法)
RMQ问题(区间最值问题Range Minimum/Maximum Query) ST算法 RMQ(Range Minimum/Maximum Query),即区间最值查询,是指这样一个问题:对于长度 ...
- [WC2008]游览计划 解题报告
[WC2008]游览计划 斯坦纳树板子题,其实就是状压dp 令\(dp_{i,s}\)表示任意点\(i\)联通关键点集合\(s\)的最小代价 然后有转移 \[ dp_{i,S}=\min_{T\in ...
- [ZJOI2010]贪吃的老鼠(网络流+建图)
题目描述 奶酪店里最近出现了m只老鼠!它们的目标就是把生产出来的所有奶酪都吃掉.奶酪店中一天会生产n块奶酪,其中第i块的大小为pi,会在第ri秒被生产出来,并且必须在第di秒之前将它吃掉.第j只老鼠吃 ...
- 解题:HAOI2018 苹果树
题面 统计贡献,每个大小为i的子树贡献就是$i(n-i)$,然后子树里又有$i!$种:同时这个子树的根不确定,再枚举这个根是$r$个放的,又有了$r!$种.子树内选点的方式因为子树的根被钦定了顺序所以 ...
- cf 990G - GCD Counting
题意 #include<bits/stdc++.h> #define t 200000 #define MAXN 200100 using namespace std; int n; in ...
- A1132. Cut Integer
Cutting an integer means to cut a K digits lone integer Z into two integers of (K/2) digits long int ...
- 利用SHAPEIT将vcf文件进行基因型(genotype)定相(phasing):查看两个突变是否来源于同一条链(染色体或父本或母本),two mutations carried by the same read
首先,下载SHAPEIT. 按照里面的步骤安装完后,将vcf文件进行基因型定相,分四步走. 第一步,将vcf文件转化为plink二进制文件(.bed, .bim, .fam). 这一步需要用到GATK ...
- 使用海康的某款摄像头以及v4l2的经验
Video Capture模式下发现使用MJPEG流和YUYV流相同分辨率视野却不同. 最后没找到怎么设物理窗口或图片窗口的方法,做实验知道了为什么. 设为1280×720 YUYV实际物理窗口是19 ...