• Spring Boot Actuator


    1 简介

          Spring Boot Actuator端点通过 JMX 和HTTP 公开暴露给外界访问,大多数时候我们使用基于HTTP的Actuator端点,因为它们很容易通
          过浏览器、CURL命令、shell脚本等方式访问。
    
    • 1
    • 2

    一些有用的执行器端点是:

    /beans:此端点返回应用程序中配置的所有bean的列表。
    /env:提供有关Spring Environment属性的信息。
    /health:显示应用程序运行状况
    /info:显示应用程序信息,我们可以在Spring环境属性中配置它。
    /mappings:显示所有 @RequestMapping 路径 的列表 。
    /shutdown:允许我们正常关闭应用程序。
    /threaddump:提供应用程序的线程转储。
    你可以从 此处 获得Spring执行器端点的完整列表 。
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    Spring Actuator端点安全
    只有“/health”和“/info”端点暴露在没有任何安全性的情况下,为了访问我们需要配置Spring安全。 这很容易实现,我们将在本教程的后半部分介绍它。

    #1、actuator默认只开启了info和health两个端点
    #以下配置可以开启所有的端点:
    management.endpoints.web.exposure.include= *

    #2、开启健康监控数据
    management.endpoint.health.show-details=always

    #3、启用httptrace端点
    management.endpoint.httptrace.enabled=true

    #4、每次都要加个actuator前缀太麻烦,改变端点前缀路径
    management.endpoints.web.base-path= /

    启用S​​pring Actuator端点
    当我们将Spring Actuator Dependencies添加到我们的Spring启动项目时,它会自动启用执行器端点。将以下依赖项添加到Spring应用程序以启用Spring启动执行器端点。

    <dependency>
    	<groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    
    • 1
    • 2
    • 3
    • 4

    现在,当你运行应用程序时,你将看到执行器端点映射到日志中。

    2 endpoint(s) beneath base path ‘/actuator’

    : Mapped "{[/actuator/health],methods=[GET]
    请注意,只有两个端点 - health 并 info 已映射。

    下图显示了这些执行器端点的输出。

    是否注意到 /actuator/info 没有数据?这是因为我们还没有配置它们。 只需在 application.properties 文件中 添加以下属性即可 。

    info.app.name=Spring Actuator Example
    info.app.java.version=10
    info.app.type=Spring Boot
    重新启动应用程序,就能够获得上面信息了。

    自定义执行器端点基本路径
    默认情况下,执行器端点的基本路径是 /actuator ,我们可以通过 management.endpoints.web.base-path 在应用程序属性文件中 设置将其更改为任何其他值 。

    management.endpoints.web.base-path=/management
    暴露其他执行器端点
    我们可以通过属性文件启用和禁用其他执行器端点。

    如果要启用所有执行器端点,请添加以下属性。

    management.endpoints.web.exposure.include=*
    要仅启用特定的执行器端点,请提供端点ID列表。

    management.endpoints.web.exposure.include=health,info,beans,env
    执行器端点的Srping安全性
    请注意,我们需要将 Spring Security 添加 到我们的应用程序中以启用其他端点,因为所有其他端点至少需要基本身份验证。

    在应用程序中为spring security添加以下依赖项。

    org.springframework.boot spring-boot-starter-security 另外,在应用程序属性文件中添加spring安全性用户名和密码。

    spring.security.user.name=pankaj
    spring.security.user.password=pankaj
    重新启动应用程序,将看到正在映射的其他端点。

    现在,当尝试访问安全的执行器端点时,你必须提供登录凭据。

    Spring执行器自定义端点
    Spring Framework的 一个重要特性 是它很容易扩展。 我们可以使用 @Endpoint 类上的注释 创建自己的自定义执行器端点 。 然后我们必须 在方法上 使用 @ReadOperation , @WriteOperation 或 @DeleteOperation 注释将它们公开为执行器端点bean。

    我们可以使用 @JmxEndpoint 和 @WebEndpoint 注释 创建特定于技术的端点 。

    以下是我们自定义Spring执行器端点的示例。

    package com.samplesjdon.spring;

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

    @Endpoint(id=“myendpoint”)
    @Component
    public class MyCustomEndpoints {

    @ReadOperation
    @Bean
    public String hi() {
    	return "Hi from custom endpoint";
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    }
    你是否注意到端点ID? 我们还需要在要启用的执行器端点列表中配置它。

    更新 application.properties 文件中的 以下属性 。

    management.endpoints.web.exposure.include=health,info,beans,env,myendpoint
    现在,当你启动应用程序时,请检查日志中映射的新端点。

    总结
    Spring Boot Actuator是一个生产就绪的管理和信息模块。 我们可以轻松扩展它以添加我们自己的API并管理我们的应用程序。

  • 相关阅读:
    【C++】运算符重载 ④ ( 一元运算符重载 | 使用 全局函数 实现 前置 ++ 自增运算符重载 | 使用 全局函数 实现 前置 - - 自减运算符重载 )
    浅谈GPGPU任务调度-1
    成为职业游戏建模师有没有前景,未来自己该如何发展学习
    子菜单前带有复选框的创建方法
    gin源码分析
    【Linux安全之iptables的target】
    Go Gin Gorm Casbin权限管理实现 - 2. 使用Gorm存储Casbin权限配置以及`增删改查`
    Python基础:【习题系列】多选题(一)
    【Spring 学习系列】Bean 的生命周期之初始化与销毁
    哈希表专项练习 LeetCode
  • 原文地址:https://blog.csdn.net/qq_37705525/article/details/125448290