当前位置: 首页 > news >正文

SpringBoot系列: Actuator监控

Sprng Boot 2 actuator变动加大, 网上很多资料都都已经过期.  

============================
配置项
============================
在 application.properties 配置文件, actuator 的设置项 management.endpoints(设置 actuator 全局级的属性) 和 management.endpoint(设置 具体endpoint 属性) 开头.

----------------------
全局级的控制
----------------------

#定制管理功能的 port, 如果端口为 -1 代表不暴露管理功能 over HTTP
management.server.port=8081      
# 设定 /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

 

相关文章:

  • JavaScript常用八种继承方案
  • 20172313 2018-2019-1 《程序设计与数据结构》课堂测试修改报告
  • 使用fiddler抓取手机上的HTTPS包
  • 云栖科技评论第76期:车用半导体混战 中国怎么战?
  • 兼容性总结
  • 运维技术(一)用docker安装elk之CentOS7.4
  • 时间复杂度分析经典问题——最大子序列和
  • Android Studio踩过的坑
  • 细说Redis(一)之 Redis的数据结构与应用场景
  • Python变量的相互转换
  • 2018.10.23-dtoi-1770不设找零No Change (nochange)
  • 【NOIP2017D2T3】列队
  • Algs4-1.3.13判断正确的出队次序
  • Dubbo分析之Exchange层
  • QML-qmake大法
  • 30天自制操作系统-2
  • HTML5新特性总结
  • httpie使用详解
  • JavaScript 是如何工作的:WebRTC 和对等网络的机制!
  • LeetCode算法系列_0891_子序列宽度之和
  • MobX
  • Mysql数据库的条件查询语句
  • passportjs 源码分析
  • PAT A1092
  • php的插入排序,通过双层for循环
  • vue2.0项目引入element-ui
  • 从零开始的无人驾驶 1
  • 关于 Linux 进程的 UID、EUID、GID 和 EGID
  • 官方新出的 Kotlin 扩展库 KTX,到底帮你干了什么?
  • 技术:超级实用的电脑小技巧
  • 开源中国专访:Chameleon原理首发,其它跨多端统一框架都是假的?
  • 前端学习笔记之观察者模式
  • 通过获取异步加载JS文件进度实现一个canvas环形loading图
  • 温故知新之javascript面向对象
  • 详解移动APP与web APP的区别
  • 硬币翻转问题,区间操作
  • 用quicker-worker.js轻松跑一个大数据遍历
  • 第二十章:异步和文件I/O.(二十三)
  • $.ajax()方法详解
  • %3cli%3e连接html页面,html+canvas实现屏幕截取
  • (51单片机)第五章-A/D和D/A工作原理-A/D
  • (Demo分享)利用原生JavaScript-随机数-实现做一个烟花案例
  • (JSP)EL——优化登录界面,获取对象,获取数据
  • (紀錄)[ASP.NET MVC][jQuery]-2 純手工打造屬於自己的 jQuery GridView (含完整程式碼下載)...
  • (欧拉)openEuler系统添加网卡文件配置流程、(欧拉)openEuler系统手动配置ipv6地址流程、(欧拉)openEuler系统网络管理说明
  • (三)Pytorch快速搭建卷积神经网络模型实现手写数字识别(代码+详细注解)
  • (一)eclipse Dynamic web project 工程目录以及文件路径问题
  • .NET/ASP.NETMVC 大型站点架构设计—迁移Model元数据设置项(自定义元数据提供程序)...
  • .Net多线程总结
  • .NET简谈互操作(五:基础知识之Dynamic平台调用)
  • .NET企业级应用架构设计系列之应用服务器
  • // an array of int
  • [ai笔记9] openAI Sora技术文档引用文献汇总
  • [AR Foundation] 人脸检测的流程
  • [C#] 基于 yield 语句的迭代器逻辑懒执行