SpringBoot學習之SpringBoot執行器


    在以往的分布式開發當中,各個服務節點的監控必不可少。監控包含有很多方面,比如說:內存占用情況,節點是否健康等。在spring-boot會給我們提供相關資源監控叫做spring-boot-actuator通過執行器可以幫我管理和監控生產環境下的應用服務。

 一。添加SpringBoot執行器的依賴(版本2.0.0.RELEASE)

   添加gradle配置依賴:

dependencies {
    compile('org.springframework.boot:spring-boot-starter-actuator')
}
View Code

 

 二。關於SpringBoot的端點

     端點是一個提供給我們監控應用程序的功能點,SpringBoot提供了一系列內置端點叫我們使用,舉個例子:health端點為我們提供了一個對我們基礎程序的一個健康狀態的監控

每個端點都可以打開或關閉,絕大多數的應用都可以通過http請求進行訪問,springboot包含很多內置內置端點。tips:參考官網的

ID Description Enabled by default

auditevents

Exposes audit events information for the current application.

Yes

beans

Displays a complete list of all the Spring beans in your application.

Yes

conditions

Shows the conditions that were evaluated on configuration and auto-configuration classes and the reasons why they did or did not match.

Yes

configprops

Displays a collated list of all @ConfigurationProperties.

Yes

env

Exposes properties from Spring’s ConfigurableEnvironment.

Yes

flyway

Shows any Flyway database migrations that have been applied.

Yes

health

Shows application health information.

Yes

httptrace

Displays HTTP trace information (by default, the last 100 HTTP request-response exchanges).

Yes

info

Displays arbitrary application info.

Yes

loggers

Shows and modifies the configuration of loggers in the application.

Yes

liquibase

Shows any Liquibase database migrations that have been applied.

Yes

metrics

Shows ‘metrics’ information for the current application.

Yes

mappings

Displays a collated list of all @RequestMapping paths.

Yes

scheduledtasks

Displays the scheduled tasks in your application.

Yes

sessions

Allows retrieval and deletion of user sessions from a Spring Session-backed session store. Not available when using Spring Session’s support for reactive web applications.

Yes

shutdown

Lets the application be gracefully shutdown.

No

threaddump

Performs a thread dump.

Yes

  

有幾點要補充說明一下:

  1). 我們訪問的端點監控的地址規范是:/actuator/{ID}的方式訪問,比如說:health端點默認被映射的路徑就是/actuator/health

      2) 並不是所有端點都可以通過http請求訪問,以下表格列舉了各個端點的狀態值:

ID JMX Web

auditevents

Yes

No

beans

Yes

No

conditions

Yes

No

configprops

Yes

No

env

Yes

No

flyway

Yes

No

health

Yes

Yes

heapdump

N/A

No

httptrace

Yes

No

info

Yes

Yes

jolokia

N/A

No

logfile

N/A

No

loggers

Yes

No

liquibase

Yes

No

metrics

Yes

No

mappings

Yes

No

prometheus

N/A

No

scheduledtasks

Yes

No

sessions

Yes

No

shutdown

Yes

No

threaddump

Yes

No

  
    我們可以發現默認情況下只有health與info端點才能通過http請求訪問,當然我們可以在屬性文件中配置  management.endpoints.web.exposure.include 來開放可以訪問的端點:
    
management.endpoints.web.exposure.include=*
management.endpoints.web.exposure.exclude=env,beans

 

3)當我們訪問端點時,響應的數據會緩存一定的時間,我們可以通過這個屬性進行配置:

  

management.endpoint.<id>.cache.time-to-live=10s

 

4) 我們也可以自己定義監視路徑,默認情況是:/actuator/xxx,通過如下屬性可以設置:
management.endpoints.web.base-path=/

5) 關於保護敏感端點,首先我們要添加對spring-security的依賴,並設置進行安全驗證的用戶名,密碼以及角色,如果不使用spring-security就要慎重考慮暴露端口的端點了

 

   三。關於Health端點

  1.  health端點用於檢測我們運行程序的健康狀態,當程序宕機時,可以提供給開發人員相關的提示信息
  2. 默認情況下該端點只是顯示簡略的監控信息,不過我們可以通過management.endpoint.health.show-details屬性來讓其顯示詳細的監控信息
  3. 端點有如下幾種狀態: UP DOWN UNKNOW-SERVICE 從字面上我們很好理解
  4. 實現自定義健康監控:
package com.bdqn.lyrk.springboot.study.monitor;

import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;

@Component
public class SelfMonitor implements HealthIndicator {
    @Override
    public Health health() {
        return Health.up().withDetail("health","next").build();
    }
}
View Code

    例子很簡單,主要是實現HealthIndicator接口,當我們訪問:http://localhost:8080/actuator/health時,我們可以看到相關監控信息,如下圖:

    

 


免責聲明!

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



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