zoukankan      html  css  js  c++  java
  • SpringBoot-actuator服务监控与管理

    SpringBoot-actuator服务监控与管理

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

    Endpoints

    actuator 的核心部分,它用来监视应用程序及交互,spring-boot-actuator中已经内置了非常多的 Endpoints(health、info、beans、httptrace、shutdown等等),同时也允许我们自己扩展自己的端点

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

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

    Spring Actuator端点安全

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

    使用SpringActuator

            <!-- web-->
            <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所必须的,此时,启动你的SpringBoot项目时,“/health”和“/info”就被自动映射。

     2 endpoint(s) beneath base path '/actuator'
    : Mapped "{[/actuator/health],methods=[GET]
    

    此时可以通过浏览器来访问并获取信息:

    http://localhost:8080/actuator/health
    
    {
        "status": "DOWN",
        "details": {
            "db": {
                "status": "UP",
                "details": {
                    "database": "MySQL",
                    "hello": 1
                }
            },
            "diskSpace": {
                "status": "UP",
                "details": {
                    "total": 128849014784,
                    "free": 74140549120,
                    "threshold": 10485760
                }
            },
            "redis": {
                "status": "DOWN",
                "details": {
                    "error": "org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to 192.168.188.128:6379"
                }
            }
        }
    }
    

    当访问info时,发现页面中没有信息

    http://localhost:8080/actuator/info
    

    这是由于我们没有进行配置,在application.yml中加入如下配置即可

    info:
      app:
        name: Spring Actuator Example 	#projectName
        java.version: 8					#java version
        type: Spring Boot				#projectType
    

    此时,重新访问,即可出现以下数据:

    {
        "app": {
            "name": "Spring Actuator Example",
            "java": {
                "version": 8
            },
            "type": "Spring Boot"
        }
    }
    

    自定义执行器断点基本路径

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

    management:
      endpoints:
        web:
          base-path: /management
    

    此时访问下面的页面会返回404page

    http://localhost:8080/actuator/info
    

    应该访问:

    http://localhost:8080/management/info
    

    暴露其他Endpoint

    我们可以通过属性文件启用和禁用其他执行器端点。

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

    management:
      endpoints:
        web:
          #base-path: /management
          exposure:
            include: '*'
      endpoint:
        health:
          show-details: always
    

    上面是暴露所有的endpoint,如果想暴露指定的断点可以使用下面的配置

    management:
      endpoints:
        web:
          #base-path: /management
          exposure:
            include: health,info,beans,env
      endpoint:
        health:
          show-details: always
    

    常用的API使用及说明

    http://localhost:8080/actuator				#查看所有actuator API
    
    http://localhost:8080/actuator/health		#查看项目健康情况
    
    http://localhost:8080/actuator/beans		#查看当前Spring容器中的所有bean,可以进行排错
    
    http://localhost:8080/actuator/env 			#查看详细的服务器,以及配置环境
    
    http://localhost:8080/actuator/mappings		#查看当前所有的mapping
    
    http://localhost:8080/actuator/httptrace	#查看所有的http请求历史,以及请求信息
    
    http://localhost:8080/actuator/info			#查看项目的信息
    

    其他的大家可以自己访问http://localhost:8080/actuator去研究哈~

    SpringBoot-actuator的高级用法其实是自定义endpoint,没有太多经历去研究,大家有兴趣可以参考下面的两篇文章~

    参考文章:来自jdon.com

    更详细的讲解:博客园文章

  • 相关阅读:
    智能移动机器人背后蕴含的技术——激光雷达
    Kalman Filters
    Fiddler抓HttpClient的包
    VSCode开发WebApi EFCore的坑
    WPF之小米Logo超圆角的实现
    windows react打包发布
    jenkins in docker踩坑汇总
    Using ML.NET in Jupyter notebooks 在jupyter notebook中使用ML.NET ——No design time or full build available
    【Linux知识点】CentOS7 更换阿里云源
    【Golang 报错】exec gcc executable file not found in %PATH%
  • 原文地址:https://www.cnblogs.com/zhangruifeng/p/13431983.html
Copyright © 2011-2022 走看看