34. 应用监控【监控端点配置】

SpringBoot的actuator模块提供了丰富的端点用于监控和管理应用,包括健康检查、应用信息和内存使用等。开发者可以通过配置暴露特定端点,并使用SpringSecurity保护这些端点的安全。此外,还可以设置端点响应的缓存时间和自定义路径映射,以及开启CORS支持以实现跨域访问。
摘要由CSDN通过智能技术生成




       当一个 Spring Boot 项目运行时,开发者需要对 Spring Boot 项目进行实时监控来获取项目的运行情况,在项目出错时能够实现自动报警等。 Spring Boot 提供了actuator 来帮助开发者获取应用程序的实时运行数据。开发者可以选择使用 HTTP 端点或JMX来管理和监控应用程序,获取应用程序的运行数据,包括健康状况、应用信息、内存使用情况等。

1、开启端点

       在Spring Boot 中开启应用监控非常容易 ,只需要添spring-boot-starter-actuator 依赖即可。依赖如下:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

       开发者可以使用执行器中的端点( EndPoints )对应用进行监控或者与应用进行交互,Spring Boot 默认包含许多端点 ,如下图所示:
在这里插入图片描述
       如果系统是一个web应用的话,还会有其他端点:
在这里插入图片描述
       这些端点大部分都是默认开启的, 只有 shutdown 端点未开启,如果需要开启,可以在 application.properties 文件中添加以下配置信息:

#开启shutdown端点
management.endpoint.shutdown.enabled=true

       如果开发者不想暴露这么多端点,那么可以关闭默认配置,然后手动指定需要开启哪些端点, 如下配置表示关闭所有端点,只开启 info 端点:

#关闭所有端点,只开启 info 端点
management.endpoints.enabled-by-default=false 
management.endpoint.info.enabled=true

2、暴露端点

       由于有的端点包含敏感信息,因此端点启用和暴露是两码事,以下图展示了默认暴露的断点情况。
在这里插入图片描述
       在web应用中默认暴露了health和info两个端点,以下是启动日志:
在这里插入图片描述
       通过日志提供的路径访问该地址:localhost:8080/actuator
在这里插入图片描述
       我们可以在配置文件中自定义需要暴露哪些端点,例如要暴露 mappings、metrics 端点,添加如下配置即可:

#暴露指定端点
management.endpoints.web.exposure.include=mappings,metrics

在这里插入图片描述

#暴露所有端点
management.endpoints.web.exposure.include=*

在这里插入图片描述

3、端点保护

       如果这些端点需要对外提供服务,那么最好能够将这些端点保护起来,若 classpath 中存在 Spring Security ,则默认使用 Spring Security 保护,使用 Spring Security 保护的步骤很简单,首先添加 Spring Security 依赖:

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-security</artifactId>
</dependency>

Java配置文件

import org.springframework.boot.actuate.autoconfigure.security.servlet.EndpointRequest;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
public class ActuatorSecurity extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception{
        httpSecurity.requestMatcher(EndpointRequest.toAnyEndpoint())
                .authorizeRequests()
                .anyRequest().hasAnyRole("admin")
                .and()
                .httpBasic();
    }
}

       在 HttpSecurity 中配置 Endpoint 都需要具有 admin 角色才能访问,同时开启 httpBasic 认证。注意, endpointRequest.toAnyEndpoint() 表示匹配所有的 endpoint,例如 shutdown、mappings、health 等,但是不包括开发者通过@RequestMapping 注解定义的接口,这里为了演示方便, Spring Security 就不连接数据 了, 直接在 appIication. properties 定义一个用户进行测试,代码如下:

spring.security.user.name=admin
spring.security.user.password=123456
spring.security.user.roles=admin

       定义完成后启动 Spring Boot 项目 再去访问 health 端点,需要登录后才可以访问
在这里插入图片描述

4、端点晌应缓存

       对于一些不带参数的端点请求会自动进行缓存,我们可以通过如下方式配置缓存时间。这个配置表示 beans 端点的缓存时间为 100s ,如果要配置其他端点,只需将 beans 修改为其他端点名称即可。注意,如果端点添加了 Spring Security 保护 ,此时 Principal 会被视为端点的输入,因此端点响应将不被缓存。

management.endpoint.beans.cache.time-to-live=100s

5、路径映射

       默认情况下 ,所有端点都暴露在/actuator路径下,例如 health 端点的访问路径是/actuator/health ,如果开发者需要对端点路径进行定制,可以通过如下配置进行,访问地址变为:localhost:8080/healthcheck

#路径映射
management.endpoints.web.base-path=/
management.endpoints.web.path-mapping.health=healthcheck

6、CORS 支持

       所有端点默认都没有开启跨域,开发者可以通过如下配置快速开启 CORS 支持进而实现跨域。这个配置表示允许端点处理来自 http://localhost:8081 地址的请求,允许的请求方法为 GET、POST

management.endpoints.web.cors.allowed-origins=http://localhost:8081
management.endpoints.web.cors.allowed-methods=GET,POST
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值