Actuator(監控)
1. Actuator介紹
通過前面的介紹我們明白了SpringBoot為什麼能夠很友善快捷的建構Web應用,那麼應用部署上線後的健康問題怎麼發現呢?在SpringBoot中給我們提供了Actuator來解決這個問題。
官網位址:
https://docs.spring.io/spring-boot/docs/current/reference/html/actuator.htmlSpring Boot includes a number of additional features to help you monitor and manage your application when you push it to production. You can choose to manage and monitor your application by using HTTP endpoints or with JMX. Auditing, health, and metrics gathering can also be automatically applied to your application.
Spring Boot包括許多附加特性,可以幫助您在将應用程式投入生産時監視和管理應用程式。您可以選擇使用HTTP端點或使用JMX來管理和監視應用程式。審計、運作狀況和度量收集也可以自動應用于您的應用程式。
使用Actuator我們需要添加依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2.端點(Endpoints)
執行器端點(endpoints)可用于監控應用及與應用進行互動,Spring Boot包含很多内置的端點,你也可以添加自己的。例如,health端點提供了應用的基本健康資訊。
每個端點都可以啟用或禁用。這控制着端點是否被建立,并且它的bean是否存在于應用程式上下文中。要遠端通路端點,還必須通過JMX或HTTP進行暴露,大部分應用選擇HTTP,端點的ID映射到一個帶
/actuator
字首的URL。例如,health端點預設映射到
/actuator/health
。
注意:
Spring Boot 2.0的端點基礎路徑由“/”調整到”/actuator”下,如:
/info
調整為
/actuator/info
可以通過以下配置改為和舊版本一緻:
management.endpoints.web.base-path=/
預設在隻放開了
health
和
info
兩個 Endpoint。
如果要放開更多的Endpoint,我們需要配置如下資訊
management.endpoints.web.exposure.include=*
以下是SpringBoot中提供的Endpoint。
ID | 描述 | 預設啟用 |
auditevents | 顯示目前應用程式的審計事件資訊 | Yes |
beans | 顯示一個應用中 的完整清單 | |
conditions | 顯示 (configuration and auto-configuration classes)的狀态及它們被應用或未被應用的原因 | |
configprops | 顯示一個所有 的集合清單 | |
env | 顯示來自Spring的 的屬性 | |
flyway | 顯示資料庫遷移路徑,如果有的話 | |
health | 顯示應用的 (當使用一個未認證連接配接通路時顯示一個簡單的’status’,使用認證連接配接通路則顯示全部資訊詳情) | |
info | 顯示任意的 | |
liquibase | 展示任何Liquibase資料庫遷移路徑,如果有的話 | |
metrics | 展示目前應用的 資訊 | |
mappings | 路徑的集合清單 | |
scheduledtasks | 顯示應用程式中的 | |
sessions | 允許從Spring會話支援的會話存儲中檢索和删除(retrieval and deletion)使用者會話。使用Spring Session對反應性Web應用程式的支援時不可用。 | |
shutdown | 允許應用以優雅的方式關閉(預設情況下不啟用) | No |
threaddump | 執行一個線程dump |
shutdown端點預設是關閉的,我們可以打開它
# 放開 shutdown 端點
management.endpoint.shutdown.enabled=true
shutdown
隻支援post方式送出,我們可以使用
POSTMAN
來送出或者使用IDEA中提供的工具來送出。
如果使用web應用(Spring MVC, Spring WebFlux, 或者 Jersey),你還可以使用以下端點:
heapdump | 傳回一個GZip壓縮的 堆dump檔案 | |
jolokia | 通過HTTP暴露 (當Jolokia在類路徑上時,WebFlux不可用) | |
logfile | 傳回 (如果設定了logging.file或logging.path屬性的話),支援使用HTTP Range頭接收日志檔案内容的部分資訊 | |
prometheus | 以可以被Prometheus伺服器抓取的格式顯示 |
現在我們看的
health
資訊比較少,如果我們需要看更詳細的資訊,可以配置如下
# health 顯示詳情
management.endpoint.health.show-details=always
再通路測試
這樣一來
health
不僅僅可以監控SpringBoot本身的狀态,還可以監控其他元件的資訊,比如我們開啟Redis服務。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
添加Redis的配置資訊
# Redis的 host 資訊
spring.redis.host=192.168.100.120
重新開機服務,檢視
http://localhost:8080/actuator/health
3.監控類型
介紹幾個監控中比較重要的類型
3.1 health
顯示的系統的健康資訊,這個在上面的案例中講解的比較多,不再贅述。
3.2 metrics
metrics 端點用來傳回目前應用的各類重要度量名額,比如記憶體資訊,線程資訊,垃圾回收資訊等。如下:
各個名額資訊的較長的描述:
序号 | 參數 | 參數說明 | 是否監控 | 監控手段 | 重要度 |
JVM | |||||
1 | jvm.memory.max | JVM 最大記憶體 | |||
2 | jvm.memory.committed | JVM 可用記憶體 | 是 | 展示并監控堆記憶體和 Metaspace | 重要 |
3 | jvm.memory.used | JVM 已用記憶體 | |||
4 | jvm.buffer.memory.used | JVM 緩沖區已用記憶體 | |||
5 | jvm.buffer.count | 目前緩沖區數 | |||
6 | jvm.threads.daemon | JVM 守護線程數 | 顯示在監控頁面 | ||
7 | jvm.threads.live | JVM 目前活躍線程數 | 顯示在監控頁面;監控達到門檻值時報警 | ||
8 | jvm.threads.peak | JVM 峰值線程數 | |||
9 | jvm.classes.loaded | 加載 classes 數 | |||
10 | jvm.classes.unloaded | 未加載的 classes 數 | |||
11 | jvm.gc.memory.allocated | GC 時,年輕代配置設定的記憶體空間 | |||
12 | jvm.gc.memory.promoted | GC 時,老年代配置設定的記憶體空間 | |||
13 | jvm.gc.max.data.size | GC 時,老年代的最大記憶體空間 | |||
14 | jvm.gc.live.data.size | FullGC 時,老年代的記憶體空間 | |||
15 | jvm.gc.pause | GC 耗時 | |||
TOMCAT | |||||
16 | tomcat.sessions.created | tomcat 已建立 session 數 | |||
17 | tomcat.sessions.expired | tomcat 已過期 session 數 | |||
18 | tomcat.sessions.active.current | tomcat 活躍 session 數 | |||
19 | tomcat.sessions.active.max | tomcat 最多活躍 session 數 | 顯示在監控頁面,超過門檻值可報警或者進行動态擴容 | ||
20 | tomcat.sessions.alive.max.second | tomcat 最多活躍 session 數持續時間 | |||
21 | tomcat.sessions.rejected | 超過 session 最大配置後,拒絕的 session 個數 | 顯示在監控頁面,友善分析問題 | ||
22 | tomcat.global.error | 錯誤總數 | |||
23 | tomcat.global.sent | 發送的位元組數 | |||
24 | tomcat.global.request.max | request 最長時間 | |||
25 | tomcat.global.request | 全局 request 次數和時間 | |||
26 | tomcat.global.received | 全局 received 次數和時間 | |||
27 | tomcat.servlet.request | servlet 的請求次數和時間 | |||
28 | tomcat.servlet.error | servlet 發生錯誤總數 | |||
29 | tomcat.servlet.request.max | servlet 請求最長時間 | |||
30 | tomcat.threads.busy | tomcat 繁忙線程 | 顯示在監控頁面,據此檢查是否有線程夯住 | ||
31 | tomcat.threads.current | tomcat 目前線程數(包括守護線程) | |||
32 | tomcat.threads.config.max | tomcat 配置的線程最大數 | |||
33 | tomcat.cache.access | tomcat 讀取緩存次數 | |||
34 | tomcat.cache.hit | tomcat 緩存命中次數 | |||
CPU | |||||
35 | system.cpu.count | CPU 數量 | |||
36 | system.load.average.1m | load average | 超過門檻值報警 | ||
37 | system.cpu.usage | 系統 CPU 使用率 | |||
38 | process.cpu.usage | 目前程序 CPU 使用率 | |||
39 | http.server.requests | http 請求調用情況 | 顯示 10 個請求量最大,耗時最長的 URL;統計非 200 的請求量 | ||
40 | process.uptime | 應用已運作時間 | |||
41 | process.files.max | 允許最大句柄數 | 配合目前打開句柄數使用 | ||
42 | process.start.time | 應用啟動時間點 | |||
43 | process.files.open | 目前打開句柄數 | 監控檔案句柄使用率,超過門檻值後報警 |
如果要檢視具體的度量資訊的話,直接在通路位址後面加上度量資訊即可:
http://localhost:8080/actuator/metrics/jvm.buffer.memory.used
添加自定義的統計名額
除了使用metrics端點預設的這些統計名額外,我們還可以實作自定義統計名額,metrics提供了4中基本的度量類型:
- gauge 計量器,最簡單的度量類型,隻有一個簡單的傳回值,他用來記錄一些對象或者事物的瞬時值。
- Counter 計數器 簡單了解就是一種隻增不減的計數器,它通常用于記錄服務的請求數量,完成的任務數量,錯誤的發生數量
- Timer 計時器 可以同時測量一個特定的代碼邏輯塊的調用(執行)速度和它的時間分布。簡單來說,就是在調用結束的時間點記錄整個調用塊執行的總時間,适用于測量短時間執行的事件的耗時分布,例如消息隊列消息的消費速率。
- Summary 摘要)用于跟蹤事件的分布。它類似于一個計時器,但更一般的情況是,它的大小并不一定是一段時間的測量值。在 micrometer 中,對應的類是 DistributionSummary,它的用法有點像 Timer,但是記錄的值是需要直接指定,而不是通過測量一個任務的執行時間。
測試:
@RestController
public class UserController {
static final Counter userCounter = Metrics.counter("user.counter.total","services","bobo");
private Timer timer = Metrics.timer("user.test.timer","timer","timersample");
private DistributionSummary summary = Metrics.summary("user.test.summary","summary","summarysample");
@GetMapping("/hello")
public String hello(){
// Gauge
Metrics.gauge("user.test.gauge",8);
// Counter
userCounter.increment(1);
// timer
timer.record(()->{
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
summary.record(2);
summary.record(3);
summary.record(4);
return "Hello";
}
}
通路 http://localhost:8080/hello 這個請求後在看 metrics 資訊
多出了我們自定義的度量資訊。
3.3 loggers
loggers是用來檢視目前項目每個包的日志級别的。
預設的是info級别。
修改日志級别:
發送POST請求到 http://localhost:8080/actuator/loggers/[包路徑]
請求參數為
{
"configuredLevel":"DEBUG"
}
通過POSTMAN來發送消息
然後再檢視日志級别發現已經變動了
控制台也可以看到
3.4 info
顯示任意的應用資訊。我們可以在 properties 中來定義
通路:http://localhost:8080/actuator/info
4.自定義Endpoint
如果我們需要擴充Endpoint,這時我們可以自定義實作,我們可以在類的頭部定義如下的注解
@Endpoint
@WebEndpoint
@ControllerEndpoint
@RestControllerEndpoint
@ServletEndpoint
再給方法添加@ReadOperation,@ WritOperation或@DeleteOperation注釋後,該方法将通過JMX自動公開,并且在Web應用程式中也通過HTTP公開。
于方法的注解有以下三種,分别對應get post delete 請求
Operation | HTTP method |
@ReadOperation | GET |
@WriteOperation | POST |
@DeleteOperation | DELETE |
案例:
/**
* 自定義Endpoint
*/
@Component
@Endpoint(id = "sessions")
public class MyHealthEndPoint {
/**
* @Selector 擷取傳遞的參數
*/
@ReadOperation
public Info get(@Selector String name){
return new Info(name,"18");
}
}
5.Actuator的兩種監控形态
- http
- jmx [Java Management Extensions] Java管理擴充
放開jmx
# 放開 jmx 的 endpoint
management.endpoints.jmx.exposure.include=*
spring.jmx.enabled=true
通過jdk中提供的jconsole來檢視
6.監控系統
SpringBoot可以收集監控資料,但是檢視不是很友善,這時我們可以選擇開源的監控系統來解決,比如Prometheus
- 資料采集
- 資料存儲
- 可視化
Prometheus在可視化方面效果不是很好,可以使用grafana來實作
6.1 Prometheus
先來安裝Prometheus:官網:
https://prometheus.io/download/然後通過wget指令來直接下載下傳
wget https://github.com/prometheus/prometheus/releases/download/v2.28.1/prometheus-2.28.1.linux-amd64.tar.gz
然後配置Prometheus。
- job_name: 'Prometheus'
static_configs:
metrics_path: '/actuator/prometheus'
scrape_interval: 5s
- targets: ['192.168.127.1:8080']
labels:
instance: Prometheus
- job_name:任務名稱
- metrics_path: 名額路徑
- targets:執行個體位址/項目位址,可配置多個
- scrape_interval: 多久采集一次
- scrape_timeout: 采集逾時時間
執行腳本啟動應用
./prometheus --config.file=prometheus.yml
通路應用: http://ip:9090
然後在我們的SpringBoot服務中添加 Prometheus的端點,先添加必要的依賴
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
然後就會有該端點資訊
Prometheus伺服器可以周期性的爬取這個endpoint來擷取metrics資料,然後可以看到
6.2 Grafana
可視化工具:
https://grafana.com/grafana/download通過wget指令下載下傳
wget https://dl.grafana.com/oss/release/grafana-8.0.6-1.x86_64.rpm
sudo yum install grafana-8.0.6-1.x86_64.rpm
啟動指令
sudo service grafana-server start
sudo service grafana-server status
通路的位址是 http://ip:3000 預設的帳号密碼 admin/admin
登入進來後的頁面
添加資料源:
添加Dashboards
https://grafana.com/grafana/dashboards搜尋SpringBoot的 Dashboards
找到Dashboards的ID
然後導入即可
點選Load出現如下界面
然後就可以看到對應的監控資料了
到此Actuator的相關内容就介紹完了!!!