当前位置:首页 > 技术 > 正文内容

使用 Spring Boot Admin 进行应用监控和管理

访客 技术 2026年9月15日 12

Spring Boot Admin (SBA) 是一个开源工具,专为 Spring Boot 应用提供全面的监控和管理能力。它允许应用程序通过 HTTP 或 Spring Cloud 的服务发现机制向 SBA 注册,从而实现对 Spring Boot 项目的可视化操作和状态查看。

主要功能

  • 监控 Spring Boot 应用的性能指标。
  • 查看进程线程数量及 CPU 资源占用情况。
  • 获取 Spring Boot 运行环境的详细信息。
  • 展示和管理 Spring Boot 应用中的定时任务。
  • 查看和调整 Spring Boot 应用的日志级别。
  • 提供 JVM 线程和内存转储的详细信息,支持文件下载。
  • 显示应用的启动时间和累计运行时间。
  • 分析垃圾回收的详细数据,包括回收次数和耗时。
  • 查看和管理 Spring Boot 应用中的所有缓存。
  • 列出 Spring Boot 应用的所有类信息。

集成步骤

搭建 Spring Boot Admin 服务器端

在 SBA 服务器项目的 pom.xml 文件中添加以下依赖:


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

在 SBA 服务器主类上添加 @EnableAdminServer 注解:


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

@SpringBootApplication
@EnableAdminServer // 启用 Admin Server 功能
public class AdminServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(AdminServerApplication.class, args);
    }
}

启动 SBA 服务器后,可通过 http://{服务器IP}:{服务器端口}/applications 访问 SBA 管理界面。

集成被监控的应用

方法一:直接配置 Admin 服务器地址

在被监控应用的 pom.xml 文件中添加客户端依赖:


<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-client</artifactId>
</dependency>

在被监控应用的 application.properties (或 application.yml) 文件中配置 SBA 服务器地址:


# 被监控应用自身的端口
server.port=8081
# Spring Boot Admin 服务器地址
spring.boot.admin.client.url=http://localhost:9001

方法二:通过服务注册中心集成 (推荐)

此方法允许客户端从服务注册中心动态发现并注册到 Admin 服务器,无需在每个客户端中硬编码 Admin 地址。

首先,确保被监控应用已集成服务注册中心客户端(例如 Nacos):


<!-- Spring Cloud Alibaba Nacos Discovery -->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

在被监控应用的 application.yml 文件中配置应用信息和服务注册中心地址:


spring:
  application:
    # 应用的唯一名称,用于服务注册
    name: my-monitored-app
  cloud:
    nacos:
      discovery:
        # Nacos 服务注册中心地址
        server-addr: 127.0.0.1:8848

在这种模式下,SBA 服务器会从服务注册中心拉取注册的应用实例进行管理。

启用更多监控指标

为了暴露 Spring Boot Actuator 的所有监控端点,请添加 spring-boot-starter-actuator 依赖:


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

在被监控应用的 application.properties (或 application.yml) 文件中配置暴露所有 Actuator 端点:


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

添加此配置后,SBA 将能够获取更丰富的监控数据。

相关文章

Linux crontab 详解

1) crontab 是什么cron 是 Linux 的定时任务守护进程;crontab 是用来编辑/查看“按时间周期执行命令”的表(cron table)。常见两类:用户 crontab:每个用户一份(crontab -e 编辑)系统级 crontab / cron.d:可指定执行用户(/etc/crontab、/etc/cron.d/*)2) crontab 时间...

富文本里可以允许的 HTML 属性

一、所有标签默认允许的安全属性(极少)class        (可选)id           (通常建议禁用)title️ 注意:id 容易被滥用做锚点注入,很多系统直接禁用class 允许的话最好只允许固定前缀(如 editor-*)二、a 标签允许属性<a href="" t...

Dom\HTML_NO_DEFAULT_NS 的副作用:自动加闭合标签

在使用Dom\HTMLDocument时,Dom\HTML_NO_DEFAULT_NS 将禁止在解析过程中设置元素的命名空间, 此设置是为了与DOMDocument向后兼容而存在的。当使用它时,已知的一个副作用就是:自动加闭合标签例如 </img> 为什么会这样?当你使用:Dom\HTML_NO_DEFAULT_NS文档会变成 无命名空间模式,此时内部更接近 XML...

Laravel 事件和监听器创建

在 Laravel 中,使用 Artisan 命令创建 Events(事件) 和 Listeners(监听器) 是非常高效的。你可以通过以下几种方式来实现:1. 手动创建单个 Event如果你只想创建一个事件类,可以使用 make:event 命令:Bashphp artisan make:event UserRegistered执行后,文件将生成在 app/Even...

自定义域名解析神器 dnsmasq

什么是 dnsmasq?dnsmasq 是一个轻量级、功能强大的网络服务工具,专为小型和中等规模网络设计。它是一个综合的网络基础设施解决方案[1]。dnsmasq 能做什么?功能说明应用场景DNS 转发与缓存将 DNS 查询转发到上游服务器(ISP、Google DNS 等),并在本地缓存结果加快 DNS 查询速度,减少外部 DNS 流量本地 DNS解析本地网络设备的主机名,无需编辑&n...

linux screen 用法详情 (nohup 的替代方案)

一、screen 是什么?能干嘛?screen 是一个终端复用器,可以:在一个 SSH 会话中开多个“虚拟终端”SSH 断线后,程序仍然在后台运行随时重新连接到原来的会话特别适合:nohup 的替代方案跑脚本 / 爬虫 / 训练模型运维、远程开发二、安装 screen# CentOS / Rocky / Almayum install -y screen# Debian / Ubuntuapt i...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。