【SpringCloud微服务实践】Springboot Actuator

应用监控者Actuator

Springboot Actuator是一个用于监控和管理Springboot应用程序的框架,它提供了许多端点(endpoint)来监控应用程序的健康状况、性能、配置和日志等信息,并且可以通过HTTP、JMX和SSH等多种方式访问这些端点。

集成Actuator

如果使用的IDE是IntelliJ IDEA,运行Springboot应用后在面板上进入Actuator启用一下,然后重启即可

或者在pom中手动引入Actuator

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

我在之前user-service项目中引入Actuator

配置Actuator

集成完之后,访问http://localhost:8081/actuator(应用地址+/actuator)可以看到actuator对外公开的端点,不出意外的化,应该只有2到3个,我只有self, health和health-path

开放端点

通常情况下,我们需要获取更多的端点和信息,因此要在项目的配置文件(properties,yaml,yml)中开启

# Actuator配置
management.endpoints.web.exposure.include=*

我这里方便起见,开放了全部端点。
实际情景下,应该有选择性的开放端点,可以include部分端点,也可以先include全部端点,再exclude掉不需要开放的端点。

健康详情

直接访问http://localhost:8081/actuator/health,只能获得如下信息:

{
    
    
    "status": "UP"
}

获取更详细的信息,我们需要做如下设置:

management.endpoint.health.show-details=always

然后就能获取详情的健康信息:

{
    
    
    "status": "UP",
    "components": {
    
    
        "db": {
    
    
            "status": "UP",
            "details": {
    
    
                "database": "MySQL",
                "validationQuery": "isValid()"
            }
        },
        "diskSpace": {
    
    
            "status": "UP",
            "details": {
    
    
                "total": 304217063424,
                "free": 62942789632,
                "threshold": 10485760,
                "exists": true
            }
        },
        "ping": {
    
    
            "status": "UP"
        }
    }
}

自定义info

在配置文件中配置info信息:

info:
  app:
    name: ${
    
    spring.application.name}
    author: evanpatchouli
    frame: springboot
    java:
      source: @java.version@
      target: @java.version@

直接访问http://localhost:8081/actuator/info,是看不到info的,需要做如下配置:

management.info.env.enabled=true

这样就能获取到自定义的info:

{
    
    
    "app": {
    
    
        "name": "user-service",
        "author": "evanpatchouli",
        "frame": "springboot",
        "java": {
    
    
            "source": "17.0.6",
            "target": "17.0.6"
        }
    }
}

开启shutdown

如果需要通过actuator终止应用,需要做如下配置:

management.info.env.enabled=true

然后POST访问http://localhost:8081/actuator/shutdown即可

在实际情景下,如需开启shutdown,必须要做一定的鉴权防护,以防止被恶意攻击者关停服务。


本文只对Actuator做基本的介绍,更多的设置和应用请参考官方手册以及其它文章的讲解。

猜你喜欢

转载自blog.csdn.net/m0_51810668/article/details/130526535