actuator 通過暴露一系列的endpoints可以讓開發者快速了解spring boot的各項運行指標,比如:線程數,jvm剩余內存等一系列參數。
啟用方法很簡單,參考下面:
dependencies { compile('org.springframework.boot:spring-boot-starter-thymeleaf') compile('org.springframework.boot:spring-boot-devtools') compile('org.springframework.boot:spring-boot-starter-actuator') compile('org.springframework.boot:spring-boot-starter-test') compileOnly('org.projectlombok:lombok') }
關鍵是添加spring-boot-starter-actuator依賴項即可,下表是actuator提供的endpoints列表(從官網文檔上抄過來的)
ID | Description | Sensitive Default |
---|---|---|
|
Provides a hypermedia-based “discovery page” for the other endpoints.Requires Spring HATEOAS to be on the classpath. |
true |
|
Exposes audit events information for the current application. |
true |
|
Displays an auto-configuration report showing all auto-configuration candidates and the reason why they ‘were’ or ‘were not’ applied. |
true |
|
Displays a complete list of all the Spring beans in your application. |
true |
|
Displays a collated list of all |
true |
|
Performs a thread dump. |
true |
|
Exposes properties from Spring’s |
true |
|
Shows any Flyway database migrations that have been applied. |
true |
|
Shows application health information (when the application is secure, a simple ‘status’ when accessed over an unauthenticated connection or full message details when authenticated). |
false |
|
Displays arbitrary application info. |
false |
|
Shows and modifies the configuration of loggers in the application. |
true |
|
Shows any Liquibase database migrations that have been applied. |
true |
|
Shows ‘metrics’ information for the current application. |
true |
|
Displays a collated list of all |
true |
|
Allows the application to be gracefully shutdown (not enabled by default). |
true |
|
Displays trace information (by default the last 100 HTTP requests). |
true |
這張表中,有很多信息其實是敏感信息,并不適合匿名訪問(特別是在公網環境下),所以默認情況下,如果想訪問類似 http://localhost:8081/metrics 會看到以下錯誤:
比較好的做法是,將這些endpoints的端口,包括訪問路徑與常規應用的端口分開,application.yml可以參考下面的配置:
server: port: 8081 spring: main: banner-mode: "off" devtools: restart: trigger-file: .trigger thymeleaf: cache: false management: security: enabled: false #關掉安全認證 port: 1101 #管理端口調整成1101 context-path: /admin #actuator的訪問路徑
如果在公網環境,建議在防火墻上做下限制,僅允許8081進來,1101用于內網訪問即可,這樣相對比較安全,也不用繁瑣的輸入密碼。
訪問下http://localhost:1101/admin/metrics 可以看到類似以下輸出:
{ mem: 466881, mem.free: 289887, processors: 4, instance.uptime: 10947, uptime: 18135, systemload.average: 3.12646484375, heap.committed: 411648, heap.init: 131072, heap.used: 121760, heap: 1864192, nonheap.committed: 56192, nonheap.init: 2496, nonheap.used: 55234, nonheap: 0, threads.peak: 27, threads.daemon: 19, threads.totalStarted: 32, threads: 22, classes: 6755, classes.loaded: 6755, classes.unloaded: 0, gc.ps_scavenge.count: 8, gc.ps_scavenge.time: 136, gc.ps_marksweep.count: 2, gc.ps_marksweep.time: 193, httpsessions.max: -1, httpsessions.active: 0 }
jvm的內存,cpu核數,線程數,gc情況一目了然。其它指標大概含義如下(網上抄來的)
系統信息:
包括處理器數量processors、運行時間uptime和instance.uptime、系統平均負載systemload.average。
mem.*:
內存概要信息,包括分配給應用的總內存數量以及當前空閑的內存數量。這些信息來自java.lang.Runtime。
heap.*:
堆內存使用情況。這些信息來自java.lang.management.MemoryMXBean接口中getHeapMemoryUsage方法獲取的java.lang.management.MemoryUsage。
nonheap.*:
非堆內存使用情況。這些信息來自java.lang.management.MemoryMXBean接口中getNonHeapMemoryUsage方法獲取的java.lang.management.MemoryUsage。
threads.*:
線程使用情況,包括線程數、守護線程數(daemon)、線程峰值(peak)等,這些數據均來自java.lang.management.ThreadMXBean。
classes.*:
應用加載和卸載的類統計。這些數據均來自java.lang.management.ClassLoadingMXBean。
gc.*:
垃圾收集器的詳細信息,包括垃圾回收次數gc.ps_scavenge.count、垃圾回收消耗時間gc.ps_scavenge.time、標記-清除算法的次數gc.ps_marksweep.count、標記-清除算法的消耗時間gc.ps_marksweep.time。這些數據均來自java.lang.management.GarbageCollectorMXBean。
httpsessions.*:
Tomcat容器的會話使用情況。包括最大會話數httpsessions.max和活躍會話數httpsessions.active。該度量指標信息僅在引入了嵌入式Tomcat作為應用容器的時候才會提供。
gauge.*:
HTTP請求的性能指標之一,它主要用來反映一個絕對數值。比如上面示例中的gauge.response.hello: 5,它表示上一次hello請求的延遲時間為5毫秒。
counter.*:
HTTP請求的性能指標之一,它主要作為計數器來使用,記錄了增加量和減少量。如上示例中counter.status.200.hello: 11,它代表了hello請求返回200狀態的次數為11
結合其它一些工具把這些信息采集到grafana里,就有得到一系列很實用的監控圖表數據,比如:
其它endpoint,就不一一展示了,大家有興趣可以自行研究,最后要提一下的是shutdown這個endpoint,它可以實現優雅停機,這在線上部署時很有用,發布前先調用這個url,讓應用優雅停掉,再部署新的代碼,這樣就不會導致正在處理的請求被中斷,不過默認該功能是關閉的,可參考下面的設置啟用:
endpoints: shutdown: enabled: true
而且出于安全考慮,該url只能以post方式訪問,下圖是用postman模擬post訪問 http://locahost:1101/admin/shutdown的效果:
同時在日志里也能看到應用確實被關閉:
參考文章:
http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#production-ready
文章列表