如何做好对Spring Boot项目全面监控之Actuator

开发 架构
Actuator 为Spring Boot项目提供了很全面的监控和审查,通过启用和公开Endpoints,可以很方便的查看项目中的一些指标。

定义

Actuator 为springboot项目提供了很全面的监控和审查,通过启用和公开endpoints,可以很方便的查看项目中的一些指标。

添加依赖

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

关于Endpoints

Actuator提供了很多内置的Endpoints,每一个EP都可以启用/禁用 和 公开。

当然,EP只有启动并公开之后,才会真正生效。

如何启用/禁用Endpoints?

除了 shutdown 之外,所有的EP都是默认启用的,如果要启用/禁用EP,可使用以下配置:

management.endpoint.<id>.enabled=true/false​。

比如启用 shutdown :

management.endpoint.shutdown.enabled=true

为了安全,在生产环境不建议启用所有的EP,而是按需启用,所以要首先禁用默认行为,然后启用需要的EP,如下:

management.endpoints.enabled-by-default=false
management.endpoint.info.enabled=true

这样只有启用的EP才会加载到上下文。

这样只是启用和禁用了EP,但是否暴露出去,还需要单独的配置。

如何暴露/隐藏 Endpoints?

EP会暴露应用的很多指标,所以非常重要和敏感,在生产环境一定要做好选择和安全防护。

暴露和隐藏使用以下配置:

# 针对 JMX 规范协议
management.endpoints.jmx.exposure.exclude
management.endpoints.jmx.exposure.include *
# 针对http协议
management.endpoints.web.exposure.exclude
management.endpoints.web.exposure.include health

使用 include 暴露Ep,使用 exclude 隐藏 Ep,其中 exclude 优先级高于 include。

星号 * 表示全部的,另外星号在properties和yaml中有一点不同:

在YAML中星号(*)具有特殊的含义,所以需要用双引号括起来,如 "*"

比如要隐藏所有的并仅仅启用health和info:

management.endpoints.web.exposure.include=health,info

比如要暴露所有并禁用env和beans:

management.endpoints.web.exposure.include=*
management.endpoints.web.exposure.exclude=env,beans

前文有述,EP包括很多敏感信息,一定要注意安全,那应该如何确保安全?

如何实现Http Endpoints 安全加固

如果项目中使用了 spring security 或 shiro 等安全框架,可以把EP放在安全框架之后,和其他业务API一样保护起来。

或者,也可以使用RequestMatcher对象来配合使用,以控制某些用户具备访问权限,比如:

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

@Configuration(proxyBeanMethods = false)
public class MySecurityConfiguration {

@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http.requestMatcher(EndpointRequest.toAnyEndpoint())
.authorizeRequests((requests) -> requests.anyRequest().hasRole("ENDPOINT_ADMIN"));
http.httpBasic();
return http.build();
}
}

这样只有具备 ENDPOINT_ADMIN 角色的用户才可以访问EP。

如何通过页面访问各类指标

通过项目的 ip:port /actuator ,比如 localhost:8080/actuator:

可以通过配置修改路径:

# 修改 /actutor  /manage
management.endpoints.web.base-path=/manage

# 修改特定指标 /health /myhealth
management.endpoints.web.path-mapping.health=myhealth

# 修改访问端口(默认和server.port相同)
management.server.port=8036
责任编辑:姜华 来源: 今日头条
相关推荐

2022-02-09 20:39:52

Actuator应用监控

2022-06-22 08:02:01

业务监控Web站点监控

2017-01-19 18:06:19

听云Network

2017-04-19 14:23:08

项目管理分配

2011-05-26 16:27:24

SEO

2020-07-22 07:00:00

微服务架构

2019-04-29 09:52:46

容器安全漏洞网络安全

2019-08-19 09:01:54

项目管理

2023-04-11 16:04:19

Spring Boo端点运维

2021-01-19 09:59:02

招聘管理团队

2013-07-10 09:22:59

云配置云实践云应用程序接口

2011-04-18 13:20:40

单元测试软件测试

2022-01-04 09:01:10

开源项目开源技术

2016-01-13 13:13:29

运维监控工具

2010-09-07 16:09:29

2022-05-07 19:18:16

防御性编码代码

2020-02-05 14:49:04

网络性能优化微调

2014-10-30 10:53:22

Android内存优化

2011-05-19 10:20:23

2013-01-22 11:10:11

点赞
收藏

51CTO技术栈公众号