springboot成神之——监视器
2021-07-04 05:04
标签:ram tty serve with bind hashmap man tar enable 此功能用来控制spring boot程序和查看程序信息 springboot成神之——监视器 标签:ram tty serve with bind hashmap man tar enable 原文地址:https://www.cnblogs.com/ye-hcj/p/9614109.html
Spring Boot 的监视器
依赖
配置
spring.datasource.url=jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useSSL=false&allowPublicKeyRetrieval=true
spring.datasource.username=root
spring.datasource.password=123
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
server.tomcat.uri-encoding=UTF-8
# 程序运行端口
server.port=8888
# 监视程序运行端口
management.server.port=8090
# 激活所有的内置Endpoints
management.endpoints.web.exposure.include=*
# 开启shutdown这个endpoint
management.endpoint.shutdown.enabled=true
书写监视控制器
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;
@Controller
public class MainController{
@ResponseBody
@RequestMapping("/GetEndPoints")
public String GetAllPoint(HttpServletRequest request){
String path = request.getContextPath();
String host = request.getServerName();
String endPointPath = "/actuator";
StringBuilder sb = new StringBuilder();
sb.append("
Sprig Boot Actuator
");
sb.append("");
String url = "http://" + host + ":8090" + path + endPointPath;
sb.append("
");
return sb.toString();
}
}
// 访问 http://localhost:8888/GetEndPoints常用的一些内置endpoint
actuator/health 查看程序健康信息
actuator/metrics 查看监视标准
actuator/beans 列出程序中的Spring BEAN
actuator/env 列出程序运行所有信息
定义actuator/info特殊endpoint
actuator/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;
@Component
public class BuildInfoActuator implements InfoContributor{
@Override
public void contribute(Info.Builder builder) {
Map
actuator/shutdown需要post请求才能访问
可以用来关闭程序
定义如下控制器即可
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;
@Controller
public class ShutDownController {
@ResponseBody
@RequestMapping(path = "/shutdown")
public String actuatorShutdown(){
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
上一篇:二分法python实现