Spring Boot Actuator使用詳解


Actuator提供可以通過HTTP或者JMX監控和管理線上應用的附加功能,包括了審核、服務運行狀況和指標收集,這些功能通過endpoints獲取,每個endpoints都可以通過Http或者JMX 進行enabled(啟用)disabled(禁用)exposed(公開)

當依賴加入到項目中之后,endpoints可用的時候會自動注冊到程序中,可以通過http或者jmx進行訪問

配置和簡單使用

開始使用
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

啟動項目訪問/actuator/health,得到如下內容

{"status":"UP"}

其中/actuator 是訪問endpoints的默認url前綴,/health 則展示應用的健康信息,

可以通過以下屬性配置,修改endpoints的url前綴,修改之后health訪問地址為 /simple/health

management.endpoints.web.base-path=/simple

由於endpoints中會包含敏感信息,所以暴露端點的時候應該謹慎考慮,下面列出常用的endpoints以及默認暴露情況

ID 描述 JMX Web
health 應用健康信息 YES YES
info 任意應用信息 YES NO
beans 顯示應用中所有的beans信息 YES NO
env Spring中的ConfigurableEnvironment公開屬性 YES NO
metrics 應用程序的指標 YES NO
headdump 獲取hprof dump文件 N/A NO
loggers 展示和修改應用的日志配置 YES NO

可以通過以下命令選擇暴露和過濾不想暴露的endpoints

Property Default
management.endpoints.jmx.exposure.exclude  
management.endpoints.jmx.exposure.include *
management.endpoints.web.exposure.exclude  
management.endpoints.web.exposure.include health、info
啟用和禁用endpoint
#通過management.endpoint.<id>.enabled屬性設置,開啟shutdown endpoint
#默認情況下除了shutdown以外的所有endpoint都是開啟狀態的
management:
  endpoint:
    shutdown:
      enabled: true
暴露和過濾不想暴露的endpoint
management:
  endpoints:
    web:
      exposure:
        #暴露endpoints,通過http可以訪問 /actuator/health、/actuator/beans、/actuator/info
        include: health,info,beans
        #禁止info端點暴露, 通過http訪問不了
        exclude: info

如果include和exclude同時使用的時候,exclude的優先級高,所以上面來說info通過http是訪問不了的

自定義Endpoint

創建Bean,在類上添加@Endpoint、@WebEndpoint、@EndpointWebExtension其中一個注解,在方法上添加@ReadOperation或者@WriteOperation或者@DeleteOperation。如果是web應用,endpoint可以使用Jersey、SpringMVC、Spring WebFlux通過HTTP暴露,如果Jersey和SpingMVC都可用,則使用SpingMVC。

也可以使用@ServletEndpoint 和@ControllerEndpoint,但是建議使用@Endpoint 和@WebEndpoint

這里以@Endpoint舉例,其他的可以參考提供的完整demo代碼

@Endpoint id屬性必須進行設置

@Configuration
//id必須進行設置
@Endpoint(id = "myCustomEndpoints")
public class MyCustomEndpoints {
    @ReadOperation
    public ShopData getData(){
        return new ShopData("店小二","地球村");
    }
    @Data
    @AllArgsConstructor
    public static class ShopData{
        private String name;
        private String address;
    }
}
management:
  endpoints:
    web:
      exposure:
      ##暴露myCustomEndpoints
        include: health,info,beans,env,myCustomEndpoints

訪問:actuator/myCustomEndpoints 返回結果{"name":"店小二","address":"地球村"}

常見的Endpoint詳解

Health endpoint

可以通過health指標來判斷應用程序的運行狀況,可以通過一下屬性配置查看health的詳細信息

有三個只可以選擇,nerver、when-authorized、always 默認微nerver

#如果show-components 沒有指定 則用show-details的值進行展示
#如果show-components 指定了,則以show-components的配置為准
management.endpoint.health.show-details=always
management.endpoint.health.show-components=always

springboot 自帶了一部分一些常見的健康指標(只列出一部分

  • DataSourceHealthIndicator: 檢查是數據庫連接狀態
  • DiskSpaceHealthIndicator:檢查磁盤空間是否不足
  • ElasticsearchRestHealthIndicator:檢查ES集群是否是up狀態
  • mail:檢查郵箱服務器是否是up狀態
  • rabbit:檢查rabbitmq server是否是up狀態
  • redis:檢查redis是否是up狀態

自定義健康指標

/**
 * 驗證custom.url 的值是否正確,如果正確則狀態是up,如果不正確,則表示down
 */
@Component
public class MyCustomIndicator implements HealthIndicator {

    @Value("${custom.url}")
    private String customUrl;

    @Override
    public Health health() {
        if("http://www.baidu.com".equalsIgnoreCase(customUrl)){
            return Health.up().build();
        }
        return Health.down().withDetail("customUrl",customUrl).build();
    }
}
custom.url = http://www.baidu.com
{"status":"UP","components":{"diskSpace":{"status":"UP","details":{"total":250685575168,"free":46600232960,"threshold":10485760,"exists":true}},"myCustomIndicator":{"status":"UP"},"ping":{"status":"UP"}}}

info endpoint

SpringBoot包含了許多自動配置的InfoContributor 下面列出幾個常用的

  • EnvironmentInfoContributor 公開Environment中的信息
  • GitInfoContributor 如果git.properties文件可用,則公開git信息
  • BuildInfoContributor 如果META/INF/build-info.properties 我那件可用,則公開構建信息

自定義 Application Infomation

#通過設置info.* Spring屬性來自定義ifo端點公開的數據
info.app.encoding = UTF-8
#info.app.java.source = 8
info.app.java.source = @java.version@

運行結果如下:

{"app":{"encoding":"UTF-8","java":{"source":"1.8.0_191"}}}

如果需要獲取build info

<build>
  <plugins>
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
      <executions>
        <execution>
          <goals>
            <goal>build-info</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

然后運行結果如下

{"app":{"encoding":"UTF-8","java":{"source":"1.8.0_191"}},"build":{"version":"1.0-SNAPSHOT","artifact":"actuator-simple","name":"actuator-simple","group":"com.hs.springboot","time":"2021-06-04T02:51:53.878Z"}}

Loggers endpoint

用來查看和修改應用的日志級別 通過/actuator/loggers 可以查看所有的日志級別情況,由於訪問內容比較多,就截取一部分

{"levels":["OFF","ERROR","WARN","INFO","DEBUG","TRACE"],"loggers":{"ROOT":{"configuredLevel":"INFO","effectiveLevel":"INFO"},"com":{"configuredLevel":null,"effectiveLevel":"INFO"},"com.hs":{"configuredLevel":null,"effectiveLevel":"INFO"},"com.hs.springboot":{"configuredLevel":null,"effectiveLevel":"INFO"},"com.hs.springboot.actuator":{"configuredLevel":null,"effectiveLevel":"INFO"},"com.hs.springboot.actuator.ActuatorSimpleApplication":{"configuredLevel":null,"effectiveLevel":"INFO"},"io":{"configuredLevel":null,"effectiveLevel":"INFO"},"io.micrometer":

如果想單獨訪問某個的日志配置,例如訪問ROOT的,訪問路徑如下:/actuator/loggers/ROOT

//如果配置了configuredLevel 則effectiveLevel和configuredLevel相同,如果沒有顯示配置 則configuredLevel為null
{"configuredLevel":"INFO","effectiveLevel":"INFO"}

修改ROOT的日志級別 請求路徑/actuator/loggers/ROOT 進行動態調整應用程序的日志級別 參數如下:

{
    "configuredLevel": "DEBUG"
}

總結:

Actuator 提供了可以查看應用程序運行的狀態信息,同時也可以自定義符合自己業務要求的健康檢查

還可以和監控軟件例如Prometheus 進行系統整合

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM