- Spring Boot's Monitor
- Depend on
- Configuration
- Writing monitoring Controller
- Some of the commonly used built-in endpoint
- Defining Actuator/info Special Endpoint
- Actuator/shutdown requires a POST request to access
Spring Boot's Monitor
This feature is used to control spring boot programs and view program information
Depend on
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> <version>2.0.4.RELEASE</version></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId></dependency>
Configuration
spring.datasource.url=jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useSSL=false&allowPublicKeyRetrieval=truespring.datasource.username=rootspring.datasource.password=123spring.datasource.driver-class-name=com.mysql.jdbc.Driverserver.tomcat.uri-encoding=UTF-8# 程序运行端口server.port=8888# 监视程序运行端口management.server.port=8090# 激活所有的内置Endpointsmanagement.endpoints.web.exposure.include=*# 开启shutdown这个endpointmanagement.endpoint.shutdown.enabled=true
Writing monitoring Controller
package Com.springlearn.learn.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; @Controllerpublic class maincontroller{@ResponseBody @ Requestmapping ("/getendpoints") public String Getallpoint (HttpServletRequest request) {string path = Request.get ContextPath (); String host = Request.getservername (); String Endpointpath = "/actuator"; StringBuilder sb = new StringBuilder (); Sb.append ("
Some of the commonly used built-in endpointactuator/health 查看程序健康信息actuator/metrics 查看监视标准actuator/beans 列出程序中的Spring BEAN actuator/env 列出程序运行所有信息
Defining Actuator/info Special Endpointactuator/info可以自定义一些信息书写如下代码即可访问package com.springlearn.learn.selfactuator;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;@Componentpublic class BuildInfoActuator implements InfoContributor{ @Override public void contribute(Info.Builder builder) { Map<String,String> data= new HashMap<String,String>(); data.put("build.version", "1.0.0"); builder.withDetail("buildInfo", data); }}
Actuator/shutdown requires a POST request to accessCan be used to close the program define the following controller can package Com.springlearn.learn.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;@ Controllerpublic class Shutdowncontroller {@ResponseBody @RequestMapping (path = "/shutdown") public String Actua Torshutdown () {String URL = "Http://localhost:8090/actuator/shutdown"; Httpheaders headers = new Httpheaders (); Headers.add ("Accept", Mediatype.application_json_value); Headers.setcontenttype (Mediatype.application_json); Resttemplate resttemplate = new Resttemplate (); httpentity<string> requestbody = new Httpentity<> ("", headers); String e = resttemplate.postforobject (URL, requestbody, string.class); Return "Result:" + E; }}
Springboot into God--monitor