Spring Boot 的 Actuator 提供了很多生產(chǎn)級(jí)的特性搅幅,比如監(jiān)控和度量Spring Boot 應(yīng)用程序。Actuator 的這些特性可以通過眾多 REST 接口、遠(yuǎn)程 shell 和 JMX 獲得全封。
一嵌灰、Actuator 的 REST 接口
Spring Boot Actuator 的關(guān)鍵特性是在應(yīng)用程序里提供眾多 Web 接口,通過它們了解應(yīng)用程序運(yùn)行時(shí)的內(nèi)部狀況督函。Actuator 提供了 13 個(gè)接口,可以分為三大類:配置接口激挪、度量接口和其它接口辰狡,具體如下表所示。
HTTP 方法 | 路徑 | 描述 |
---|---|---|
GET | /autoconfig | 提供了一份自動(dòng)配置報(bào)告垄分,記錄哪些自動(dòng)配置條件通過了宛篇,哪些沒通過 |
GET | /configprops | 描述配置屬性(包含默認(rèn)值)如何注入Bean |
GET | /beans | 描述應(yīng)用程序上下文里全部的Bean,以及它們的關(guān)系 |
GET | /dump | 獲取線程活動(dòng)的快照 |
GET | /env | 獲取全部環(huán)境屬性 |
GET | /env/{name} | 根據(jù)名稱獲取特定的環(huán)境屬性值 |
GET | /health | 報(bào)告應(yīng)用程序的健康指標(biāo)薄湿,這些值由HealthIndicator的實(shí)現(xiàn)類提供 |
GET | /info | 獲取應(yīng)用程序的定制信息叫倍,這些信息由info打頭的屬性提供 |
GET | /mappings | 描述全部的URI路徑,以及它們和控制器(包含Actuator端點(diǎn))的映射關(guān)系 |
GET | /metrics | 報(bào)告各種應(yīng)用程序度量信息豺瘤,比如內(nèi)存用量和HTTP請(qǐng)求計(jì)數(shù) |
GET | /metrics/{name} | 報(bào)告指定名稱的應(yīng)用程序度量值 |
POST | /shutdown | 關(guān)閉應(yīng)用程序吆倦,要求endpoints.shutdown.enabled設(shè)置為true |
GET | /trace | 提供基本的HTTP請(qǐng)求跟蹤信息(時(shí)間戳、HTTP頭等) |
要啟用 Actuator 的端點(diǎn)坐求,只要在項(xiàng)目中引入 Actuator 的依賴即可蚕泽。對(duì)于 Maven 依賴,引入的依賴是這樣的:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
1. 查看配置明細(xì)
Actuator 有一些接口不僅可以顯示組件映射關(guān)系桥嗤,還可以告訴你自動(dòng)配置在配置 Spring 應(yīng)用程序上下文時(shí)做了哪些決策须妻。
1.1 獲得 Bean 裝配報(bào)告
要了解應(yīng)用程序中 Spring 上下文的情況派任,最重要的接口就是 /beans
。它會(huì)返回一個(gè) JSON 文檔璧南,描述上下文里每個(gè) bean 的情況掌逛,包括其 Java 類型以及注入的其它 bean。返回的信息如下所示:
[
{
"context": "application",
"parent": null,
"beans": [
{
"bean": "demoApplication",
"aliases": [],
"scope": "singleton",
"type": "com.example.DemoApplication$$EnhancerBySpringCGLIB$$88686e04",
"resource": "null",
"dependencies": []
},
{
"bean": "org.springframework.boot.autoconfigure.internalCachingMetadataReaderFactory",
"aliases": [],
"scope": "singleton",
"type": "org.springframework.core.type.classreading.CachingMetadataReaderFactory",
"resource": "null",
"dependencies": []
}
]
}
]
所有的 Bean 條目都有五類信息:
- bean:Spring 應(yīng)用程序上下文中的 Bean 名稱或 ID司倚。
- resource:.class 文件的物理位置豆混,通常是一個(gè) URL,指向構(gòu)建出的 JAR 文件动知。這會(huì)隨著應(yīng)用程序的構(gòu)建和運(yùn)行方式發(fā)生變化皿伺。
- dependencies:當(dāng)前 Bean 注入的 Bean ID 列表。
- scope:Bean 的作用域(通常是單例盒粮,這也是默認(rèn)作用域)鸵鸥。
- type:Bean 的 Java 類型。
1.2 詳解自動(dòng)配置
/autoconfig
接口能告訴你為什么會(huì)有這個(gè) bean 丹皱,或者為什么沒有這個(gè) bean妒穴。
{
"positiveMatches": {
"AuditAutoConfiguration#auditListener": [
{
"condition": "OnBeanCondition",
"message": "@ConditionalOnMissingBean (types: org.springframework.boot.actuate.audit.listener.AbstractAuditListener; SearchStrategy: all) did not find any beans"
}
],
"MultipartAutoConfiguration#multipartConfigElement": [
{
"condition": "OnBeanCondition",
"message": "@ConditionalOnMissingBean (types: javax.servlet.MultipartConfigElement; SearchStrategy: all) did not find any beans"
}
]
},
"negativeMatches": {
"AuditAutoConfiguration#authenticationAuditListener": {
"notMatched": [
{
"condition": "OnClassCondition",
"message": "@ConditionalOnClass did not find required class 'org.springframework.security.authentication.event.AbstractAuthenticationEvent'"
}
],
"matched": []
},
"AuditAutoConfiguration#authorizationAuditListener": {
"notMatched": [
{
"condition": "OnClassCondition",
"message": "@ConditionalOnClass did not find required class 'org.springframework.security.access.event.AbstractAuthorizationEvent'"
}
],
"matched": []
}
}
}
1.3 查看配置屬性
/env
接口會(huì)生成應(yīng)用程序可用的所有環(huán)境屬性的列表,無論這些屬性是否用到摊崭。這其中包括環(huán)境變量讼油、JVM 屬性、命令行參數(shù)呢簸,以及 application.properties 或 application.yml 文件提供的屬性矮台。
{
"profiles": [],
"server.ports": {
"local.server.port": 8080
},
"servletContextInitParams": {},
"systemProperties": {
"java.runtime.name": "Java(TM) SE Runtime Environment",
"spring.output.ansi.enabled": "always",
"sun.boot.library.path": "/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib",
"java.vm.version": "25.121-b13",
"gopherProxySet": "false",
"java.vm.vendor": "Oracle Corporation",
"java.vendor.url": "http://java.oracle.com/",
"path.separator": ":",
"idea.launcher.port": "7532",
"java.vm.name": "Java HotSpot(TM) 64-Bit Server VM",
"file.encoding.pkg": "sun.io",
"user.country": "CN",
"sun.java.launcher": "SUN_STANDARD",
"sun.os.patch.level": "unknown",
"PID": "2716",
"java.vm.specification.name": "Java Virtual Machine Specification",
"user.dir": "/Users/FlySheep/FlySheep/Code/ServerCode/SpringBoot_Demo/demo",
"java.runtime.version": "1.8.0_121-b13",
"java.awt.graphicsenv": "sun.awt.CGraphicsEnvironment",
"org.jboss.logging.provider": "slf4j",
"java.endorsed.dirs": "/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/endorsed",
"os.arch": "x86_64",
"visualvm.id": "11136812717769",
"java.io.tmpdir": "/var/folders/d6/zn9yrwns46j4pwbl1_mv_6hw0000gn/T/",
"line.separator": "\n",
"java.vm.specification.vendor": "Oracle Corporation",
"os.name": "Mac OS X",
"sun.jnu.encoding": "UTF-8",
"spring.beaninfo.ignore": "true",
"java.library.path": "/Users/FlySheep/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.",
"java.specification.name": "Java Platform API Specification",
"java.class.version": "52.0",
"sun.management.compiler": "HotSpot 64-Bit Tiered Compilers",
"os.version": "10.11",
"user.home": "/Users/FlySheep",
"catalina.useNaming": "false",
"user.timezone": "Asia/Shanghai",
"java.awt.printerjob": "sun.lwawt.macosx.CPrinterJob",
"file.encoding": "UTF-8",
"idea.launcher.bin.path": "/Applications/IntelliJ IDEA.app/Contents/bin",
"java.specification.version": "1.8",
"catalina.home": "/private/var/folders/d6/zn9yrwns46j4pwbl1_mv_6hw0000gn/T/tomcat.6698906163877756728.8080",
"java.class.path": "/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/ext/jaccess.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/lib/packager.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/lib/tools.jar:/Users/FlySheep/FlySheep/Code/ServerCode/SpringBoot_Demo/demo/target/classes:/Users/FlySheep/.m2/repository/org/springframework/boot/spring-boot-starter-web/1.5.2.RELEASE/spring-boot-starter-web-1.5.2.RELEASE.jar:/Users/FlySheep/.m2/repository/org/springframework/boot/spring-boot-starter/1.5.2.RELEASE/spring-boot-starter-1.5.2.RELEASE.jar:/Users/FlySheep/.m2/repository/org/springframework/boot/spring-boot/1.5.2.RELEASE/spring-boot-1.5.2.RELEASE.jar:/Users/FlySheep/.m2/repository/org/springframework/boot/spring-boot-autoconfigure/1.5.2.RELEASE/spring-boot-autoconfigure-1.5.2.RELEASE.jar:/Users/FlySheep/.m2/repository/org/springframework/boot/spring-boot-starter-logging/1.5.2.RELEASE/spring-boot-starter-logging-1.5.2.RELEASE.jar:/Users/FlySheep/.m2/repository/ch/qos/logback/logback-classic/1.1.11/logback-classic-1.1.11.jar:/Users/FlySheep/.m2/repository/ch/qos/logback/logback-core/1.1.11/logback-core-1.1.11.jar:/Users/FlySheep/.m2/repository/org/slf4j/jcl-over-slf4j/1.7.24/jcl-over-slf4j-1.7.24.jar:/Users/FlySheep/.m2/repository/org/slf4j/jul-to-slf4j/1.7.24/jul-to-slf4j-1.7.24.jar:/Users/FlySheep/.m2/repository/org/slf4j/log4j-over-slf4j/1.7.24/log4j-over-slf4j-1.7.24.jar:/Users/FlySheep/.m2/repository/org/yaml/snakeyaml/1.17/snakeyaml-1.17.jar:/Users/FlySheep/.m2/repository/org/springframework/boot/spring-boot-starter-tomcat/1.5.2.RELEASE/spring-boot-starter-tomcat-1.5.2.RELEASE.jar:/Users/FlySheep/.m2/repository/org/apache/tomcat/embed/tomcat-embed-core/8.5.11/tomcat-embed-core-8.5.11.jar:/Users/FlySheep/.m2/repository/org/apache/tomcat/embed/tomcat-embed-el/8.5.11/tomcat-embed-el-8.5.11.jar:/Users/FlySheep/.m2/repository/org/apache/tomcat/embed/tomcat-embed-websocket/8.5.11/tomcat-embed-websocket-8.5.11.jar:/Users/FlySheep/.m2/repository/org/hibernate/hibernate-validator/5.3.4.Final/hibernate-validator-5.3.4.Final.jar:/Users/FlySheep/.m2/repository/javax/validation/validation-api/1.1.0.Final/validation-api-1.1.0.Final.jar:/Users/FlySheep/.m2/repository/org/jboss/logging/jboss-logging/3.3.0.Final/jboss-logging-3.3.0.Final.jar:/Users/FlySheep/.m2/repository/com/fasterxml/classmate/1.3.3/classmate-1.3.3.jar:/Users/FlySheep/.m2/repository/com/fasterxml/jackson/core/jackson-databind/2.8.7/jackson-databind-2.8.7.jar:/Users/FlySheep/.m2/repository/com/fasterxml/jackson/core/jackson-annotations/2.8.0/jackson-annotations-2.8.0.jar:/Users/FlySheep/.m2/repository/com/fasterxml/jackson/core/jackson-core/2.8.7/jackson-core-2.8.7.jar:/Users/FlySheep/.m2/repository/org/springframework/spring-web/4.3.7.RELEASE/spring-web-4.3.7.RELEASE.jar:/Users/FlySheep/.m2/repository/org/springframework/spring-aop/4.3.7.RELEASE/spring-aop-4.3.7.RELEASE.jar:/Users/FlySheep/.m2/repository/org/springframework/spring-beans/4.3.7.RELEASE/spring-beans-4.3.7.RELEASE.jar:/Users/FlySheep/.m2/repository/org/springframework/spring-context/4.3.7.RELEASE/spring-context-4.3.7.RELEASE.jar:/Users/FlySheep/.m2/repository/org/springframework/spring-webmvc/4.3.7.RELEASE/spring-webmvc-4.3.7.RELEASE.jar:/Users/FlySheep/.m2/repository/org/springframework/spring-expression/4.3.7.RELEASE/spring-expression-4.3.7.RELEASE.jar:/Users/FlySheep/.m2/repository/org/springframework/boot/spring-boot-starter-actuator/1.5.2.RELEASE/spring-boot-starter-actuator-1.5.2.RELEASE.jar:/Users/FlySheep/.m2/repository/org/springframework/boot/spring-boot-actuator/1.5.2.RELEASE/spring-boot-actuator-1.5.2.RELEASE.jar:/Users/FlySheep/.m2/repository/org/slf4j/slf4j-api/1.7.24/slf4j-api-1.7.24.jar:/Users/FlySheep/.m2/repository/org/springframework/spring-core/4.3.7.RELEASE/spring-core-4.3.7.RELEASE.jar:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar",
"user.name": "FlySheep",
"java.vm.specification.version": "1.8",
"sun.java.command": "com.intellij.rt.execution.application.AppMain com.example.DemoApplication",
"java.home": "/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre",
"sun.arch.data.model": "64",
"user.language": "zh",
"java.specification.vendor": "Oracle Corporation",
"awt.toolkit": "sun.lwawt.macosx.LWCToolkit",
"java.vm.info": "mixed mode",
"java.version": "1.8.0_121",
"java.ext.dirs": "/Users/FlySheep/Library/Java/Extensions:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/ext:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java",
"sun.boot.class.path": "/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/sunrsasign.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/classes",
"java.awt.headless": "true",
"java.vendor": "Oracle Corporation",
"catalina.base": "/private/var/folders/d6/zn9yrwns46j4pwbl1_mv_6hw0000gn/T/tomcat.6698906163877756728.8080",
"file.separator": "/",
"java.vendor.url.bug": "http://bugreport.sun.com/bugreport/",
"sun.io.unicode.encoding": "UnicodeBig",
"sun.cpu.endian": "little",
"sun.cpu.isalist": ""
},
"systemEnvironment": {
"PATH": "/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/bin:/usr/local/mysql/bin:/Users/FlySheep/FlySheep/JavaTools/apache-maven-3.3.9/bin/bin",
"SHELL": "/bin/zsh",
"PAGER": "less",
"LSCOLORS": "Gxfxcxdxbxegedabagacad",
"OLDPWD": "/Applications/IntelliJ IDEA.app/Contents/bin",
"USER": "FlySheep",
"VERSIONER_PYTHON_PREFER_32_BIT": "no",
"ZSH": "/Users/FlySheep/.oh-my-zsh",
"TMPDIR": "/var/folders/d6/zn9yrwns46j4pwbl1_mv_6hw0000gn/T/",
"SSH_AUTH_SOCK": "/private/tmp/com.apple.launchd.vZEKsGdZ1T/Listeners",
"JAVA_MAIN_CLASS_2716": "com.intellij.rt.execution.application.AppMain",
"XPC_FLAGS": "0x0",
"VERSIONER_PYTHON_VERSION": "2.7",
"__CF_USER_TEXT_ENCODING": "0x1F5:0x19:0x34",
"Apple_PubSub_Socket_Render": "/private/tmp/com.apple.launchd.5VgRuDCQ5D/Render",
"LOGNAME": "FlySheep",
"LESS": "-R",
"LC_CTYPE": "",
"PWD": "/Users/FlySheep/FlySheep/Code/ServerCode/SpringBoot_Demo/demo",
"XPC_SERVICE_NAME": "com.jetbrains.intellij.136992",
"HOME": "/Users/FlySheep"
},
"applicationConfig: [classpath:/application.properties]": {
"management.security.enabled": "false"
}
}
基本上,任何能給Spring Boot 應(yīng)用程序提供屬性的屬性源都會(huì)列在 /env 的結(jié)果里根时,同時(shí)會(huì)顯示具體的屬性瘦赫。
為了避免敏感信息暴露到 /env 里,所有名為password蛤迎、secret确虱、key(或者名字中最后一段是這些)的屬性在 /env 里都會(huì)加上“*”。舉個(gè)例子忘苛,如果有一個(gè)屬性名字是database.password蝉娜,那么它在/env中的顯示效果是這樣的:
"database.password":"******"
/env 接口還能用來獲取單個(gè)屬性的值唱较,只需要在請(qǐng)求時(shí)在 /env 后加上屬性名即可扎唾。
1.4 查看屬性的使用方法
/configprops 接口會(huì)生成一個(gè)報(bào)告,說明屬性如何進(jìn)行設(shè)置(注入或其他方式)南缓。
{
"endpoints-org.springframework.boot.actuate.endpoint.EndpointProperties": {
"prefix": "endpoints",
"properties": {
"enabled": true,
"sensitive": null
}
},
"management.info-org.springframework.boot.actuate.autoconfigure.InfoContributorProperties": {
"prefix": "management.info",
"properties": {
"git": {
"mode": "SIMPLE"
}
}
},
"metricsEndpoint": {
"prefix": "endpoints.metrics",
"properties": {
"id": "metrics",
"sensitive": true,
"enabled": true
}
},
"spring.jackson-org.springframework.boot.autoconfigure.jackson.JacksonProperties": {
"prefix": "spring.jackson",
"properties": {
"propertyNamingStrategy": null,
"defaultPropertyInclusion": null,
"dateFormat": null,
"timeZone": null,
"locale": null,
"jodaDateTimeFormat": null
}
},
"heapdumpMvcEndpoint": {
"prefix": "endpoints.heapdump",
"properties": {
"path": "/heapdump",
"sensitive": true,
"enabled": true
}
},
"endpoints.cors-org.springframework.boot.actuate.autoconfigure.EndpointCorsProperties": {
"prefix": "endpoints.cors",
"properties": {
"allowedOrigins": [],
"maxAge": 1800,
"exposedHeaders": [],
"allowedHeaders": [],
"allowedMethods": [],
"allowCredentials": null
}
},
"environmentMvcEndpoint": {
"prefix": "endpoints.env",
"properties": {
"path": "/env"
}
},
"environmentEndpoint": {
"prefix": "endpoints.env",
"properties": {
"id": "env",
"sensitive": true,
"enabled": true
}
},
"spring.http.multipart-org.springframework.boot.autoconfigure.web.MultipartProperties": {
"prefix": "spring.http.multipart",
"properties": {
"maxRequestSize": "10MB",
"fileSizeThreshold": "0",
"location": null,
"maxFileSize": "1MB",
"enabled": true,
"resolveLazily": false
}
},
"spring.info-org.springframework.boot.autoconfigure.info.ProjectInfoProperties": {
"prefix": "spring.info",
"properties": {
"build": {
"location": {}
},
"git": {
"location": {}
}
}
},
"auditEventsEndpoint": {
"prefix": "endpoints.auditevents",
"properties": {
"enabled": true
}
},
"traceEndpoint": {
"prefix": "endpoints.trace",
"properties": {
"id": "trace",
"sensitive": true,
"enabled": true
}
},
"metricsMvcEndpoint": {
"prefix": "endpoints.metrics",
"properties": {
"path": "/metrics"
}
},
"infoEndpoint": {
"prefix": "endpoints.info",
"properties": {
"id": "info",
"sensitive": false,
"enabled": true
}
},
"auditEventMvcEndpoint": {
"prefix": "endpoints.auditevents",
"properties": {
"path": "/auditevents",
"sensitive": true,
"enabled": true
}
},
"management.trace-org.springframework.boot.actuate.trace.TraceProperties": {
"prefix": "management.trace",
"properties": {
"include": [
"COOKIES",
"REQUEST_HEADERS",
"RESPONSE_HEADERS",
"ERRORS"
]
}
},
"spring.resources-org.springframework.boot.autoconfigure.web.ResourceProperties": {
"prefix": "spring.resources",
"properties": {
"cachePeriod": null,
"addMappings": true,
"chain": {
"cache": true,
"htmlApplicationCache": false,
"gzipped": false,
"strategy": {
"fixed": {
"enabled": false,
"paths": [
"/**"
],
"version": null
},
"content": {
"enabled": false,
"paths": [
"/**"
]
}
}
},
"staticLocations": [
"/",
"classpath:/META-INF/resources/",
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/"
]
}
},
"management.health.status-org.springframework.boot.actuate.autoconfigure.HealthIndicatorProperties": {
"prefix": "management.health.status",
"properties": {
"order": null
}
},
"healthMvcEndpoint": {
"prefix": "endpoints.health",
"properties": {
"path": "/health"
}
},
"serverProperties": {
"prefix": "server",
"properties": {
"address": null,
"maxHttpPostSize": 0,
"undertow": {
"maxHttpPostSize": 0,
"bufferSize": null,
"buffersPerRegion": null,
"ioThreads": null,
"workerThreads": null,
"directBuffers": null,
"accesslog": {
"enabled": null,
"pattern": "common",
"prefix": "access_log.",
"suffix": "log",
"dir": "/Users/FlySheep/FlySheep/Code/ServerCode/SpringBoot_Demo/demo/logs",
"rotate": true
}
},
"tomcat": {
"accesslog": {
"enabled": false,
"pattern": "common",
"directory": "logs",
"prefix": "access_log",
"suffix": ".log",
"rotate": true,
"renameOnRotate": false,
"requestAttributesEnabled": false,
"buffered": true
},
"internalProxies": "10\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}|192\\.168\\.\\d{1,3}\\.\\d{1,3}|169\\.254\\.\\d{1,3}\\.\\d{1,3}|127\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}|172\\.1[6-9]{1}\\.\\d{1,3}\\.\\d{1,3}|172\\.2[0-9]{1}\\.\\d{1,3}\\.\\d{1,3}|172\\.3[0-1]{1}\\.\\d{1,3}\\.\\d{1,3}",
"protocolHeader": null,
"protocolHeaderHttpsValue": "https",
"portHeader": "X-Forwarded-Port",
"remoteIpHeader": null,
"basedir": null,
"backgroundProcessorDelay": 30,
"maxThreads": 0,
"minSpareThreads": 0,
"maxHttpPostSize": 0,
"redirectContextRoot": null,
"uriEncoding": null,
"maxConnections": 0,
"acceptCount": 0,
"additionalTldSkipPatterns": []
},
"displayName": "application",
"session": {
"timeout": null,
"trackingModes": null,
"persistent": false,
"storeDir": null,
"cookie": {
"name": null,
"domain": null,
"path": null,
"comment": null,
"httpOnly": null,
"secure": null,
"maxAge": null
}
},
"contextPath": null,
"error": {
"path": "/error",
"includeStacktrace": "NEVER"
},
"ssl": null,
"serverHeader": null,
"useForwardHeaders": null,
"port": null,
"maxHttpHeaderSize": 0,
"servletPath": "/",
"jspServlet": null,
"jetty": {
"maxHttpPostSize": 0,
"acceptors": null,
"selectors": null
},
"connectionTimeout": null
}
},
"spring.metrics.export-org.springframework.boot.actuate.metrics.export.MetricExportProperties": {
"prefix": "spring.metrics.export",
"properties": {
"excludes": null,
"statsd": {
"host": null,
"port": 8125,
"prefix": null
},
"includes": null,
"enabled": true,
"redis": {
"prefix": "spring.metrics.application.7a5b013db041f4a1b6c45b6456487d05",
"key": "******"
},
"aggregate": {
"prefix": "application.7a5b013db041f4a1b6c45b6456487d05",
"keyPattern": "k.d"
}
}
},
"configurationPropertiesReportEndpoint": {
"prefix": "endpoints.configprops",
"properties": {
"id": "configprops",
"sensitive": true,
"enabled": true
}
},
"healthEndpoint": {
"prefix": "endpoints.health",
"properties": {
"timeToLive": 1000,
"id": "health",
"sensitive": false,
"enabled": true
}
},
"loggersMvcEndpoint": {
"prefix": "endpoints.loggers",
"properties": {
"path": "/loggers"
}
},
"loggersEndpoint": {
"prefix": "endpoints.loggers",
"properties": {
"id": "loggers",
"sensitive": true,
"enabled": true
}
},
"endpoints.metrics.filter-org.springframework.boot.actuate.autoconfigure.MetricFilterProperties": {
"prefix": "endpoints.metrics.filter",
"properties": {
"counterSubmissions": [
"MERGED"
],
"gaugeSubmissions": [
"MERGED"
]
}
},
"dumpEndpoint": {
"prefix": "endpoints.dump",
"properties": {
"id": "dump",
"sensitive": true,
"enabled": true
}
},
"autoConfigurationReportEndpoint": {
"prefix": "endpoints.autoconfig",
"properties": {
"id": "autoconfig",
"sensitive": true,
"enabled": true
}
},
"endpoints.jmx-org.springframework.boot.actuate.autoconfigure.EndpointMBeanExportProperties": {
"prefix": "endpoints.jmx",
"properties": {
"uniqueNames": false,
"enabled": true,
"domain": ""
}
},
"spring.http.encoding-org.springframework.boot.autoconfigure.web.HttpEncodingProperties": {
"prefix": "spring.http.encoding",
"properties": {
"charset": "UTF-8",
"force": false,
"mapping": null,
"forceRequest": false,
"forceResponse": false
}
},
"shutdownEndpoint": {
"prefix": "endpoints.shutdown",
"properties": {
"id": "shutdown",
"sensitive": true,
"enabled": false
}
},
"beansEndpoint": {
"prefix": "endpoints.beans",
"properties": {
"id": "beans",
"sensitive": true,
"enabled": true
}
},
"managementServerProperties": {
"prefix": "management",
"properties": {
"security": {
"enabled": false,
"roles": [
"ACTUATOR"
],
"sessions": "STATELESS"
},
"address": null,
"port": null,
"addApplicationContextHeader": true,
"contextPath": "",
"ssl": null
}
},
"requestMappingEndpoint": {
"prefix": "endpoints.mappings",
"properties": {
"id": "mappings",
"sensitive": true,
"enabled": true
}
},
"endpoints.health-org.springframework.boot.actuate.autoconfigure.HealthMvcEndpointProperties": {
"prefix": "endpoints.health",
"properties": {
"mapping": {}
}
},
"spring.mvc-org.springframework.boot.autoconfigure.web.WebMvcProperties": {
"prefix": "spring.mvc",
"properties": {
"dateFormat": null,
"servlet": {
"loadOnStartup": -1
},
"staticPathPattern": "/**",
"dispatchOptionsRequest": true,
"dispatchTraceRequest": false,
"locale": null,
"ignoreDefaultModelOnRedirect": true,
"logResolvedException": false,
"async": {
"requestTimeout": null
},
"messageCodesResolverFormat": null,
"mediaTypes": {},
"view": {
"prefix": null,
"suffix": null
},
"localeResolver": "ACCEPT_HEADER",
"throwExceptionIfNoHandlerFound": false
}
},
"diskSpaceHealthIndicatorProperties": {
"prefix": "management.health.diskspace",
"properties": {
"path": "/Users/FlySheep/FlySheep/Code/ServerCode/SpringBoot_Demo/demo/.",
"threshold": 10485760
}
}
}
1.5 生成接口道控制器的映射
在應(yīng)用程序相對(duì)較小的時(shí)候胸遇,很容易搞清楚控制器都映射到了哪些接口上。如果Web界面的控制器和請(qǐng)求處理方法數(shù)量多汉形,那最好能有一個(gè)列表纸镊,羅列出應(yīng)用程序發(fā)布的全部接口倍阐。/mappings 接口就提供了這么一個(gè)列表。
{
"/webjars/**": {
"bean": "resourceHandlerMapping"
},
"/**": {
"bean": "resourceHandlerMapping"
},
"/**/favicon.ico": {
"bean": "faviconHandlerMapping"
},
"{[/error]}": {
"bean": "requestMappingHandlerMapping",
"method": "public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)"
},
"{[/error],produces=[text/html]}": {
"bean": "requestMappingHandlerMapping",
"method": "public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)"
},
"{[/env/{name:.*}],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}": {
"bean": "endpointHandlerMapping",
"method": "public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EnvironmentMvcEndpoint.value(java.lang.String)"
},
"{[/env || /env.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}": {
"bean": "endpointHandlerMapping",
"method": "public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()"
},
"{[/beans || /beans.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}": {
"bean": "endpointHandlerMapping",
"method": "public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()"
},
"{[/auditevents || /auditevents.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}": {
"bean": "endpointHandlerMapping",
"method": "public org.springframework.http.ResponseEntity<?> org.springframework.boot.actuate.endpoint.mvc.AuditEventsMvcEndpoint.findByPrincipalAndAfterAndType(java.lang.String,java.util.Date,java.lang.String)"
},
"{[/autoconfig || /autoconfig.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}": {
"bean": "endpointHandlerMapping",
"method": "public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()"
},
"{[/loggers/{name:.*}],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}": {
"bean": "endpointHandlerMapping",
"method": "public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.LoggersMvcEndpoint.get(java.lang.String)"
},
"{[/loggers/{name:.*}],methods=[POST],consumes=[application/vnd.spring-boot.actuator.v1+json || application/json],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}": {
"bean": "endpointHandlerMapping",
"method": "public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.LoggersMvcEndpoint.set(java.lang.String,java.util.Map<java.lang.String, java.lang.String>)"
},
"{[/loggers || /loggers.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}": {
"bean": "endpointHandlerMapping",
"method": "public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()"
},
"{[/heapdump || /heapdump.json],methods=[GET],produces=[application/octet-stream]}": {
"bean": "endpointHandlerMapping",
"method": "public void org.springframework.boot.actuate.endpoint.mvc.HeapdumpMvcEndpoint.invoke(boolean,javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) throws java.io.IOException,javax.servlet.ServletException"
},
"{[/mappings || /mappings.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}": {
"bean": "endpointHandlerMapping",
"method": "public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()"
},
"{[/trace || /trace.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}": {
"bean": "endpointHandlerMapping",
"method": "public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()"
},
"{[/configprops || /configprops.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}": {
"bean": "endpointHandlerMapping",
"method": "public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()"
},
"{[/dump || /dump.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}": {
"bean": "endpointHandlerMapping",
"method": "public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()"
},
"{[/metrics/{name:.*}],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}": {
"bean": "endpointHandlerMapping",
"method": "public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.MetricsMvcEndpoint.value(java.lang.String)"
},
"{[/metrics || /metrics.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}": {
"bean": "endpointHandlerMapping",
"method": "public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()"
},
"{[/health || /health.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}": {
"bean": "endpointHandlerMapping",
"method": "public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.HealthMvcEndpoint.invoke(javax.servlet.http.HttpServletRequest,java.security.Principal)"
},
"{[/info || /info.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}": {
"bean": "endpointHandlerMapping",
"method": "public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()"
}
}
這里我們可以看到不少接口的映射逗威。每個(gè)映射的鍵都是一個(gè)字符串峰搪,其內(nèi)容就是 Spring MVC 的 @RequestMapping 注解上設(shè)置的屬性。實(shí)際上凯旭,這個(gè)字符串能讓你清晰地了解控制器是如何映射的概耻,哪怕不看源代碼。每個(gè)映射的值都有兩個(gè)屬性:bean 和 method罐呼。bean 屬性標(biāo)識(shí)了 Spring Bean 的名字鞠柄,映射源自這個(gè)Bean。method屬性是映射對(duì)應(yīng)方法的全限定方法簽名嫉柴。
2. 運(yùn)行時(shí)指標(biāo)
對(duì)運(yùn)行時(shí)指標(biāo)情況做一個(gè)快照厌杜,這對(duì)評(píng)估應(yīng)用程序的健康情況很有幫助。Actuator 提供了一系列接口计螺,讓你能在運(yùn)行時(shí)快速檢查應(yīng)用程序夯尽。
2.1 查看應(yīng)用程序的指標(biāo)值
關(guān)于運(yùn)行中的應(yīng)用程序,有很多有趣而且有用的信息登馒。舉個(gè)例子呐萌,了解應(yīng)用程序的內(nèi)存情況 (可用或空閑)有助于決定給JVM分配多少內(nèi)存。對(duì)Web應(yīng)用程序而言谊娇,不用查看Web服務(wù)器日志肺孤,如果請(qǐng)求失敗或者是耗時(shí)太長,就可以大概知道內(nèi)存的情況了济欢。運(yùn)行中的應(yīng)用程序有諸多計(jì)數(shù)器和度量器赠堵,/metrics 接口提供了這些東西的快照。
{
"mem": 365004,
"mem.free": 153938,
"processors": 8,
"instance.uptime": 852279,
"uptime": 855002,
"systemload.average": 3.509765625,
"heap.committed": 316928,
"heap.init": 262144,
"heap.used": 162989,
"heap": 3728384,
"nonheap.committed": 50816,
"nonheap.init": 2496,
"nonheap.used": 48077,
"nonheap": 0,
"threads.peak": 18,
"threads.daemon": 16,
"threads.totalStarted": 23,
"threads": 18,
"classes": 5974,
"classes.loaded": 5974,
"classes.unloaded": 0,
"gc.ps_scavenge.count": 4,
"gc.ps_scavenge.time": 35,
"gc.ps_marksweep.count": 1,
"gc.ps_marksweep.time": 27,
"httpsessions.max": -1,
"httpsessions.active": 0,
"gauge.response.mappings": 4,
"gauge.response.configprops": 155,
"counter.status.200.mappings": 1,
"counter.status.200.configprops": 1
}
對(duì) /metrics 接口提供的信息進(jìn)行簡(jiǎn)單分類如下表:
分類 | 前綴 | 報(bào)告內(nèi)容 |
---|---|---|
垃圾收集器 | gc.* | 已經(jīng)發(fā)生過的垃圾收集次數(shù)法褥,以及垃圾收集所耗費(fèi)的時(shí)間茫叭,適用于標(biāo)記-清理垃圾收集器和并行垃圾收集器(數(shù)據(jù)源自java.lang.management. GarbageCollectorMXBean) |
內(nèi)存 | mem.* | 分配給應(yīng)用程序的內(nèi)存數(shù)量和空閑的內(nèi)存數(shù)量(數(shù)據(jù)源自java.lang. Runtime) |
堆 | heap.* | 當(dāng)前內(nèi)存用量(數(shù)據(jù)源自java.lang.management.MemoryUsage) |
類加載器 | classes.* | JVM類加載器加載與卸載的類的數(shù)量(數(shù)據(jù)源自java.lang. management.ClassLoadingMXBean) |
系統(tǒng) | processors、instance.uptime半等、uptime揍愁、systemload.average | 系統(tǒng)信息,例如處理器數(shù)量(數(shù)據(jù)源自java.lang.Runtime)杀饵、運(yùn)行時(shí)間(數(shù)據(jù)源自java.lang.management.RuntimeMXBean)莽囤、平均負(fù)載(數(shù)據(jù)源自java.lang.management.OperatingSystemMXBean) |
線程池 | thread.* | 線程、守護(hù)線程的數(shù)量切距,以及JVM啟動(dòng)后的線程數(shù)量峰值(數(shù)據(jù)源自 java.lang .management.ThreadMXBean) |
數(shù)據(jù)源 | datasource.* | 數(shù)據(jù)源連接的數(shù)量(源自數(shù)據(jù)源的元數(shù)據(jù)朽缎,僅當(dāng)Spring應(yīng)用程序上下文里存在 DataSource Bean 的時(shí)候才會(huì)有這個(gè)信息) |
Tomcat 會(huì)話 | httpsessions.* | Tomcat的活躍會(huì)話數(shù)和最大會(huì)話數(shù)(數(shù)據(jù)源自嵌入式Tomcat的Bean,僅在使用嵌入式Tomcat服務(wù)器運(yùn)行應(yīng)用程序時(shí)才有這個(gè)信息) |
HTTP | counter.status.、gauge.response. | 多種應(yīng)用程序服務(wù)HTTP請(qǐng)求的度量值與計(jì)數(shù)器 |
請(qǐng)注意话肖,這里的一些度量值北秽,比如數(shù)據(jù)源和Tomcat會(huì)話,僅在應(yīng)用程序中運(yùn)行特定組件時(shí)才有數(shù)據(jù)最筒。你還可以注冊(cè)自己的度量信息贺氓。
HTTP的計(jì)數(shù)器和度量值需要做一點(diǎn)說明。counter.status 后的值是HTTP狀態(tài)碼床蜘,隨后是所請(qǐng)求的路徑掠归。舉個(gè)例子,counter.status.200.metrics 表明/metrics端點(diǎn)返回 200(OK) 狀態(tài)碼的次數(shù)悄泥。
HTTP的度量信息在結(jié)構(gòu)上也差不多虏冻,卻在報(bào)告另一類信息。它們?nèi)恳詆auge.response 開頭,弹囚,表明這是HTTP響應(yīng)的度量信息厨相。前綴后是對(duì)應(yīng)的路徑。度量值是以毫秒為單位的時(shí)間鸥鹉,反映了最近處理該路徑請(qǐng)求的耗時(shí)蛮穿。
這里還有幾個(gè)特殊的值需要注意。root路徑指向的是根路徑或/毁渗。star-star代表了那些Spring 認(rèn)為是靜態(tài)資源的路徑践磅,包括圖片、JavaScript和樣式表灸异,其中還包含了那些找不到的資源府适。這就是為什么你經(jīng)常會(huì)看到 counter.status.404.star-star,這是返回了HTTP 404 (NOT FOUND) 狀態(tài)的請(qǐng)求數(shù)肺樟¢艽海
/metrics 接口會(huì)返回所有的可用度量值,但你也可能只對(duì)某個(gè)值感興趣么伯。要獲取單個(gè)值疟暖,請(qǐng)求時(shí)可以在URL后加上對(duì)應(yīng)的鍵名。例如田柔,要查看空閑內(nèi)存大小,可以向/metrics/mem.free發(fā)一 個(gè)GET請(qǐng)求俐巴。
2.2 追蹤 Web 請(qǐng)求
/trace 接口能報(bào)告所有Web請(qǐng)求的詳細(xì)信息,包括請(qǐng)求方法硬爆、路徑欣舵、時(shí)間戳以及請(qǐng)求和響應(yīng)的頭信息。
[
{
"timestamp": 1491279254447,
"info": {
"method": "GET",
"path": "/metrics",
"headers": {
"request": {
"host": "localhost:8080",
"connection": "keep-alive",
"upgrade-insecure-requests": "1",
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36",
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"accept-encoding": "gzip, deflate, sdch, br",
"accept-language": "zh-CN,zh;q=0.8,en;q=0.6",
"cookie": "JSESSIONID=24CCB83195B2A35F5193C97D2CD51D69"
},
"response": {
"X-Application-Context": "application",
"Content-Type": "application/vnd.spring-boot.actuator.v1+json;charset=UTF-8",
"Transfer-Encoding": "chunked",
"Date": "Tue, 04 Apr 2017 04:14:14 GMT",
"status": "200"
}
}
}
}
]
2.3 導(dǎo)出線程活動(dòng)
在確認(rèn)應(yīng)用程序運(yùn)行情況時(shí)摆屯,除了跟蹤請(qǐng)求邻遏,了解線程活動(dòng)也會(huì)很有幫助。/dump 接口會(huì)生成當(dāng)前線程活動(dòng)的快照虐骑。
2.4 監(jiān)控應(yīng)用程序健康狀況
如果你想知道自己的應(yīng)用程序是否在運(yùn)行,可以直接訪問/health 接口准验。在最簡(jiǎn)單的情況下,該端點(diǎn)會(huì)顯示一個(gè)簡(jiǎn)單的JSON廷没,內(nèi)容如下:
{
"status": "UP",
"diskSpace": {
"status": "UP",
"total": 48978722816,
"free": 8812167168,
"threshold": 10485760
}
}
二糊饱、定制 Actuator
雖然Actuator提供了很多運(yùn)行中Spring Boot應(yīng)用程序的內(nèi)部工作細(xì)節(jié),但難免和你的需求有所偏差颠黎。也許你并不需要它提供的所有功能另锋,想要關(guān)閉一些也說不定∠凉椋或者夭坪,你需要對(duì)Actuator 稍作擴(kuò)展,增加一些自定義的度量信息过椎,以滿足你對(duì)應(yīng)用程序的需求室梅。
1. 修改接口 ID
每個(gè)Actuator 接口都有一個(gè)ID用來決定接口的路徑,比方說疚宇,/beans接口的默認(rèn)ID就是beans亡鼠。比如要修改 /beans 為 /instances,則設(shè)置如下:
endpoints.beans.id = instances
2. 啟用和禁用接口
雖然Actuator的接口都很有用敷待,但你不一定需要全部這些接口间涵。默認(rèn)情況下,所有接口(除 了/shutdown)都啟用榜揖。比如要禁用 /metrics 接口勾哩,則可以設(shè)置如下:
endpoints.metrics.enabled = false
如果你只想打開一兩個(gè)接口,那就先禁用全部接口举哟,然后啟用那幾個(gè)你要的钳幅,這樣更方便。
endpoints.enabled = false
endpoints.metrics.enabled = true
3. 添加自定義度量信息
Actuator 自動(dòng)配置有兩個(gè)實(shí)例 CounterService 和 GaugeService 可以用來計(jì)數(shù)使用炎滞,我們所要做的就是把它們的實(shí)例注入所需的 bean 然后調(diào)用相應(yīng)的方法敢艰。除此之外,我們還可以實(shí)現(xiàn) PublicMetrics 接口册赛,提供自己需要的度量信息钠导。
4. 創(chuàng)建自定義跟蹤倉庫
默認(rèn)情況下,/trace 接口報(bào)告的跟蹤信息都存儲(chǔ)在內(nèi)存?zhèn)}庫里森瘪,100個(gè)條目封頂牡属。一旦倉庫滿了,就開始移除老的條目扼睬,給新的條目騰出空間逮栅。在開發(fā)階段這沒什么問題悴势,但在生產(chǎn)環(huán)境中,大流量會(huì)造成跟蹤信息還沒來得及看就被丟棄措伐。我們可以將那些跟蹤條目存儲(chǔ)在其他地方——既不消耗內(nèi)存特纤,又能長久保存的地方。只需實(shí)現(xiàn)Spring Boot的TraceRepository接口即可侥加。
5. 插入自定義的健康指示器
實(shí)現(xiàn) HealthIndicator 接口則可以實(shí)現(xiàn)自定義的健康指示器捧存。
6. 保護(hù) Actuator 接口
很多Actuator端點(diǎn)發(fā)布的信息都可能涉及敏感數(shù)據(jù),還有一些端點(diǎn)担败,(比如/shutdown)非常危險(xiǎn)昔穴,可以用來關(guān)閉應(yīng)用程序。因此提前,保護(hù)這些端點(diǎn)尤為重要,能訪問它們的只能是那些經(jīng)過授權(quán)的客戶端吗货。