Spring Boot,actuator服務(wù)監(jiān)控與管理詳解

Actuator 簡介
官方媽媽說:

Spring 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.

傳送至官方:https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#production-ready

大概的意思是說,Spring boot 包括了許多額外的、可配置的特性,可以幫助我們來管理、監(jiān)控在生產(chǎn)環(huán)境下運(yùn)行的應(yīng)用程序,可使用HTTP或者JMX收集應(yīng)用程序的各項(xiàng)指標(biāo),包括但不限定于審計(jì)(Auditing)、健康(health)狀態(tài)信息、數(shù)據(jù)采集(metrics gathering)統(tǒng)計(jì)等監(jiān)控運(yùn)維的功能.

同時(shí),我們可以擴(kuò)展 Actuator 端點(diǎn)(Endpoint) 自定義監(jiān)控指標(biāo)。這些指標(biāo)都是以 JSON 接口數(shù)據(jù)的方式呈現(xiàn)。而使用 Spring Boot Admin 可以實(shí)現(xiàn)這些 JSON 接口數(shù)據(jù)的界面展現(xiàn)。
本章介紹 Spring Boot Actuator 和使用Spring Boot Admin實(shí)現(xiàn)對(duì) Spring Boot應(yīng)用的監(jiān)控與管理。

使用方式 / Usage

Maven , add pom dependency:

<dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

Gradle, use the following declaration:

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

啟動(dòng)的時(shí)候就會(huì)有下面這些提示.

Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.uti
Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.ser
Mapped URL path [/webjars/**] onto handler of type [class org.springframework.we
Mapped URL path [/**] onto handler of type [class org.springframework.web.servle
Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframewor
Mapped "{[/metrics/{name:.*}],methods=[GET],produces=[application/json]}" onto p
Mapped "{[/metrics || /metrics.json],methods=[GET],produces=[application/json]}"
Mapped "{[/trace || /trace.json],methods=[GET],produces=[application/json]}" ont
Mapped "{[/configprops || /configprops.json],methods=[GET],produces=[application
Mapped "{[/autoconfig || /autoconfig.json],methods=[GET],produces=[application/j
Mapped "{[/info || /info.json],methods=[GET],produces=[application/json]}" onto 
Mapped "{[/dump || /dump.json],methods=[GET],produces=[application/json]}" onto 
Mapped "{[/mappings || /mappings.json],methods=[GET],produces=[application/json]
Mapped "{[/health || /health.json],produces=[application/json]}" onto public jav
Mapped "{[/beans || /beans.json],methods=[GET],produces=[application/json]}" ont
Mapped "{[/env/{name:.*}],methods=[GET],produces=[application/json]}" onto publi
Mapped "{[/env || /env.json],methods=[GET],produces=[application/json]}" onto pu

- actuator 的端點(diǎn)分為3類

  1. 應(yīng)用配置類

/configprops /autoconfig /beans /env /info /mappings

  1. 度量指標(biāo)類

/dump /health

  1. 操作控制類
  • 下面找?guī)讉€(gè)來解釋

    • /autoconfig

自動(dòng)化配置報(bào)告,可以看出一些自動(dòng)化配置為什么沒有生效

autoconfig
  • /beans

可以看到定義的所有的bean

beans
  • /configprops

可以看到application.properties里面的信息

configprops
  • /env
env
  • /mappings
mappings
  • /info

返回application.properties文件中info開頭的配置信息,如:

# /info端點(diǎn)信息配置
info.app.name=spring-boot-hello
info.app.version=v1.0.0

info

下面是度量指標(biāo)類

  • /metrics
metrics

我們也可以自定實(shí)現(xiàn) CounterService 接口來實(shí)現(xiàn)count指標(biāo).

metrics

也可以用 [/metrics/{name:.*}] 如: /metrics/mem.free 來獲取單個(gè)指標(biāo)信息

metrics
  • /health

可以通過實(shí)現(xiàn) HealthIndicator 接口來實(shí)現(xiàn)健康檢查,返回值的狀態(tài)信息在org.springframework.boot.actuate.health.Status內(nèi)

health
health
  • /dump

調(diào)用 java.lang.management.ThreadMXBean
public ThreadInfo[] dumpAllThreads(boolean lockedMonitors, boolean lockedSynchronizers); 方法來返回活動(dòng)線程的信息

image
image
  • 操作控制類

如:/shutdown ,通過在application.properties文件中添加
endpoints.shutdown.enabled=true
來開啟

shutdown

END

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容