spring Boot Actuator使用
spring Boot Actuator使用
Spring Boot 的 Actuator 提供了很多生產級的特性,比如監控和度量Spring Boot 應用程序。
官網參考文檔Spring Boot Actuator: Production-ready Features
maven依賴
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency> </dependencies>暴露的endpoints
Actuator endpoints let you monitor and interact with your application. Spring Boot includes a number of built-in endpoints and lets you add your own. For example, the health endpoint provides basic application health information.
根據官網的提示,我們可以得知Actuator endpoints讓我們能跟我們的application進行監控和互動。Spring Boot Actuator本身已經提供的若干個定義好的endpoints。更加這些endpoints,我們對我們的application進行監控。如:health endpoint (監控application的健康信息)
Most applications choose exposure via HTTP, where the ID of the endpoint along with a prefix of /actuator is mapped to a URL. For example, by default, the health endpoint is mapped to /actuator/health.
根據官網的提示,這些endpoint都是以HTTP進行暴露的,并且是以/actuator為前綴的URL,如:/actuator/health是健康監控HTTP端口。
表格鏈接:https://www.jianshu.com/p/d5943e303a1f
| auditevents | 顯示應用暴露的審計事件 (比如認證進入、訂單失敗) |
| info | 顯示應用的基本信息 |
| health | 顯示應用的健康狀態 |
| metrics | 顯示應用多樣的度量信息 |
| loggers | 顯示和修改配置的loggers |
| logfile | 返回log file中的內容(如果logging.file或者logging.path被設置) |
| httptrace | 顯示HTTP足跡,最近100個HTTP request/repsponse |
| env | 顯示當前的環境特性 |
| flyway | 顯示數據庫遷移路徑的詳細信息 |
| liquidbase | 顯示Liquibase 數據庫遷移的纖細信息 |
| shutdown | 讓你逐步關閉應用 |
| mappings | 顯示所有的@RequestMapping路徑 |
| scheduledtasks | 顯示應用中的調度任務 |
| threaddump | 執行一個線程dump |
| heapdump | 返回一個GZip壓縮的JVM堆dump |
暴露方式
根據官網的提示這些端口都有默認的暴露方式,分別是JMX和Web。這個是官網的表格:
| auditevents | Yes | No |
| beans | Yes | No |
| caches | 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 | No |
| integrationgraph | Yes | No |
| jolokia | N/A | No |
| logfile | N/A | No |
| loggers | Yes | No |
| liquibase | Yes | No |
| metrics | Yes | No |
| mappings | Yes | No |
| prometheus | N/A | No |
| quartz | Yes | No |
| scheduledtasks | Yes | No |
| sessions | Yes | No |
| shutdown | Yes | No |
| startup | Yes | No |
| threaddump | Yes | No |
如果想更改這些默認的暴露方式,可以從配置文件中修改如下四個配置屬性:
management.endpoints.jmx.exposure.excludemanagement.endpoints.jmx.exposure.include=* #默認management.endpoints.web.exposure.excludemanagement.endpoints.web.exposure.include=health #默認保護 HTTP endpoin
You should take care to secure HTTP endpoints in the same way that you would any other sensitive URL. If Spring Security is present, endpoints are secured by default using Spring Security’s content-negotiation strategy. If you wish to configure custom security for HTTP endpoints, for example, only allow users with a certain role to access them, Spring Boot provides some convenient RequestMatcher objects that can be used in combination with Spring Security.
關于HTTP endpoint的保護,如果你有整合Spring Security,那么endpoints會使用Spring Security默認的安全策略,你也可以自定義一個安全策略。
自定義的SecurityFilterChain(安全過濾鏈),可以幫助我們對Endpoint的請求進行過濾,如下面安全過濾鏈中,對Endpoint進行的請求都必須有ENDPOINT_ADMIN role。
import org.springframework.boot.actuate.autoconfigure.security.servlet.EndpointRequest; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.web.SecurityFilterChain;@Configuration(proxyBeanMethods = false) public class MySecurityConfiguration {@Beanpublic SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {http.requestMatcher(EndpointRequest.toAnyEndpoint()).authorizeRequests((requests) -> requests.anyRequest().hasRole("ENDPOINT_ADMIN"));http.httpBasic();return http.build();} }開啟CORS支持
management:endpoints:web:cors:allowed-origins: "https://example.com"allowed-methods: "GET,POST"/actuator/health
Spring Boot Actuator有很多預定義的健康指示器,下面列出幾個是:
DataSourceHealthIndicator
DiskSpaceHealthIndicator
MongoHealthIndicator
RedisHealthIndicator
CassandraHealthIndicator
官網API地址:Overview (Spring Boot 2.5.3 API)
除了上面的還有其他支持的指示器,可以去官網API文檔看,我們使用也十分簡單,Spring Boot Actuator已經把這些裝配到上下文中了,默認是開啟監控的。如果想關閉某個健康檢測,在配置文件進行配置即可
management.health.mongo.enabled=false如果需要詳細的健康檢測數據,只要在配置文件中加
management.endpoint.health.show-details=always還有兩個Kubernetes常用的健康檢測端口:
/actuator/metrics
/actuator/metrics是度量或者檢測端口,我們可以通過一些方式檢測指定的metrics
http://localhost:8080/actuator/metrics/{MetricName}可以通過端口/actuator來獲取MetricName
{"names": ["jvm.memory.max","http.server.requests","process.files.max",...] }/actuator/loggers
該端口用于獲取loggers的等級或者進行修改
http://localhost:8080/actuator/loggers/{name} //如: http://localhost:8080/actuator/loggers/root/actuator/bus-refres
這個端口一般是在spring cloud config中使用,通過這個端口可以刷新配置文件信息。
curl -v -X POST “http://localhost:8081/actuator/bus-refresh”
curl -X POST “http://localhost:8081/actuator/bus-refresh”
/actuator/bus-refres
這個端口一般是在spring cloud config中使用,通過這個端口可以刷新配置文件信息。
curl -v -X POST “http://localhost:8081/actuator/bus-refresh”
curl -X POST “http://localhost:8081/actuator/bus-refresh”
總結
以上是生活随笔為你收集整理的spring Boot Actuator使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 绣球花花语(绣球花的寓意及象征)
- 下一篇: 网络监控摄像头如何改ip网络摄像机怎么修