強大的 actuator 服務監控與管理


SpringBoot 是為了簡化 Spring 應用的創建、運行、調試、部署等一系列問題而誕生的產物,自動裝配的特性讓我們可以更好的關注業務本身而不是外部的XML配置,我們只需遵循規范,引入相關的依賴就可以輕易的搭建出一個 WEB 工程

actuatorspring boot項目中非常強大一個功能,有助於對應用程序進行監視和管理,通過 restful api 請求來監管、審計、收集應用的運行情況,針對微服務而言它是必不可少的一個環節…

Endpoints

actuator 的核心部分,它用來監視應用程序及交互,spring-boot-actuator中已經內置了非常多的 Endpoints(health、info、beans、httptrace、shutdown等等),同時也允許我們自己擴展自己的端點

Spring Boot 2.0 中的端點和之前的版本有較大不同,使用時需注意。另外端點的監控機制也有很大不同,啟用了不代表可以直接訪問,還需要將其暴露出來,傳統的management.security管理已被標記為不推薦。

皮一下很開心皮一下很開心

內置Endpoints

id desc Sensitive
auditevents 顯示當前應用程序的審計事件信息 Yes
beans 顯示應用Spring Beans的完整列表 Yes
caches 顯示可用緩存信息 Yes
conditions 顯示自動裝配類的狀態及及應用信息 Yes
configprops 顯示所有 @ConfigurationProperties 列表 Yes
env 顯示 ConfigurableEnvironment 中的屬性 Yes
flyway 顯示 Flyway 數據庫遷移信息 Yes
health 顯示應用的健康信息(未認證只顯示status,認證顯示全部信息詳情) No
info 顯示任意的應用信息(在資源文件寫info.xxx即可) No
liquibase 展示Liquibase 數據庫遷移 Yes
metrics 展示當前應用的 metrics 信息 Yes
mappings 顯示所有 @RequestMapping 路徑集列表 Yes
scheduledtasks 顯示應用程序中的計划任務 Yes
sessions 允許從Spring會話支持的會話存儲中檢索和刪除用戶會話。 Yes
shutdown 允許應用以優雅的方式關閉(默認情況下不啟用) Yes
threaddump 執行一個線程dump Yes
httptrace 顯示HTTP跟蹤信息(默認顯示最后100個HTTP請求 - 響應交換) Yes

導入依賴

在 pom.xml 中添加 spring-boot-starter-actuator 的依賴

1
2
3
4
5
6
7
8
 <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

注意事項

如果要訪問info接口想獲取maven中的屬性內容請記得添加如下內容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

屬性配置

在 application.properties 文件中配置actuator的相關配置,其中info開頭的屬性,就是訪問info端點中顯示的相關內容,值得注意的是Spring Boot2.x中,默認只開放了info、health兩個端點,剩余的需要自己通過配置management.endpoints.web.exposure.include屬性來加載(有include自然就有exclude,不做詳細概述了)。如果想單獨操作某個端點可以使用management.endpoint.端點.enabled屬性進行啟用或禁用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 描述信息
info.blog-url=http://blog.battcn.com
info.author=Levin
info.version=@project.version@

# 加載所有的端點/默認只加載了 info / health
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always

# 可以關閉制定的端點
management.endpoint.shutdown.enabled=false

# 路徑映射,將 health 路徑映射成 rest_health 那么在訪問 health 路徑將為404,因為原路徑已經變成 rest_health 了,一般情況下不建議使用
# management.endpoints.web.path-mapping.health=rest_health

簡單測試

啟動項目,訪問 http://localhost:8080/actuator/info 看到如下內容代表配置成功

1
2
3
4
5
{
"blog-url": "http://blog.battcn.com",
"author": "Levin",
"version": "0.0.1-SNAPSHOT"
}

自定義 - 重點

上面講了很多都是配置相關,以及自帶的一些端點,在實際應用中有時候默認並不能滿足我們的要求,比如Spring Boot默認的健康端點就很有可能不能滿足

默認裝配 HealthIndicators

下列是依賴spring-boot-xxx-starter后相關HealthIndicator的實現(通過management.health.defaults.enabled 屬性可以禁用它們),但想要獲取一些額外的信息時,自定義的作用就體現出來了…

名稱 描述
CassandraHealthIndicator 檢查 Cassandra 數據庫是否啟動。
DiskSpaceHealthIndicator 檢查磁盤空間不足。
DataSourceHealthIndicator 檢查是否可以獲得連接 DataSource
ElasticsearchHealthIndicator 檢查 Elasticsearch 集群是否啟動。
InfluxDbHealthIndicator 檢查 InfluxDB 服務器是否啟動。
JmsHealthIndicator 檢查 JMS 代理是否啟動。
MailHealthIndicator 檢查郵件服務器是否啟動。
MongoHealthIndicator 檢查 Mongo 數據庫是否啟動。
Neo4jHealthIndicator 檢查 Neo4j 服務器是否啟動。
RabbitHealthIndicator 檢查 Rabbit 服務器是否啟動。
RedisHealthIndicator 檢查 Redis 服務器是否啟動。
SolrHealthIndicator 檢查 Solr 服務器是否已啟動。

健康端點(第一種方式)

實現HealthIndicator接口,根據自己的需要判斷返回的狀態是UP還是DOWN,功能簡單。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.battcn.health;

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

/**
* <p>自定義健康端點</p>
*
* @author Levin
* @since 2018/5/24 0024
*/
@Component("my1")
public class MyHealthIndicator implements HealthIndicator {

private static final String VERSION = "v1.0.0";

@Override
public Health health() {
int code = check();
if (code != 0) {
Health.down().withDetail("code", code).withDetail("version", VERSION).build();
}
return Health.up().withDetail("code", code)
.withDetail("version", VERSION).up().build();
}
private int check() {
return 0;
}
}

簡單測試

啟動項目,訪問 http://localhost:8080/actuator/health 看到如下內容代表配置成功

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
{
"status": "UP",
"details": {
"my1": {
"status": "UP",
"details": {
"code": 0,
"version": "v1.0.0"
}
},
"diskSpace": {
"status": "UP",
"details": {
"total": 100944310272,
"free": 55071866880,
"threshold": 10485760
}
}
}
}

健康端點(第二種方式)

繼承AbstractHealthIndicator抽象類,重寫doHealthCheck方法,功能比第一種要強大一點點,默認的DataSourceHealthIndicator 、 RedisHealthIndicator 都是這種寫法,內容回調中還做了異常的處理。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.battcn.health;

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

/**
* <p>自定義健康端點</p>
* <p>功能更加強大一點,DataSourceHealthIndicator / RedisHealthIndicator 都是這種寫法</p>
*
* @author Levin
* @since 2018/5/24 0024
*/
@Component("my2")
public class MyAbstractHealthIndicator extends AbstractHealthIndicator {

private static final String VERSION = "v1.0.0";

@Override
protected void doHealthCheck(Health.Builder builder) throws Exception {
int code = check();
if (code != 0) {
builder.down().withDetail("code", code).withDetail("version", VERSION).build();
}
builder.withDetail("code", code)
.withDetail("version", VERSION).up().build();
}

private int check() {
return 0;
}
}

簡單測試

啟動項目,訪問 http://localhost:8080/actuator/health 看到如下內容代表配置成功

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
"status": "UP",
"details": {
"my2": {
"status": "UP",
"details": {
"code": 0,
"version": "v1.0.0"
}
},
"my1": {...},
"diskSpace": {...}
}
}

定義自己的端點

上面介紹的 infohealth 都是spring-boot-actuator內置的,真正要實現自己的端點還得通過@Endpoint、 @ReadOperation、@WriteOperation、@DeleteOperation

注解介紹

不同請求的操作,調用時缺少必需參數,或者使用無法轉換為所需類型的參數,則不會調用操作方法,響應狀態將為400(錯誤請求)

  • @Endpoint 構建 rest api 的唯一路徑
  • @ReadOperation GET請求,響應狀態為 200 如果沒有返回值響應 404(資源未找到)
  • @WriteOperation POST請求,響應狀態為 200 如果沒有返回值響應 204(無響應內容)
  • @DeleteOperation DELETE請求,響應狀態為 200 如果沒有返回值響應 204(無響應內容)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.battcn.endpoint;

import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;

import java.util.HashMap;
import java.util.Map;

/**
* <p>@Endpoint 是構建 rest 的唯一路徑 </p>
* 不同請求的操作,調用時缺少必需參數,或者使用無法轉換為所需類型的參數,則不會調用操作方法,響應狀態將為400(錯誤請求)
* <P>@ReadOperation = GET 響應狀態為 200 如果沒有返回值響應 404(資源未找到) </P>
* <P>@WriteOperation = POST 響應狀態為 200 如果沒有返回值響應 204(無響應內容) </P>
* <P>@DeleteOperation = DELETE 響應狀態為 200 如果沒有返回值響應 204(無響應內容) </P>
*
* @author Levin
* @since 2018/5/24 0024
*/
@Endpoint(id = "battcn")
public class MyEndPoint {

@ReadOperation
public Map<String, String> hello() {
Map<String, String> result = new HashMap<>();
result.put("author", "Levin");
result.put("age", "24");
result.put("email", "1837307557@qq.com");
return result;
}
}

以為這就大功告成了嗎,現實告訴我的是spring-boot默認是不認識這玩意的,得申明成一個Bean(請看 主函數

皮一下很開心皮一下很開心

主函數

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.battcn;

import com.battcn.endpoint.MyEndPoint;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnEnabledEndpoint;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
* @author Levin
*/
@SpringBootApplication
public class Chapter13Application {

public static void main(String[] args) {
SpringApplication.run(Chapter13Application.class, args);
}

@Configuration
static class MyEndpointConfiguration {
@Bean
@ConditionalOnMissingBean
@ConditionalOnEnabledEndpoint
public MyEndPoint myEndPoint() {
return new MyEndPoint();
}
}
}

測試

完成准備事項后,啟動Chapter13Application 訪問 http://localhost:8080/actuator/battcn 看到如下內容代表配置成功…

1
2
3
4
5
{
"author": "Levin",
"age": "24",
"email": "1837307557@qq.com"
}

總結

參考文檔:https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#production-ready


免責聲明!

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



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