SpringBoot系列: Actuator監控


Sprng Boot 2 actuator變動加大, 網上很多資料都都已經過期.  

============================
配置項
============================
在 application.properties 配置文件, actuator 的設置項 management.endpoints(設置 actuator 全局級的屬性) 和 management.endpoint(設置 具體endpoint 屬性) 開頭.

----------------------
全局級的控制
----------------------

#定制管理功能的 port, 如果端口為 -1 代表不暴露管理功能 over HTTP
management.server.port=8081      
# 設定 /actuator 入口路徑
management.endpoints.web.base-path=/actuator            
# 所有endpoint缺省為禁用狀態
management.endpoints.enabled-by-default=false           
# 暴露所有的endpoint, 但 shutdown 需要顯示enable才暴露, * 表示全部, 如果多個的話,用逗號隔開
management.endpoints.web.exposure.include=*           
# 排除暴露 loggers和beans endpoint
management.endpoints.web.exposure.exclude=loggers,beans 
# 定制化 health 端點的訪問路徑
management.endpoints.web.path-mapping.health=healthcheck  

----------------------
endpoint 級別的控制
----------------------
所有的endpoint都有 enabled 屬性, 可以按需開啟或關閉特定端點.

#啟用 shutdown
management.endpoint.shutdown.enabled=true    


----------------------
重點關注 health 端點一些配置項
----------------------

management.endpoint.health.enabled=true
#show-details屬性的取值有: never/always/when-authorized, 默認值是 never
management.endpoint.health.show-details=always  
#增加磁盤空間health 統計, 還有其他health indicator
management.health.diskspace.enabled=true  


----------------------
actuator 缺省的設置
----------------------
缺省 actuator 的根路徑為 /actuator
缺省僅開放 health 和 info, 其他都不開放.
有些 endpoint 是GET, 有些是POST方法, 比如 health 為GET, shutdown 為 POST, 從SpringBoot程序啟動日志中, 可以看出到底有哪些endpoint被開啟.

 

----------------------
endpoint 清單
----------------------
actuator 支持的所有 endpoint, 可以查官網 https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-endpoints.html

下面是一些重要的端點,(以訪問路徑列出):
/actuator: actuator 嚴格地說不能算 endpoint, /actuator 返回要給 endpoint 的清單.
/actuator/health: 展現系統運行狀態, 在和spring cloud consul集成時候, 就使用該端點檢查Spring應用的狀態.
/actuator/metric/: 顯示更全面的系統指標
/actuator/configprops:展現 SpringBoot 配置項
/actuator/env: 顯示系統變量和SpringBoot應用變量, actuator 非常貼心, 如果屬性名包含 password/secret/key 這些關鍵詞, 對應的屬性值將用 * 號代替.
/actuator/httptrace: 顯示最近100條 request-response 信息
/actuator/autoconfig: 生成Spring boot自動化配置報告, 該報告非常有用, 說明如下: Spring Boot項目傾向於使用很多auto config技術, 包括散落在很多config java類. 我們在開發過程中偶爾會遇到, 為什么我的配置沒起作用這樣的問題. 這時候查看 /actuator/autoconfig 的報告非常有用, 它會告訴你哪些自動化裝配成功了,哪些沒有成功.
/actuator/beans: 該端點可以獲取 application context 中創建的所有 bean, 並列出它們的scope 和 type 等詳細信息
/actuator/mappings: 該端點列出了所有 controller 的路由信息.

 

============================
簡單示例
============================
pom 加上 actuator 依賴, 注意不加 spring-boot-starter-security.

<dependencies>
    <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>
</dependencies>

 

application.properties 內容如下,

management.endpoints.enabled-by-default=true
management.endpoints.web.exposure.include=*
management.endpoints.web.exposure.exclude=loggers,beans

management.endpoint.health.show-details=always
management.health.diskspace.enabled=true
management.health.enabled=true

該項目不需要手寫任何 java 代碼的情況下, 已有了監控檢查功能, 訪問 http://localhost:8080/actuator/health 即可. 


============================
加上 spring-boot-starter-security 安全控制
============================
接上面示例project, 增加了 pring-boot-starter-security 依賴項. 

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

增加了 pring-boot-starter-security 依賴項后, 再訪問 health 端點將會跳轉到一個 login 界面, 實際情況往往是我們總是希望 health 能被自由地訪問到, 比如被監控系統訪問. 有兩個做法:

1.粗暴的方式,直接在主程序類上禁止  Security 的 auto configuration, 注解為  @SpringBootApplication(exclude = { SecurityAutoConfiguration.class }) 

2. 創建一個基於 WebSecurityConfigurerAdapter 的配置類, 在其鑒權configure()方法中, 開啟對特定 actuator 端點的訪問許可.

@Configuration
public class ActuatorSecurityConfig extends WebSecurityConfigurerAdapter {

    /*
        This spring security configuration does the following

        1. Restrict access to the Shutdown endpoint to the ACTUATOR_ADMIN role.
        2. Allow access to all other actuator endpoints.
        3. Allow access to static resources.
        4. Allow access to the home page (/).
        5. All other requests need to be authenticated.
        5. Enable http basic authentication to make the configuration complete.
           You are free to use any other form of authentication.
     */

    @Override
    protected void configure(HttpSecurity http) throws Exception {
                http.authorizeRequests()
                    .requestMatchers(EndpointRequest.to(ShutdownEndpoint.class)).hasRole("ACTUATOR_ADMIN")
                    .requestMatchers(EndpointRequest.toAnyEndpoint()).permitAll()
                    .requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll()
                    .antMatchers("/").permitAll()
                    .antMatchers("/**").authenticated()
                    .and()
                    .httpBasic();
    }
}

 

在 application.properties 先簡單設置spring security 相關屬性, 然后運行程序, 訪問 http://localhost:8080/actuator/health 進行測試. 

# Spring Security Default user name and password
spring.security.user.name=actuator
spring.security.user.password=actuator
spring.security.user.roles=ACTUATOR_ADMIN 

 


============================
參考
============================
Spring Boot Actuator: Health check, Auditing, Metrics gathering and Monitoring
https://www.callicoder.com/spring-boot-actuator/
springboot(十九):使用Spring Boot Actuator監控應用
http://www.ityouknow.com/springboot/2018/02/06/spring-boot-actuator.html
Spring Boot Actuator監控端點小結
http://blog.didispace.com/spring-boot-actuator-1/
老司機的應用級監控——spring actuator
https://www.jianshu.com/p/c043d3c71f47
Spring Boot Actuator in Spring Boot 2.0
https://dzone.com/articles/spring-boot-actuator-in-spring-boot-20
SpringBoot-Actuator-加SpringSecurity驗證
https://blog.csdn.net/goldenfish1919/article/details/78130516

 


免責聲明!

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



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