SpringBoot Actuator监控

307 阅读3分钟

指标监控

在日常开发中,如果需要监控,查看SpringBoot程序内的URL,有哪些Bean,配置项,健康度,运行指标,日志信息,线程状态...,SpringBoot为我们提供了SpringBootActuator为我们进行监控

1.端点介绍

1.端点分类

1.信息类端点

端点名称默认是否开启HTTP端点说明
auditevents提供系统的审计信息
beans提供系统的Bean列表
caches提供系统的缓存信息
conditions提供配置类的匹配信息以及条件运算结果
configprops提供@ConfigurationProperties的列表
env提供ConfigurableEnvironment中的属性信息
flyway提供已执行的Flyway数据库迁移信息
httptrace提供HTTP跟踪信息,默认最近的100条
info显示事先设置好的系统信息
integrationgraph提供Spring Integration图信息
liquibase提供已执行的Liquibase数据库迁移信息
logfile如果设置了Logging.file.name/logging.file.path属性,则显示日志文件内容
mappings提供@RequestMapping的映射列表
scheduledtasks提供系统中的调度任务列表

2.监控类端点

端点默认是否开启HTTP端点说明
health提供系统运行的健康状态
metrices提供系统的度量信息
prometheus提供Prometheus系统可解析的度量信息

3.操作类端点

端点默认是否开启HTTP端点说明
heapdump执行Heap Dump操作
loggers查看并修改日志信息
sessions针对使用了SpringSession的系统,可以获取/删除用户的Session
shutdown关闭系统
threaddump执行Thread Dump操作

4.集成类端点

端点默认是否开启HTTP端点说明
jolokia通过HTTP来发布JMX Bean

2.使用

1.依赖

    <!--指标监控-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>

2.访问端点

http://localhost:8083/actuator

3.配置监控端点

1.开启/禁用端点

默认情况下,除了shutdown端点以外,其余所有的端点都是处于开启状态,只是访问的状态不同,或者保护状态不同,如果要开启/禁用某个端点,可以如下配置

management:
  endpoint:
    "端点名称"
      enabled: false/true
#例如
management:
  endpoint:
    shutdown:
      enabled: false

禁用所有端点

management:
  endpoints:
    enabled-by-default: false

禁用所有端点后开启某个端点

#禁用所有端点后开启health端点
management:
  endpoints:
    enabled-by-default: false
  endpoint:
    health:
      enabled: true

image-20230817232126222.png

templated:表示有无占位符,可以使用具体的值去代替{}里面的内容

2.通过HTTP访问端点

可以使用如下配置来控制哪些端点是可以通过HTTP的方式发布,哪些端点是不行的

management:
  endpoints:
    web:
      exposure:
      #放开所有的端点
        include: "*"

image-20230817234653268.png

management:
  endpoints:
    enabled-by-default: false
    web:
      exposure:
      #排除所有的端点
        exclude: "*"

image-20230817234847752.png

注:如果同一个端点同时出现在include和exclude中,前者的优先级高于后者

如果需要使用HTTP端点去访问,需要有Web支持,因此需要引入

    <!--web依赖-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

3.图形界面使用

1.使用SpringBoot Admin图形界面

1.显示模块

  1. 创建SpringBoot Admin Server应用

    可以在项目中添加模块

image-20230818003003923.png

image-20230818002937847.png

引入Spirng-boot-admin-starter-server依赖

```xml
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.4.0</version>
        </dependency>
```

在application.yaml中配置端口号

```yaml
server:
  port: 8888
```

在主启动类上添加@EnableAdminServer

```java
package com.jiasen.admin;

import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@EnableAdminServer
@SpringBootApplication
public class AdminApplication {

    public static void main(String[] args) {
        SpringApplication.run(AdminApplication.class, args);
    }

}
```

2.控制模块

  1. 配置SpringBoot Admin client

    添加依赖

        <!--springboot-admin-->
        <dependency>
          <groupId>de.codecentric</groupId>
          <artifactId>spring-boot-admin-starter-client</artifactId>
          <version>2.4.0</version>
        </dependency>
    

    在application.yaml中配置SpringBoot Admin Server的地址

    spring:
      boot:
        admin:
          client:
          #SpringBoot Admin Server的地址
            url: http://localhost:8888
    

    配置完成后就可以在http://localhost:8888端口中访问了

image-20230818003729768.png

image-20230818003750629.png

image-20230818003707754.png

这样就不用直接看json数据,可以使用图形界面来使用了