你應該知道的Spring Boot Actuator


1:簡介

2:基於Maven的項目:

3.端點

3.1 端點簡介

3.2 端點開啟

3.3 默認暴露的

3.4控制暴露的端點:

4: 保護端點

4.1 加入POM 依賴

4.2 配置用戶名密碼

配置權限:

4.3 通過配置的方式允許所有匿名用戶訪問

4:健康信息與自定義健康狀況:

4.1 健康信息顯示設置

4.2 spring boot 自動配置的健康指標

4.3自定義健康狀況

5: 自定義應用程序信息

6: 自定義管理端點路徑

1:簡介

將應用程序投入生產時幫助您監視和管理應用程序。您可以選擇使用HTTP端點或JMX(Java Management Extensions)管理和監視應用程序。審核,運行狀況和指標收集也可以自動應用於您的應用程序。

目的: 實現對服務的監控與管理

DevOps: 是開發 與 運維的總稱

2:基於Maven的項目:

<code><dependencies> <dependency> <groupid>org.springframework.boot/<groupid> <artifactid>spring-boot-starter-actuator/<artifactid> /<dependency>/<dependencies>/<code>
<code>dependencies { compile("org.springframework.boot:spring-boot-starter-actuator")}/<code>

3.端點

health端點映射 訪問: http://localhost:8888/actuator/health

3.1 端點簡介

如果您的應用程序是Web應用程序(Spring MVC,Spring WebFlux或Jersey),則可以使用以下附加端點:


你應該知道的Spring Boot Actuator


3.2 端點開啟

除shutdown端點均處於啟用狀態。要配置端點的啟用,請使用其management.endpoint..enabled屬性。以下示例啟用shutdown端點:

management.endpoint.shutdown.enabled=true

management.endpoints.enabled-by-default=false # 禁用所有端點management.endpoint.info.enabled=true #開啟info端點

3.3 默認暴露的


你應該知道的Spring Boot Actuator

3.4控制暴露的端點:

默認暴露 health info 端點

# 公開端點

management.endpoints.jmx.exposure.include=health,info# 閉塞端點management.endpoints.web.exposure.include=* // 暴露所有端點management.endpoints.web.exposure.exclude=env,beans //開放自定義端點


4: 保護端點

4.1 加入POM 依賴

<code><dependencies> <dependency> <groupid>org.springframework.boot/<groupid> <artifactid>spring-boot-starter-actuator/<artifactid> /<dependency>/<dependencies>/<code>

4.2 配置用戶名密碼

<code>dependencies { compile("org.springframework.boot:spring-boot-starter-actuator")}/<code>

配置權限:

繼承 WebSecurityConfigurerAdapter

<code>@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();    }}/<code>

然後配置 ENDPOINT_ADMIN

<code>spring.security.user.roles=ENDPOINT_ADMINmanagement.endpoints.web.exposure.include=*spring.security.user.name=rootspring.security.user.password=root/<code>

4.3 通過配置的方式允許所有匿名用戶訪問

該配置允許未經身份驗證的端點訪問

<code>該配置允許未經身份驗證的端點訪問@Configuration(proxyBeanMethods = false)public class ActuatorSecurity extends WebSecurityConfigurerAdapter {    @Override    protected void configure(HttpSecurity http) throws Exception {        http.requestMatcher(EndpointRequest.toAnyEndpoint()).authorizeRequests((requests) ->            requests.anyRequest().permitAll());    }}/<code>


4:健康信息與自定義健康狀況:

4.1 健康信息顯示設置

<code>management.endpoint.health.show-details=always // 顯示詳細信息management.endpoint.health.show-details=when_authorized // 授權後才能訪問management.endpoint.health.show-details=never // 不顯示/<code>

4.2 spring boot 自動配置的健康指標

HealthIndicators在適當的情況下,Spring Boot會自動配置以下內容:


你應該知道的Spring Boot Actuator


4.2.1: 禁用它們。

# 禁用全部management.health.defaults.enabled=false

# 禁用rabbitmanagement.health.rabbit.enabled=false


4.3自定義健康狀況

註冊實現該HealthIndicator接口的Spring bean 。您需要提供該health()方法的實現並返回Health響應

<code><dependency>     <groupid>org.springframework.boot/<groupid>     <artifactid>spring-boot-starter-security/<artifactid> /<dependency>/<code>


下表顯示了內置狀態的默認狀態映射:


你應該知道的Spring Boot Actuator


5: 自定義應用程序信息

<code># 通過info 關鍵字定義 info.* = xxxinfo.helloworld=helloworldinfo.jdk.version=1.8返回:{"helloworld":"helloworld","jdk":{"version":"1.8"}}/<code>


6: 自定義管理端點路徑

<code>management.endpoints.web.base-path=/manager# 原地址http://localhost:8080/actuator/health# 改變後地址http://localhost:8080/manager/health/<code>


分享到:


相關文章: