Spring boot Actuator监控管理的快速入门和实战

1、Spring Boot Actuator的介绍

Spring Boot Actuator是Spring Boot提供的一个用于监控和管理Spring Boot应用程序的功能模块。

你可以选择通过使用HTTP端点或使用JMX来管理和监控你的应用程序。 审计、健康和指标收集也可以自动应用于你的应用程序。

Actuator的定义:

actuator(执行器) 是一个制造术语,指的是用于移动或控制某物的机械装置。actuator 可以从一个小的变化中产生大量的运动。

Actuator的官网为:
生产就绪功能

2、Spring Boot Actuator的快速实战

使用的技术栈版本为:spring boot3.1.0

2.1、新建一个Actuator-parent的maven聚合工程,然后在这个聚合工程下完成我们对于Spring Boot Actuator的实战入门;

在Actuator-parent下新建一个actuator-test模块,在这个模块中引入Spring Boot Actuator的依赖,并启动这个模块;

 <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>

直接启动actuator-test模块,并访问http://localhost:8080/actuator路径,可以看到actuator默认暴露的一些端口:

默认情况下,只有health端点是通过HTTP暴露的。 由于端点可能包含敏感信息,你应该仔细考虑何时暴露它们。

要改变哪些端点被暴露,可以在yml配置文件中修改 include 和 exclude 属性。

include 属性列出了被暴露的端点的ID。 exclude 属性列出了不应该被公开的端点的ID。 exclude 属性优先于 include 属性。 你可以用一个端点ID列表来配置 include 和 exclude 属性。

暴露所有的端点:
 

management:
  endpoints:
    web:
      exposure:
        include: '*'
server:
  port: 8080

actuator默认的端点有:

auditevents

公开当前应用程序的审计事件信息。 需要一个 AuditEventRepository bean。

beans

显示你的应用程序中所有Spring Bean的完整列表。

caches

显示可用的缓存。

conditions

显示对配置和自动配置类进行评估的条件,以及它们符合或不符合的原因。

configprops

显示所有 @ConfigurationProperties 的整理列表。

env

暴露Spring的 ConfigurableEnvironment 中的属性。

flyway

显示任何已经应用的Flyway数据库迁移。 需要一个或多个 Flyway bean。

health

显示应用程序的健康信息。

httpexchanges

显示 HTTP exchange 信息(默认情况下,最后 100 个 HTTP request/response exchange)。 需要一个 HttpExchangeRepository bean。

info

显示任意的应用程序信息。

integrationgraph

显示Spring集成图。 需要依赖 spring-integration-core

loggers

显示和修改应用程序中logger的配置。

liquibase

显示任何已经应用的Liquibase数据库迁移。 需要一个或多个 Liquibase Bean。

metrics

显示当前应用程序的 “metrics” 信息。

mappings

显示所有 @RequestMapping 路径的整理列表。

quartz

显示有关Quartz Scheduler Job的信息。

scheduledtasks

显示你的应用程序中的计划任务。

sessions

允许从Spring Session支持的会话存储中检索和删除用户会话。 需要一个使用Spring Session的基于Servlet的Web应用程序。

shutdown

让应用程序优雅地关闭。只在使用jar打包时有效。默认情况下是禁用的。

startup

显示由 ApplicationStartup 收集的启动步骤数据。要求 SpringApplication 被配置为 BufferingApplicationStartup

threaddump

Performs a thread dump.

2.2、可视化工具spring-boot-admin

我们当然可以使用默认的actuator来进行我们项目的追踪,但是这样只能通过路径来监控我们的项目,这样就不太方便与我们实时的进行监控。

spring-boot-admin可视化监控平台,是一个基于Spring Boot Actuator端点之上的一个vue.js应用程序,可以帮助我们动态的看到spring boot项目的变化。

spring-boot-admin的GitHub地址:

Spring Boot Admin – Getting started

由于spring-boot-admin是一个单独的服务,所以我们需要在Actuator-parent聚合工程下新建一个admin模块。(注意spring-boot-admin的版本要与spring-boot-actuator一致)

<dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-server</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

在启动类上加上@EnableAdminServer注解

在client模块加上spring-boot-admin-client依赖,并在yml配置文件中声明spring-boot-admin-server的地址。

 <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-client</artifactId>
            <version>3.1.0</version>
        </dependency>
spring:
  boot:
    admin:
      client:
        url: http://localhost:9090

现在,将两个模块都启动,直接访问server模块的地址,就可以看到client模块被注入到server模块中了。

现在,我们已经启动了admin模块和actuator模块并成功监控到了actuator模块,但是,现在这个admin模块是所有人都可以访问的,这不安全,所以我们可以添加一些安全措施。

2.2.3整合nacos注册中心

当我们在微服务项目中要监控所有的服务,那么使用注册中心是个很好的选择,我们只需要将admin这个模块注册到nacos中,那么所有曾经使用过admin客户端的服务都会自动被监控到

如果你不了解nacos服务注册,可以看一下这篇文章:
springboot3整合nacos实现注册中心和配置中心(详细入门)_springboot3 nacos-CSDN博客

相关推荐

  1. RocketMQ监控管理工具有哪些❓

    2024-04-10 12:34:04       62 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-10 12:34:04       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-10 12:34:04       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-10 12:34:04       87 阅读
  4. Python语言-面向对象

    2024-04-10 12:34:04       96 阅读

热门阅读

  1. 2023第十四届蓝桥杯省赛C/C++大学A组题解

    2024-04-10 12:34:04       34 阅读
  2. leetcode2529-正整数和负整数的最大计数

    2024-04-10 12:34:04       32 阅读
  3. Vue文档

    Vue文档

    2024-04-10 12:34:04      26 阅读
  4. LeetCode 15.三数之和

    2024-04-10 12:34:04       35 阅读