当前位置: 首页 > article >正文

Spring Boot Actuator使用

说明:本文介绍Spring Boot Actuator的使用,关于Spring Boot Actuator介绍,下面这篇博客写得很好,珠玉在前,我就不多介绍了。

  • Spring Boot Actuator

简单使用

项目里引入下面这个依赖

<!--Spring Boot Actuator依赖-->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

增加以下配置,开放所有默认接口

management:
  endpoints:
    web:
      exposure:
        include: "*"

启动项目,浏览器输入:http://127.0.0.1:8081/actuator,可见以下接口信息

在这里插入图片描述

输入某个详细的接口名称,如:http://127.0.0.1:8081/actuator/health,查询详细的内容,如下:

在这里插入图片描述

配置文件中,添加配置,查看更详细的内容

management:
  # 开放所有接口
  endpoints:
    web:
      exposure:
        include: "*"
  # 单个接口设置
  endpoint:
    health:
      show-details: ALWAYS

如下:

在这里插入图片描述

手动自定义一个接口,如下:

import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.stereotype.Component;

@Component
@Endpoint(id = "my-actuator")
public class MyActuator {

    @ReadOperation
    public String hello() {
        return "Good Luck";
    }
}

其中@Endpoint(id = "my-actuator"),可以等同于@RequestMapping("/my-actuator"),浏览器敲:http://127.0.0.1:8081/actuator/,可见自定义的接口,如下:

在这里插入图片描述

浏览器输:http://127.0.0.1:8081/actuator/my-actuator,可见接口返回

在这里插入图片描述

使用场景

通过上面的简单使用,可以看到 Spring Boot Actuator与我们常写的接口是分别开的,有点像swagger。他可以有以下的使用场景:

  • 健康检查:默认的health就是用来扩展健康检查的,我们可以在代码里去扩展这个接口,来实现自己的健康检查操作;

  • 监控:可以写一些接口,接口内返回运行时系统内的一些指标,收集起来,观测运行时的情况;

  • 统计:如上,有些数据是“活”的,不存数据库或者日志里,服务停止,数据就消失了,这些数据就可以在接口里返回,用于统计;

  • 排查问题:有些数据可能是存在本地缓存,如某个Map中的,就可以写一个接口,返回该Map中的数据,用于排查问题;

  • ……

可以把Spring Boot Actuator当做玻璃窗,能观察到运行时系统的内部情况。当然,用常规的接口也能实现,但不方便,因为要考虑如何在鉴权框架中排出掉这些接口,以及如何对此类接口进行管理,这就不能像Spring Boot Actuator这样得心应手——Spring Boot Actuator可以通过配置文件来控制。

总结

本文介绍了Spring Boot Actuator的简单使用


http://www.kler.cn/a/539834.html

相关文章:

  • Golang 并发机制-7:sync.Once实战应用指南
  • leetcode 80. 删除有序数组中的重复项 II
  • QT修仙之路1-1--遇见QT
  • 面向对象程序设计-实验3
  • android studio无痛入门
  • 人工智能入门 数学基础 线性代数 笔记
  • AI时代医疗大健康微服务编程提升路径和具体架构设计
  • C++11详解(四) -- 新的类功能和包装器
  • GIT创建子模块(submodule)
  • 【共享文件夹】使用Samba服务可在Ubuntu和Windows系统之间共享一个实际的文件夹
  • 告别人工检测!casaim自动化三维激光扫描
  • sqlite 查看表结构
  • Python Pandas(3):DataFrame
  • MATLAB | 基于Theil-Sen斜率和Mann-Kendall检验的栅格数据趋势分析
  • xinference 安装(http导致错误解决)
  • 安全行业大模型SecLLM技术白皮书
  • Linux内核中的软中断与NAPI机制:高效处理网络数据包
  • PostgreSQL SQL优化用兵法,优化后提高 140倍速度
  • 快速上手Vim的使用
  • U3D支持webgpu阅读
  • DevOps的个人学习
  • matplotlib绘制三维曲面图时遇到的问题及解决方法
  • Ubuntu 20.04配置网络
  • 【东莞常平】戴尔R710服务器不开机维修分享
  • 数据库基础练习4(有关索引,视图完整解答)
  • 25/2/8 <机器人基础> 轨迹控制基本知识点,传动系统