天天看点

(十五)Spring Boot 应用监控 —— 《一步一步学 Spring Boot 2》读书笔记一、应用监控二、使用应用监控三、自定义 EndPoint四、自定义 HealthIndicator五、保护 Actuator 端点

本文纯个人读书笔记,书籍《一步一步学 Spring Boot 2》

如果喜欢,可直接购买书籍。如有侵权,请联系删除

一、应用监控

Spring Boot 提供的大部分模块都是用于开发业务功能或者连接外部资源 。 除此之外,Spring Boot 还提供 了 spring-boot-starter-actuator 模块,该模块主要用于管理和监控应用,是一个暴露自身信息的模块 。

spring-boot-starter-actuator 模块可以有效地减少监控系统在采集应用指标时的开发量。spring-boot-starter- actuator 模块提供了监控和管理端点以及 一 些常用的扩展和配置方式 。

监控和管理端点:

(十五)Spring Boot 应用监控 —— 《一步一步学 Spring Boot 2》读书笔记一、应用监控二、使用应用监控三、自定义 EndPoint四、自定义 HealthIndicator五、保护 Actuator 端点

二、使用应用监控

1.引入依赖

在 pom.xml 中添加依赖。

pom.xml:

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

2.添加配置

在 application.properties 中添加对应的配置。

application.properties:

# 设置单个端点(/shutdown)可用
management.endpoint.shutdown.enabled=true
# Actuator 默认所有的监控点路径都在/actuator/*,当然如果有需要这个路径也支持定制。
management.endpoints.web.base-path=/manage
#为了安全起见,Actuator 只开放了两个端点 /actuator/health 和 /actuator/info。可以在配置文件中设置打开。
management.endpoints.web.exposure.include=*
# 显示详细健康信息
management.endpoint.health.show-details=always
           

这边 Spring Boot 为 2.0 的,所以与书上的配置不一样。

3.测试

启动应用, 访问 http://127.0.0.1:8080/demo/manage/health 进行查看。

(十五)Spring Boot 应用监控 —— 《一步一步学 Spring Boot 2》读书笔记一、应用监控二、使用应用监控三、自定义 EndPoint四、自定义 HealthIndicator五、保护 Actuator 端点

三、自定义 EndPoint

spring-boot- starter- actuator 模块中己经提供了许多原生端点。根据端点的作用 ,我们可以把原生端点分为以下三大类。

( 1 )应用配置类 : 获取应用程序中加载的应用配置、环境变量 、 自动化配置报告等与 Spring Boot 应用密切相关的配置类信息 。

( 2 )度量 指 标类 : 获取应用程序运行过程中用于监控的度量指标,比 如内存信息、线程池信息、 HTTP 请求统计等。

( 3 )操作控制类: 提供对应用的关闭等操作类功能。

如果 spring-boot-starter-actuator 模块提供的这些端点无法满足需求,我们还可以进行自定义端点的开发。

1.自定义 EndPoint

新建自定义 EndPoint com.xiaoyue.demo.actuator.UserEndPoint 来监控数据库用户信息情况, 比如用户总数量、被删除用户数量、活跃用户数量等。

UserEndPoint:

@Configuration
@Endpoint(id = "my-endpoint")
public class UserEndPoint {

    @Resource
    private UserService userService;

    @ReadOperation
    public Map<String, Object> endpoint() {
        Map<String, Object> map = new HashMap<>(16);
        map.put("message", "this is my endpoint");
        //当前时间
        map.put("current_time", new Date());
        //用户总数量
        map.put("user_num", userService.findUserTotalNum());
        return map;
    }
}
           

2.service

添加对应的 service 层代码。

UserService:

Long findUserTotalNum();
           

UserServiceImpl:

@Override
    public Long findUserTotalNum() {
        return userRepository.count();
    }
           

3.测试

启动应用,访问 http://127.0.0.1:8080/demo/manage/my-endpoint 进行测试。

(十五)Spring Boot 应用监控 —— 《一步一步学 Spring Boot 2》读书笔记一、应用监控二、使用应用监控三、自定义 EndPoint四、自定义 HealthIndicator五、保护 Actuator 端点

四、自定义 HealthIndicator

默认端点 Health 的信息是从 Healthlndicator 的 bean 中收集 的, Spring 中内置了一些 Healthlndicator 。

(十五)Spring Boot 应用监控 —— 《一步一步学 Spring Boot 2》读书笔记一、应用监控二、使用应用监控三、自定义 EndPoint四、自定义 HealthIndicator五、保护 Actuator 端点

在上面我们可以知道,我们访问 http://127.0.0.1:8080/demo/manage/health 查看健康信息。

(十五)Spring Boot 应用监控 —— 《一步一步学 Spring Boot 2》读书笔记一、应用监控二、使用应用监控三、自定义 EndPoint四、自定义 HealthIndicator五、保护 Actuator 端点

这些是系统提供的信息,有时候我们想添加自己的一些信息。

1.自定义 HealthIndicator

新建自定义 HealthIndicator com.xiaoyue.demo.actuator.MyHealthlndicator 来添加自定义的健康信息。

MyHealthlndicator:

@Component
public class MyHealthlndicator implements HealthIndicator {

    @Override
    public Health health() {
        Long totalSpace = checkTocalSpace();
        Long free= checkFree() ;
        String status = checkStatus( );

        return new Health.Builder()
                . up()
                . withDetail("status", status)
                . withDetail("total", totalSpace)
                . withDetail("free", free )
                . build() ;
    }

    private String checkStatus(){
        //结合真实项目获取相关参数
        return "UP";
    }
    private Long checkTocalSpace() {
        //结合真实项目获取相关参数
        return 10000L;
    }

    private Long checkFree () {
        //结合真实项目获取相关参数
        return 5000L;
    }
}
           

2.测试

启动应用,访问 http://127.0.0.1:8080/demo/manage/health 进行测试。

(十五)Spring Boot 应用监控 —— 《一步一步学 Spring Boot 2》读书笔记一、应用监控二、使用应用监控三、自定义 EndPoint四、自定义 HealthIndicator五、保护 Actuator 端点

五、保护 Actuator 端点

Actuator 端点发布的信息很多都涉及敏感信息和高危操作,所以,我们必需控制 Actuator 端点的访问权限来保护 Actuator 端点被非法访问 。可以像保护其他 URL 路径一样 , 通过使用 Spring Security 来控制 URL 路径的授权访 问 。

继续阅读