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/httptrace: 显示最近100条 request-response 信息

============================
简单示例
============================
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 能被自由地访问到, 比如被监控系统访问. 这时需要做的是, 创建一个基于 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

原文地址:https://www.cnblogs.com/harrychinese/p/SpringBoot_actuator.html

时间: 2024-08-30 04:59:05

SpringBoot系列: Actuator监控的相关文章

SpringBoot要点之使用Actuator监控

Actuator是Springboot提供的用来对应用系统进行自省和监控的功能模块,借助于Actuator开发者可以很方便地对应用系统某些监控指标进行查看.统计等. 在pom文件中加入spring-boot-starter-actuator依赖如下: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</art

SpringBoot系列之集成Druid配置数据源监控

继上一篇博客SpringBoot系列之JDBC数据访问之后,本博客再介绍数据库连接池框架Druid的使用 实验环境准备: Maven IntelliJ IDEA 先新建一个Springboot Initializer项目,详情参考SpringBoot系列之快速创建Initializer项目,注意引入必须的JDBC,web依赖等等,因为Druid默认没提供,所以去https://mvnrepository.com/artifact/com.alibaba/druid获取配置信息,项目创建之后,po

使用springboot actuator监控应用

微服务的特点决定了功能模块的部署是分布式的,大部分功能模块都是运行在不同的机器上,彼此通过服务调用进行交互,前后台的业务流会经过很多个微服务的处理和传递,出现了异常如何快速定位是哪个环节出现了问题? 在这种框架下,微服务的监控显得尤为重要.本文主要结合Spring Boot Actuator,跟大家一起分享微服务Spring Boot Actuator的常见用法,方便我们在日常中对我们的微服务进行监控治理. Actuator监控 Spring Boot使用"习惯优于配置的理念",采用包

SpringBoot31 重识Spring01-环境搭建、Actuator监控、属性配置、多环境配置

1 前言 1.1 学习阶段说明 从2016年9月开始接触IT,学习经历主要分为以下三个阶段 1.1.1 入门阶段 从最基础的前端技术HTML.JavaScript.CSS开始入门,再到后端技术Java基础.MySQL数据库基础知识.JDBC.Servclet.JSP.利用这些简单技术实现了从前端.后台.到数据库单表的CRUD操作. 1.1.2 进阶阶段 刚入行时常常听别人说起XXX框架好NB,可以干XXX.我接触了第一个框架Spring,紧接着MyBatis:再到前端框架Angular2.Vue

springboot系列一、springboot产生背景及介绍

一.为什么用Springboot 长期以来 Java 的开发一直让人所诟病: ·Java 项目开发复杂度极其高: · Java 项目的维护非常困难: · 在云时代如何实现项目的快速部署以及快速启动: · 即便使用了大量的开发框架,发现我们的开发也没少多少: · 当所有的人认为 Spring 不在前进的时候,Spring 推出了微架构实现的两个重要开发框架:SpringBoot.SpringCloud. 但是如果要想在 Spring 之中整合 RabbitMQ.Kafka.ActiveMQ.MyS

Springboot系列1_什么是Springboot

.title { text-align: center } .todo { font-family: monospace; color: red } .done { color: green } .tag { background-color: #eee; font-family: monospace; padding: 2px; font-size: 80%; font-weight: normal } .timestamp { color: #bebebe } .timestamp-kwd

SpringBoot系列——利用系统环境变量与配置文件的分支选择实现“智能部署”

前言 通过之前的博客:SpringBoot系列——jar包与war包的部署,我们已经知道了如果实现项目的简单部署,但项目部署的时候最烦的是什么?修改成发布环境对应的配置!数据库连接地址.Eureka注册中心地址.Redis服务地址等,部署环境不一样,打包的时候就要改成对应的配置:常用的环境有本地开发环境dev,本地测试环境dev-test,生产测试环境prod-test,生产环境prod: 开发的时候我们用dev,项目直接运行,不用改配置:发布本地测试环境的时候,打包之前我们要先改成对应配置:上

SpringBoot系列教程web篇之404、500异常页面配置

接着前面几篇web处理请求的博文,本文将说明,当出现异常的场景下,如404请求url不存在,,403无权,500服务器异常时,我们可以如何处理 原文友链: SpringBoot系列教程web篇之404.500异常页面配置 I. 环境搭建 首先得搭建一个web应用才有可能继续后续的测试,借助SpringBoot搭建一个web应用属于比较简单的活; 创建一个maven项目,pom文件如下 <parent> <groupId>org.springframework.boot</gr

SpringBoot系列教程web篇之重定向

原文地址: SpringBoot系列教程web篇之重定向 前面介绍了spring web篇数据返回的几种常用姿势,当我们在相应一个http请求时,除了直接返回数据之外,还有另一种常见的case -> 重定向: 比如我们在逛淘宝,没有登录就点击购买时,会跳转到登录界面,这其实就是一个重定向.本文主要介绍对于后端而言,可以怎样支持302重定向 I. 环境搭建 首先得搭建一个web应用才有可能继续后续的测试,借助SpringBoot搭建一个web应用属于比较简单的活; 创建一个maven项目,pom文