An actuator is a manufacturing term that refers to a mechanical device for moving or controlling something. Actuators can generate a large amount of motion from a small change.
用于監(jiān)控并管理應(yīng)用程序,提供了http和jmx兩種訪問方式,依賴的jar是spring-boot-starter-actuator杈女。
endpoint的暴露配置
默認(rèn)endpoint都是開啟的官册,除了shutdown硫痰】贪可以通過以下配置開啟某個(gè)endpoint馋记。
management.endpoint.{id}.enabled=true
//開啟shutdown endpoint
management.endpoint.shutdown.enabled=true
可以通過include
和exclude
屬性來配置endpoint的暴露與否驰后。
include
配置暴露的endpoint蝇狼,exclude指定不能暴露的endpoint。
如只暴露jmx的health和info endpoint倡怎,其它都關(guān)閉迅耘。
management.endpoints.jmx.exposure.include=health,info
指定除了env,beans endpoint,其它都暴露监署。
management.endpoints.web.exposure.include=*
management.endpoints.web.exposure.exclude=env,beans
在YAML格式下颤专,星號(hào)比較特殊,必須加雙引號(hào)钠乏。
management:
endpoints:
web:
exposure:
include: "*"
安全控制
如果endpoint對(duì)外暴露栖秕,建議使用spring security增加安全保護(hù)。
如指定針對(duì)actuator的所有端點(diǎn)晓避,Role必須是ENDPOINT_ADMIN簇捍。
@Configuration(proxyBeanMethods = false)
public class ActuatorSecurity extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.requestMatcher(EndpointRequest.toAnyEndpoint()).authorizeRequests((requests) ->
requests.anyRequest().hasRole("ENDPOINT_ADMIN"));
http.httpBasic();
}
}
CORS支持
跨域資源共享( CORS )機(jī)制允許Web應(yīng)用服務(wù)器進(jìn)行跨域訪問控制,從而使跨域數(shù)據(jù)傳輸?shù)靡园踩M(jìn)行俏拱∈钏埽跨域資源共享(CORS) 是一種機(jī)制,它使用額外的 HTTP 頭來告訴瀏覽器 讓運(yùn)行在一個(gè) origin (domain) 上的Web應(yīng)用被準(zhǔn)許訪問來自不同源服務(wù)器上的指定的資源锅必。當(dāng)一個(gè)資源從與該資源本身所在的服務(wù)器不同的域事格、協(xié)議或端口請(qǐng)求一個(gè)資源時(shí),資源會(huì)發(fā)起一個(gè)跨域 HTTP 請(qǐng)求。
CORS默認(rèn)是關(guān)閉的驹愚,如果開啟https://example.com
的GET和POST CORS支持远搪,可以如下配置:
management.endpoints.web.cors.allowed-origins=https://example.com
management.endpoints.web.cors.allowed-methods=GET,POST
通過HTTP訪問endpoint
默認(rèn)path是/actuator/{id},可以通過以下配置進(jìn)行調(diào)整,如指定成manage
management.endpoints.web.base-path=/manage
//指定成不同的端口
management.server.port=8081
如果想關(guān)閉HTTP endpoints,有以下幾種方式
management.server.port=-1
或
management.endpoints.web.exposure.exclude=*