『Spring Boot Actuator Spring Boot Admin』 实现应用监控管理

news2024/9/25 2:26:20

前言

本文将会使用 Spring Boot Actuator 组件实现应用监视和管理,同时结合 Spring Boot Admin 对 Actuator 中的信息进行界面化展示,监控应用的健康状况,提供实时警报功能

Spring Boot Actuator

简介

官方文档:Production-ready Features (spring.io)

Actuator 的核心是端点(Endpoint),它用来监视、提供应用程序的信息,Spring Boot 提供的 spring-boot-actuator 组件中已经内置了非常多的 Endpointhealth、info、beans、metrics、httptrace、shutdown 等),每个端点都可以启用和禁用。Actuator 也允许我们扩展自己的端点。通过 JMX 或 HTTP 的形式暴露自定义端点

注:查看全部 Endpoints 请参照上方的官方文档

Actuator 会将自定义端点的 ID 默认映射到一个带 /actuator 前缀的 URL。比如,health 端点默认映射到 /actuator/health。这样就可以通过 HTTP 的形式获取自定义端点的数据,许多网关作为反向代理需要 URL 来探测后端集群应用是否存活,这个 URL 就可以提供给网关使用

启动端点

默认情况下,除shutdown之外的所有终结点都处于启用状态。若要配置终结点的启用,请使用其  management.endpoint.<id>.enabled  属性。以下示例启用终结点  shutdown

management:
  endpoint:
    shutdown:
      enabled: true

如果您希望端点启用选择加入而不是选择退出,请将该  management.endpoints.enabled-by-default  属性设置为  false  并使用单个端点  enabled  属性选择重新加入。以下示例启用该  info  终结点并禁用所有其他终结点:

management:
  endpoints:
    enabled-by-default: false
  endpoint:
    info:
      enabled: true

注:禁用的端点将从应用程序上下文中完全删除。如果只想更改公开终结点的技术,请改用  include  和  exclude  属性。

公开端点

禁用的端点将从应用程序上下文中完全删除。如果只想更改公开终结点的技术,请改用 include 和 exclude 属性。若要更改公开的终结点,请使用以下特定于 include 技术的 exclude 属性

include 属性列出了公开的终结点的 ID。exclude 属性列出了不应公开的终结点的 ID。 exclude 属性优先于该 include 属性。您可以使用端点 ID 列表配置属性和 exclude 属性 include 。

例如,要仅通过 JMX 公开  health  和  info  端点,请使用以下属性

management:
  endpoints:
    jmx:
      exposure:
        include: "health,info"

*可用于选择所有端点。例如,若要通过 HTTP 公开除 env 和 beans 终结点之外的所有内容,请使用以下属性

management:
  endpoints:
    web:
      exposure:
        include: "*"
        exclude: "env,beans"

Actuator 同时还可以与外部应用监控系统整合,比如 Prometheus, Graphite, DataDog, Influx, Wavefront, New Relic 等。这些系统提供了非常好的仪表盘、图标、分析和告警等功能,使得你可以通过统一的接口轻松的监控和管理你的应用系统。这对于实施微服务的中小团队来说,无疑快速高效的解决方案

配置集成

首先我们需要创建 springboot web 项目,然后 pom.xml 中添加如下 actuator 依赖

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

配置 application.yml

server:
  port: 8080

management:
  endpoints:
    enabled-by-default: false
    web:
      base-path: /manage
      exposure:
        include: 'info,health,env,beans'
  endpoint:
    info:
      enabled: true
    health:
      enabled: true
    env:
      enabled: true
    beans:
      enabled: true

上述配置只暴露 info,health,env,beans 四个 endpoints, web 通过可以 /manage 访问

访问:localhost:8080/manage 查看所有开放的端点

{
    "_links": {
        "self": {
            "href": "http://localhost:8080/manage",
            "templated": false
        },
        "beans": {
            "href": "http://localhost:8080/manage/beans",
            "templated": false
        },
        "health": {
            "href": "http://localhost:8080/manage/health",
            "templated": false
        },
        "health-path": {
            "href": "http://localhost:8080/manage/health/{*path}",
            "templated": true
        },
        "info": {
            "href": "http://localhost:8080/manage/info",
            "templated": false
        },
        "env": {
            "href": "http://localhost:8080/manage/env",
            "templated": false
        },
        "env-toMatch": {
            "href": "http://localhost:8080/manage/env/{toMatch}",
            "templated": true
        }
    }
}

访问:localhost:8080/manage/beans

image.png

拓展配置

安全性

当我们想要暴露更多接口,同时保证 endpoint 接口安全,可以与 Spring Security 集成

@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();
    }

}

此外,如果存在 Spring Security,同时你需要添加自定义安全配置,以允许对端点进行未经身份验证的访问,如以下示例所示

@Configuration(proxyBeanMethods = false)
public class MySecurityConfiguration {

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http.securityMatcher(EndpointRequest.toAnyEndpoint());
        http.authorizeHttpRequests((requests) -> requests.anyRequest().permitAll());
        return http.build();
    }

}
跨域访问
management:
  endpoints:
    web:
      cors:
        allowed-origins: "https://example.com"
        allowed-methods: "GET,POST"
自定义端点

我们可以通过@JmxEndpoint or @WebEndpoint 注解来定义自己的 endpoint, 然后通过@ReadOperation, @WriteOperation 或者@DeleteOperation 来暴露操作

比如添加系统时间 date 的 endpoint

@RestController("custom")
@WebEndpoint(id = "date")
public class CustomEndpointController {

    @ReadOperation
    public ResponseEntity<String> currentDate() {
        return ResponseEntity.ok(LocalDateTime.now().toString());
    }
}
management:
  endpoints:
    enabled-by-default: false
    web:
      base-path: /manage
      exposure:
        include: 'info,health,env,beans,date'
  endpoint:
    info:
      enabled: true
    health:
      enabled: true
    env:
      enabled: true
    beans:
      enabled: true
    date:
      enabled: true
info 不显示

我们直接访问 info 接口是空的

问题出处官方文档:Production-ready Features (spring.io)

image.png

解决方案,修改 application.yml 如下

management:
  endpoint:
    info:
      env:
        enabled: true

Spring Boot Admin

简介

官方仓库:codecentric/spring-boot-admin

官方文档:Spring Boot Admin – (spring-boot-admin.com)

Spring Boot Admin(简称 SBA)由两部分组成:SBA Server 和 SBA Client

image.png

SBA Server: 包括 Admin 用户界面并独立运行于被监控应用

SBA Client: 提供一种方式将被监控应用注册到 SBA Server

SBA 分为服务端和客户端原理:因为 SBA 需要做集中化的监控(比如应用的集群,多个服务或者微服务等),而不是每个应用都需要有一个 UI。同时被监控的应用应该是和监控平台是分离的,并且需要考虑其他语言和其他平台的集成

除此之外,SBA Client 不仅只可以注册到 SBA Server ,还可以注册到 Spring Cloud Discovery(微服务),Python Applications Using Pyctuator(其他语言)等平台

启用 Server

pom.xml 配置

<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-server</artifactId>
    <version>2.5.3</version>
</dependency>

注意:这里我们必须添加 <version> 字段,因为父模块 spring-boot-starter-parent 中的 BOM(Bill of Material) 并没有配置 SBA 的 version,无法自动识别

通过 @EnableAdminServer 注解启用 SBA Server

@Configuration
@EnableAdminServer
@SpringBootApplication
public class SpringBootActuatorDemoApplication {

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

}

访问:Spring Boot Admin

image.png

注册 Client

引入 SBA Client 依赖

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

配置 application.yml

server:
  port: 8080

management:
  endpoints:
    enabled-by-default: false
    web:
      base-path: /manage
      exposure:
        include: 'info,health,env,beans'
  endpoint:
    info:
      env:
        enabled: true
      enabled: true
    health:
      enabled: true
    env:
      enabled: true
    beans:
      enabled: true
# 添加如下配置
spring:
  boot:
    admin:
      client:
        url: 'http://localhost:8080'

访问:Spring Boot Admin

image.png

之后点击进入实例,可以自行探索监控信息

image.png

其他问题

启用 JMX 管理

默认下 SBA 没有启用 JMX,需要通过如下配置启用。

首先需要引入 POM 依赖(PS:需要 SpringBoot2.2+ 版本)

<dependency>
    <groupId>org.jolokia</groupId>
    <artifactId>jolokia-core</artifactId>
</dependency>

yml 配置

spring:
  jmx:
    enabled: true
显示日志内容

默认下没有显示 Log File 的内容,如果需要显示 SpringBoot 应用日志需要进行如下配置(配置 logging.file.path 或者 logging.file.name)

logging:
  file:
    name: 'pdai-spring-boot-application.log'
  pattern:
    file: '%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%5p) %clr(${PID}){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n%wEx'
继承 Spring Security
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
@Configuration
public static class SecurityPermitAllConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().anyRequest().permitAll()  
            .and().csrf().disable();
    }
}
通知告警信息

集成 spring-boot-starter-mail 配置 JavaMailSender 来用邮件通知信息

官方文档对应链接:Spring Boot Admin – (spring-boot-admin.com)

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>
spring:
    mail:
        host:smtp.example.com
    boot:
        admin:
            notify:
                mail:
                    to:admin@example.com

注:更多通知方式(钉钉,微信等)可以直接参考上方官方文档

补充

在生产环境下,使用 Prometheus + Grafana 组合也是非常推荐的监控解决方案,这里篇章有限,读者可以自行探索

参考链接

  • SpringBoot 监控 - 集成 actuator 监控工具 | Java 全栈知识体系 (pdai.tech)
  • SpringBoot 监控 - 集成 springboot admin 监控工具 | Java 全栈知识体系 (pdai.tech)
  • 微服务系列:服务监控 Spring Boot Actuator 和 Spring Boot Admin - 掘金 (juejin.cn)
  • 实战:使用 Spring Boot Admin 实现运维监控平台-阿里云开发者社区 (aliyun.com)
  • Prometheus 快速入门教程(六):Spring Boot Actuator 实现应用监控
  • Prometheus简介 - prometheus-book (gitbook.io)

本文由博客一文多发平台 OpenWrite 发布!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1227936.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

RocketMQ(三):集成SpringBoot

RocketMQ系列文章 RocketMQ(一)&#xff1a;基本概念和环境搭建 RocketMQ(二)&#xff1a;原生API快速入门 RocketMQ(三)&#xff1a;集成SpringBoot 目录 一、搭建环境二、不同类型消息1、同步消息2、异步消息3、单向消息4、延迟消息5、顺序消息6、带tag消息7、带key消息 一…

【IPC】 共享内存

1、概述 共享内存允许两个或者多个进程共享给定的存储区域。 共享内存的特点 1、 共享内存是进程间共享数据的一种最快的方法。 一个进程向共享的内存区域写入了数据&#xff0c;共享这个内存区域的所有进程就可以立刻看到 其中的内容。 2、使用共享内存要注意的是多个进程…

如何定位el-tree中的树节点当父元素滚动时如何定位子元素

使用到的方法 Element 接口的 scrollIntoView() 方法会滚动元素的父容器&#xff0c;使被调用 scrollIntoView() 的元素对用户可见。 参数 alignToTop可选 一个布尔值&#xff1a; 如果为 true&#xff0c;元素的顶端将和其所在滚动区的可视区域的顶端对齐。相应的 scrollIntoV…

C语言入门笔记—static、extern、define、指针、结构体

一、static static修饰局部变量的时候&#xff0c;局部变量出了作用域&#xff0c;不销毁。本质上&#xff0c;static修饰局部变量的时候&#xff0c;改变了变量的存储位置。详见下图&#xff0c;当a不被static修饰和被static修饰的时候。 C/C static关键字详解&#xff…

随机过程-张灏

文章目录 导论随机过程相关 学习视频来源&#xff1a;https://www.bilibili.com/video/BV18p4y1u7NP?p1&vd_source5e8340c2f2373cdec3870278aedd8ca4 将持续更新—— 第一次更新&#xff1a;2023-11-19 导论 教材&#xff1a;《随机过程及其应用》陆大絟.张颢 参考&…

CD36 ; + Lectin;

CD2 LIMP-2&#xff0c; LGP85 SR-BI&#xff0c; CD36&#xff1b; 清道夫受体蛋白CD36超家族的成员是 脂质代谢 和 先天免疫 的重要调节因子。它们识别正常和修饰的脂蛋白&#xff0c;以及与病原体相关的分子模式。 该家族由三个成员组成&#xff1a; SR-BI &am…

ZJU Beamer学习手册(二)

ZJU Beamer学习手册基于 Overleaf 的 ZJU Beamer模板 进行解读&#xff0c;本文则基于该模版进行进一步修改。 参考文献 首先在frame文件夹中增加reference.tex文件&#xff0c;文件内容如下。这段代码对参考文献的引用进行了预处理。 \usepackage[backendbiber]{biblatex} \…

异常语法详解

异常语法详解 一&#xff1a;异常的分类&#xff1a;二&#xff1a;异常的处理1:异常的抛出:throw2&#xff1a;异常的声明:throws3&#xff1a;try-catch捕获并处理异常 三&#xff1a;finally关键字四&#xff1a;自定义异常类&#xff1a; 一&#xff1a;异常的分类&#xf…

电路的基本原理

文章目录 一、算数逻辑单元(ALU)1、功能2、组成 二、电路基本知识1、逻辑运算2、复合逻辑 三、加法器实现1、一位加法器2、串行加法器3、并行加法器 一、算数逻辑单元(ALU) 1、功能 算术运算&#xff1a;加、减、乘、除等 逻辑运算&#xff1a;与、或、非、异或等 辅助功能&am…

对vb.net 打印条形码code39、code128A、code128C、code128Auto(picturebox和打印机)封装类一文的补充

在【精选】vb.net 打印条形码code39、code128A、code128C、code128Auto&#xff08;picturebox和打印机&#xff09;封装类_vb.net打印标签_WormJan的博客-CSDN博客 这篇文章中&#xff0c;没有对含有字母的编码进行处理。这里另开一篇帖子&#xff0c;处理这种情况。 在那篇文…

【C++入门】拷贝构造运算符重载

目录 1. 拷贝构造函数 1.1 概念 1.2 特征 1.3 常用场景 2. 赋值运算符重载 2.1 运算符重载 2.2 特征 2.3 赋值运算符 前言 拷贝构造和运算符重载是面向对象编程中至关重要的部分&#xff0c;它们C编程中的一个核心领域&#xff0c;本期我详细的介绍拷贝构造和运算符重载。 1. …

Js中clientX/Y、offsetX/Y和screenX/Y之间区别

Js中client、offset和screen的区别 前言图文解说实例代码解说 前言 本文主要讲解JavaScript中clientX、clientY、offsetX、offsetY、screenX、screenY之间的区别。 图文解说 在上图中&#xff0c;有三个框&#xff0c;第一个为屏幕&#xff0c;第二个为浏览器大小&#xff0c…

约数个数定理

首先在讲这个定理前&#xff0c;首先科普一下前置知识 约数&#xff1a; 何为约数&#xff0c;只要能整除n的整数就是n的约数&#xff0c;举个例子&#xff0c;3的约束是1和3因为1和3能整除3 质数&#xff1a; 除了这个数字本身和1以外没有其他因子的数字就叫质数&#xff…

AVL树和红黑树

AVL树和红黑树 一、AVL树1. 概念2. 原理AVL树节点的定义插入不违反AVL树性质违反AVL树性质左单旋右单旋左右双旋右左双旋总结 删除 3. 验证代码4. AVL树完整实现代码 二、红黑树1. 概念2. 性质3. 原理红黑树节点的定义默认约定插入情况一 &#xff08;u存在且为红&#xff09;情…

论文速览 Arxiv 2023 | DMV3D: 单阶段3D生成方法

注1:本文系“最新论文速览”系列之一,致力于简洁清晰地介绍、解读最新的顶会/顶刊论文 论文速览 Arxiv 2023 | DMV3D: DENOISING MULTI-VIEW DIFFUSION USING 3D LARGE RECONSTRUCTION MODEL 使用3D大重建模型来去噪多视图扩散 论文原文:https://arxiv.org/pdf/2311.09217.pdf…

【2017年数据结构真题】

请设计一个算法&#xff0c;将给定的表达式树&#xff08;二叉树&#xff09;转换成等价的中缀表达式&#xff08;通过括号反映次序&#xff09;&#xff0c;并输出。例如&#xff0c;当下列两棵表达式树作为算法的输入时&#xff1a; 输出的等价中缀表达式分别为(ab)(a(-d)) 和…

OpenAI Assistants-API简明教程

OpenAI在11月6号的开发者大会上&#xff0c;除了公布了gpt4-v、gpt-4-turbo等新模型外&#xff0c;还有一个assistants-api&#xff0c;基于assistants-api开发者可以构建自己的AI助手&#xff0c;目前assistants-api有三类的工具可以用。首先就是之前大火的代码解释器(Code In…

隐式转换导致索引失效的原因

Num1 int Num2 varchar Str1不能为null Str2可null 例子1&#xff1a; 结果&#xff1a;124非常快&#xff0c;0.001~0.005秒出结果。3最慢&#xff0c;4~5秒出结果。 查询执行计划&#xff1a;124索引扫描。3全表扫描。 解释&#xff1a;首先四个23都产生隐式转换&#x…

第7天:信息打点-资产泄漏amp;CMS识别amp;Git监控amp;SVNamp;DS_Storeamp;备份

第7天&#xff1a;信息打点-资产泄漏&CMS识别&Git监控&SVN&DS_Store&备份 知识点&#xff1a; 一、cms指纹识别获取方式 网上开源的程序&#xff0c;得到名字就可以搜索直接获取到源码。 cms在线识别&#xff1a; CMS识别&#xff1a;https://www.yun…

【Gradle-13】SNAPSHOT版本检查

1、什么是SNAPSHOT SNAPSHOT版本是指尚未发布的版本&#xff0c;是一个「动态版本」&#xff0c;它始终指向最新的发布工件&#xff08;gav&#xff09;&#xff0c;也就是说同一个SNAPSHOT版本可以反复用来发布。 这种情况在大型app多团队的开发中比较常见&#xff0c;比如us…