Anwendungsüberwachung mit Spring Boot Actuator
View more Tutorials:
Spring Boot Actuator ist ein Sub-Projekt im Projekt Spring Boot. Es wird gebaut um die Information über die Applikation zu sammeln und kotrollieren. Sie können es in Ihrer Applikation einbetten und seine Funktionen benutzen. Um die Applikation zu kontrollieren, sollen Sie in endpoint (Điểm cuối), die in Spring Boot Actuator verfügbar gebaut ist, zugreifen und gleichzeitig können Sie auch Ihre eigenen endpoint wenn gewünscht bauen.
In diesem Artikel leite ich bei der Benutzung von Spring Boot Actuator in der Applikation von Spring Boot mit der Version von 2.0.0.M7 oder neuer an.
Beachten Sie: Spring Boot Actuator hat viele Änderungen in der Version von 2.x im Vergleich von 1.x. Deshalb stellen Sie sicher, dass Sie mit Spring Boot von der Version von 2.0.0.M7 oder neuer arbeiten
Spring Boot Actuator ist ein Sub-Projekt (ein gefertiges Produkt) in dem Ökosystem Spring, und Sie können es in Ihrem verfügbaren Projekt Spring Boot einbetten. Alles, was Sie machen brauchen is die Deklarierung der Bibliothek Spring Boot Actuator und einige Konfigurationsinformaton in der File application.properties. In den Artikel werde ich ein Projekt Spring Boot erstellen und darin Spring Boot Actuator einbetten . Unser Zweck ist die Forschung der von Spring Boot Actuator gebieteten Funktionen.
OK!, Auf Eclipse erstellen Sie ein Projekt Spring Boot.

Hier werden wir die Version von 2.0.0.M7 oder neuer wählen

pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.o7planning</groupId> <artifactId>SpringBootActuator</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>SpringBootActuator</name> <description>Spring Boot +Actuator</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
Add the configuration to the application.properties file:
application.properties
server.port=8080 management.server.port=8090 management.endpoints.web.exposure.include=* management.endpoint.shutdown.enabled=true
- server.port=8080
Ihre Applikation wird auf dem Port 8080 (port 8080) laufen. Das ist der Default-Port und Sie können ändern wenn gewünscht
- management.server.port=8090
Die endpoint , die mit der Kontroll von Spring Boot Actuator betreffen, werden in anderen Port als den Port 8080 zugegriffen. Das Zweck ist die Vermeidung der Verwechslung und die Erhöhung der Sicherheit. Allerdings ist es nicht zwanghaft.
- management.endpoints.web.exposure.include=*
Standardmäßig sind nicht alle Endpoint vom Spring Boot Actuator aktiviert. Benutzen Sie * um diese allen Endpoint zu aktivieren
- management.endpoint.shutdown.enabled=true
shutdown ist ein besonderes Endpoint vom Spring Boot Actuator. Es erlaubt Sie, ohne die Benutzung von "Kill process", "end task" vom Betriebssystem sicher die Applikation zu schließen.
MainController.java
package org.o7planning.sbactuator.controller; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class MainController { @ResponseBody @RequestMapping(path = "/") public String home(HttpServletRequest request) { String contextPath = request.getContextPath(); String host = request.getServerName(); // Spring Boot >= 2.0.0.M7 String endpointBasePath = "/actuator"; StringBuilder sb = new StringBuilder(); sb.append("<h2>Sprig Boot Actuator</h2>"); sb.append("<ul>"); // http://localhost:8090/actuator String url = "http://" + host + ":8090" + contextPath + endpointBasePath; sb.append("<li><a href='" + url + "'>" + url + "</a></li>"); sb.append("</ul>"); return sb.toString(); } }
Ihre Applikation laufen und in den folgenden Pfad zugreifen

Achtung: Die Daten, die die endpoint vom Spring Boot Actuator bieten, hat die JSON Format, Deshalb sollen Sie die Daten reformatieren (reformat) um einfach zu schauen. Zum Beispiel die folgenden Website bieten die Online-Tools um JSON zu formatieren:
/actuator
/actuator ist ein endpoint , Es bietet die Liste der anderen endpoint vom Spring Boot Actuator , die Sie zugreifen können
/actuator
{ "_links": { "self": { "href": "http://localhost:8090/actuator", "templated": false }, "auditevents": { "href": "http://localhost:8090/actuator/auditevents", "templated": false }, "beans": { "href": "http://localhost:8090/actuator/beans", "templated": false }, "health": { "href": "http://localhost:8090/actuator/health", "templated": false }, "conditions": { "href": "http://localhost:8090/actuator/conditions", "templated": false }, "shutdown": { "href": "http://localhost:8090/actuator/shutdown", "templated": false }, "configprops": { "href": "http://localhost:8090/actuator/configprops", "templated": false }, "env": { "href": "http://localhost:8090/actuator/env", "templated": false }, "env-toMatch": { "href": "http://localhost:8090/actuator/env/{toMatch}", "templated": true }, "info": { "href": "http://localhost:8090/actuator/info", "templated": false }, "loggers": { "href": "http://localhost:8090/actuator/loggers", "templated": false }, "loggers-name": { "href": "http://localhost:8090/actuator/loggers/{name}", "templated": true }, "heapdump": { "href": "http://localhost:8090/actuator/heapdump", "templated": false }, "threaddump": { "href": "http://localhost:8090/actuator/threaddump", "templated": false }, "metrics-requiredMetricName": { "href": "http://localhost:8090/actuator/metrics/{requiredMetricName}", "templated": true }, "metrics": { "href": "http://localhost:8090/actuator/metrics", "templated": false }, "scheduledtasks": { "href": "http://localhost:8090/actuator/scheduledtasks", "templated": false }, "trace": { "href": "http://localhost:8090/actuator/trace", "templated": false }, "mappings": { "href": "http://localhost:8090/actuator/mappings", "templated": false } } }
/actuator/health
/actuator/health bietet Ihnen die Gesundheit der Applikation. Die Modus von (UP) or (DOWN),und die sonstige Informationen von der Festplatte wie der Größe, der verwendeten Kapazität und der nichtverwendenten Kapazität
/actuator/health
{ "status": "UP", "details": { "diskSpace": { "status": "UP", "details": { "total": 75812040704, "free": 8067600384, "threshold": 10485760 } } } }
actuator/metrics
Dieses Endpoint bietet Sie die Liste von metric (die Messungsstandard)
actuator/metrics
{ "names": [ "jvm.memory.committed", "jvm.buffer.memory.used", "jvm.buffer.count", "logback.events", "process.uptime", "jvm.memory.max", "jvm.memory.used", "jvm.buffer.total.capacity", "system.cpu.count", "process.start.time" ] }
Und Sie können die Information eines bestimmten metric sehen.
actuator/metrics/{requiredMetricName}
/actuator/metrics/jvm.memory.used
{ "name": "jvm.memory.used", "measurements": [ { "statistic": "Value", "value": 109937744 } ], "availableTags": [ { "tag": "area", "values": [ "heap", "nonheap", "nonheap", "heap", "nonheap", "nonheap", "heap" ] }, { "tag": "id", "values": [ "G1 Old Gen", "Compressed Class Space", "CodeHeap 'non-nmethods'", "G1 Survivor Space", "Metaspace", "CodeHeap 'non-profiled nmethods'", "G1 Eden Space" ] } ] }
/actuator/beans
/actuator/beans bietet Ihnen die Liste von den in Ihrer Applikation managierten Spring BEAN an
/actuator/beans
{ "contextId": "application:8080", "beans": { "endpointCachingOperationInvokerAdvisor": { "aliases": [], "scope": "singleton", "type": "org.springframework.boot.actuate.endpoint.cache.CachingOperationInvokerAdvisor", "resource": "class path resource [org/springframework/boot/actuate/autoconfigure/endpoint/EndpointAutoConfiguration.class]", "dependencies": [ "environment" ] }, "defaultServletHandlerMapping": { "aliases": [], "scope": "singleton", "type": "org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport$EmptyHandlerMapping", "resource": "class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]", "dependencies": [] } .... }, "parent": null }
/actuator/env
/actuator/env bietet Sie die Umweltsinformation an, die Ihre Applikation läuft, z.B: der Name des Betriebssystem, classpath, die Version Java,...
/actuator/env
{ "activeProfiles": [], "propertySources": [ { "name": "server.ports", "properties": { "local.management.port": { "value": 8090 }, "local.server.port": { "value": 8080 } } }, { "name": "servletContextInitParams", "properties": {} }, { "name": "systemProperties", "properties": { "sun.desktop": { "value": "windows" }, "awt.toolkit": { "value": "sun.awt.windows.WToolkit" }, "java.specification.version": { "value": "9" }, "file.encoding.pkg": { "value": "sun.io" }, "sun.cpu.isalist": { "value": "amd64" }, "sun.jnu.encoding": { "value": "Cp1252" }, "java.class.path": { "value": "C:\\Users\\tran\\.m2\\repository\\org\\springframework\\spring-jcl\\5.0.2.RELEASE\\spring-jcl-5.0.2.RELEASE.jar;...." }, "com.sun.management.jmxremote.authenticate": { "value": "false" }, "java.vm.vendor": { "value": "Oracle Corporation" }, "sun.arch.data.model": { "value": "64" }, "user.variant": { "value": "" }, "java.vendor.url": { "value": "http://java.oracle.com/" }, "catalina.useNaming": { "value": "false" }, "user.timezone": { "value": "Asia/Bangkok" }, "os.name": { "value": "Windows 8.1" }, "java.vm.specification.version": { "value": "9" }, "sun.java.launcher": { "value": "SUN_STANDARD" }, "user.country": { "value": "US" }, "sun.boot.library.path": { "value": "C:\\DevPrograms\\Java\\jre-9.0.1\\bin" }, "com.sun.management.jmxremote.ssl": { "value": "false" }, "spring.application.admin.enabled": { "value": "true" }, "sun.java.command": { "value": "org.o7planning.sbactuator.SpringBootActuatorApplication" }, "com.sun.management.jmxremote": { "value": "" }, "jdk.debug": { "value": "release" }, "sun.cpu.endian": { "value": "little" }, "user.home": { "value": "C:\\Users\\tran" }, "user.language": { "value": "en" }, "java.specification.vendor": { "value": "Oracle Corporation" }, "java.home": { "value": "C:\\DevPrograms\\Java\\jre-9.0.1" }, "file.separator": { "value": "\\" }, "java.vm.compressedOopsMode": { "value": "Zero based" }, "line.separator": { "value": "\r\n" }, "java.specification.name": { "value": "Java Platform API Specification" }, "java.vm.specification.vendor": { "value": "Oracle Corporation" }, "java.awt.graphicsenv": { "value": "sun.awt.Win32GraphicsEnvironment" }, "java.awt.headless": { "value": "true" }, "user.script": { "value": "" }, "sun.management.compiler": { "value": "HotSpot 64-Bit Tiered Compilers" }, "java.runtime.version": { "value": "9.0.1+11" }, "user.name": { "value": "tran" }, "path.separator": { "value": ";" }, "os.version": { "value": "6.3" }, "java.runtime.name": { "value": "Java(TM) SE Runtime Environment" }, "file.encoding": { "value": "UTF-8" }, "spring.beaninfo.ignore": { "value": "true" }, "java.vm.name": { "value": "Java HotSpot(TM) 64-Bit Server VM" }, "java.vendor.url.bug": { "value": "http://bugreport.java.com/bugreport/" }, "java.io.tmpdir": { "value": "C:\\Users\\tran\\AppData\\Local\\Temp\\" }, "catalina.home": { "value": "C:\\Users\\tran\\AppData\\Local\\Temp\\tomcat.16949807720416048110.8080" }, "java.version": { "value": "9.0.1" }, "com.sun.management.jmxremote.port": { "value": "54408" }, "user.dir": { "value": "E:\\ECLIPSE_TUTORIAL\\JAVA_SPRING_BOOT\\SpringBootActuator" }, "os.arch": { "value": "amd64" }, "java.vm.specification.name": { "value": "Java Virtual Machine Specification" }, "PID": { "value": "5372" }, "java.awt.printerjob": { "value": "sun.awt.windows.WPrinterJob" }, "sun.os.patch.level": { "value": "" }, "catalina.base": { "value": "C:\\Users\\tran\\AppData\\Local\\Temp\\tomcat.8934969984130443549.8090" }, "java.library.path": { "value": "C:\\DevPrograms\\Java\\jre-9.0.1\\bin;...;C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\;C:\\Program Files\\EmEditor;." }, "java.vm.info": { "value": "mixed mode" }, "java.vendor": { "value": "Oracle Corporation" }, "java.vm.version": { "value": "9.0.1+11" }, "java.rmi.server.hostname": { "value": "localhost" }, "java.rmi.server.randomIDs": { "value": "true" }, "sun.io.unicode.encoding": { "value": "UnicodeLittle" }, "java.class.version": { "value": "53.0" } } }, { "name": "systemEnvironment", "properties": { "USERDOMAIN_ROAMINGPROFILE": { "value": "tran-pc", "origin": "System Environment Property \"USERDOMAIN_ROAMINGPROFILE\"" }, "LOCALAPPDATA": { "value": "C:\\Users\\tran\\AppData\\Local", "origin": "System Environment Property \"LOCALAPPDATA\"" }, "PROCESSOR_LEVEL": { "value": "6", "origin": "System Environment Property \"PROCESSOR_LEVEL\"" }, "FP_NO_HOST_CHECK": { "value": "NO", "origin": "System Environment Property \"FP_NO_HOST_CHECK\"" }, "USERDOMAIN": { "value": "tran-pc", "origin": "System Environment Property \"USERDOMAIN\"" }, "LOGONSERVER": { "value": "\\\\TRAN-PC", "origin": "System Environment Property \"LOGONSERVER\"" }, "SESSIONNAME": { "value": "Console", "origin": "System Environment Property \"SESSIONNAME\"" }, "ALLUSERSPROFILE": { "value": "C:\\ProgramData", "origin": "System Environment Property \"ALLUSERSPROFILE\"" }, "PROCESSOR_ARCHITECTURE": { "value": "AMD64", "origin": "System Environment Property \"PROCESSOR_ARCHITECTURE\"" }, "PSModulePath": { "value": "C:\\Windows\\system32\\WindowsPowerShell\\v1.0\\Modules\\", "origin": "System Environment Property \"PSModulePath\"" }, "SystemDrive": { "value": "C:", "origin": "System Environment Property \"SystemDrive\"" }, "APPDATA": { "value": "C:\\Users\\tran\\AppData\\Roaming", "origin": "System Environment Property \"APPDATA\"" }, "USERNAME": { "value": "tran", "origin": "System Environment Property \"USERNAME\"" }, "ProgramFiles(x86)": { "value": "C:\\Program Files (x86)", "origin": "System Environment Property \"ProgramFiles(x86)\"" }, "CommonProgramFiles": { "value": "C:\\Program Files\\Common Files", "origin": "System Environment Property \"CommonProgramFiles\"" }, "Path": { "value": "D:\\DEV_PROGRAMS\\Oracle12c\\product\\12.2.0\\dbhome_1\\bin;...;C:\\Program Files\\EmEditor", "origin": "System Environment Property \"Path\"" }, "PATHEXT": { "value": ".COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC", "origin": "System Environment Property \"PATHEXT\"" }, "OS": { "value": "Windows_NT", "origin": "System Environment Property \"OS\"" }, "COMPUTERNAME": { "value": "TRAN-PC", "origin": "System Environment Property \"COMPUTERNAME\"" }, "PROCESSOR_REVISION": { "value": "3c03", "origin": "System Environment Property \"PROCESSOR_REVISION\"" }, "CommonProgramW6432": { "value": "C:\\Program Files\\Common Files", "origin": "System Environment Property \"CommonProgramW6432\"" }, "ComSpec": { "value": "C:\\Windows\\system32\\cmd.exe", "origin": "System Environment Property \"ComSpec\"" }, "ProgramData": { "value": "C:\\ProgramData", "origin": "System Environment Property \"ProgramData\"" }, "ProgramW6432": { "value": "C:\\Program Files", "origin": "System Environment Property \"ProgramW6432\"" }, "HOMEPATH": { "value": "\\Users\\tran", "origin": "System Environment Property \"HOMEPATH\"" }, "SystemRoot": { "value": "C:\\Windows", "origin": "System Environment Property \"SystemRoot\"" }, "TEMP": { "value": "C:\\Users\\tran\\AppData\\Local\\Temp", "origin": "System Environment Property \"TEMP\"" }, "HOMEDRIVE": { "value": "C:", "origin": "System Environment Property \"HOMEDRIVE\"" }, "PROCESSOR_IDENTIFIER": { "value": "Intel64 Family 6 Model 60 Stepping 3, GenuineIntel", "origin": "System Environment Property \"PROCESSOR_IDENTIFIER\"" }, "USERPROFILE": { "value": "C:\\Users\\tran", "origin": "System Environment Property \"USERPROFILE\"" }, "TMP": { "value": "C:\\Users\\tran\\AppData\\Local\\Temp", "origin": "System Environment Property \"TMP\"" }, "CommonProgramFiles(x86)": { "value": "C:\\Program Files (x86)\\Common Files", "origin": "System Environment Property \"CommonProgramFiles(x86)\"" }, "ProgramFiles": { "value": "C:\\Program Files", "origin": "System Environment Property \"ProgramFiles\"" }, "PUBLIC": { "value": "C:\\Users\\Public", "origin": "System Environment Property \"PUBLIC\"" }, "NUMBER_OF_PROCESSORS": { "value": "8", "origin": "System Environment Property \"NUMBER_OF_PROCESSORS\"" }, "windir": { "value": "C:\\Windows", "origin": "System Environment Property \"windir\"" } } }, { "name": "applicationConfig: [classpath:/application.properties]", "properties": { "server.port": { "value": "8080", "origin": "class path resource [application.properties]:1:13" }, "management.server.port": { "value": "8090", "origin": "class path resource [application.properties]:2:24" }, "management.endpoints.web.expose": { "value": "*", "origin": "class path resource [application.properties]:3:33" }, "management.endpoint.shutdown.enabled": { "value": "true", "origin": "class path resource [application.properties]:4:38" } } } ] }
/actuator/info
/actuator/info bietet Ihre Anpassungsinformation (customization) an. Nach dem Default ist sie leer. Deshalb sollen Sie ein Spring BEAN erstellen um diese Information anzubieten
BuildInfoContributor.java
package org.o7planning.sbactuator.monitoring; import java.util.HashMap; import java.util.Map; import org.springframework.boot.actuate.info.Info; import org.springframework.boot.actuate.info.InfoContributor; import org.springframework.stereotype.Component; @Component public class BuildInfoContributor implements InfoContributor { @Override public void contribute(Info.Builder builder) { Map<String,String> data= new HashMap<String,String>(); data.put("build.version", "2.0.0.M7"); builder.withDetail("buildInfo", data); } }

Das ist ein endpoint damit Sie die Applikation schließen (shutdown). Sie sollen es durch die Methode POST anrufen, Wenn erfolgreich, bekommen Sie eine Nachricht "Shutting down, bye...".
ShutdownController.java
package org.o7planning.sbactuator.controller; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.client.RestTemplate; @Controller public class ShutdownController { @ResponseBody @RequestMapping(path = "/shutdown") public String callActuatorShutdown() { // Actuator Shutdown Endpoint: String url = "http://localhost:8090/actuator/shutdown"; // Http Headers HttpHeaders headers = new HttpHeaders(); headers.add("Accept", MediaType.APPLICATION_JSON_VALUE); headers.setContentType(MediaType.APPLICATION_JSON); RestTemplate restTemplate = new RestTemplate(); // Data attached to the request. HttpEntity<String> requestBody = new HttpEntity<>("", headers); // Send request with POST method. String e = restTemplate.postForObject(url, requestBody, String.class); return "Result: " + e; } }

Spring Boot Actuator ist ein fertiges Produkt, das Ihnen die endpoint zur Applikationskontroll anbietet. Allerdings möchten Sie in einigen Situation Ihre eigenen endpoint . Um diese Dinge zu machen, können Sie die folgende Unterricht lernen
- TODO Link!