Spring Boot中的监视器简介说明
下文笔者讲述SpringBoot中监视器的简介说明,如下所示
SpringBoot监视器
Spring boot监视器可帮助 您访问生产环境中正在运行的应用程序的当前状态 Spring boot actuator是spring启动框架中的重要功能 SpringBoot监视器还有一大用途: 向相关人员触发警报消息 监视器模块对外提供http url访问的方式检查各种状态
SpringBoot监视器的使用方法
实现思路: 直接引入相应的依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-actuator</artifactId> </dependency>例:SpringBoot中应用监视器
一、引入相关依赖 <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> 二、进行相关配置 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.java265.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("<h2>Sprig Boot Actuator</h2>"); sb.append("<ul>"); String url = "http://" + host + ":8090" + path + endPointPath; sb.append("<li><a href='" + url + "'>" + url + "</a></li>"); sb.append("</ul>"); 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.java265.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<String,String> data= new HashMap<String,String>(); data.put("build.version", "1.0.0"); builder.withDetail("buildInfo", data); } } actuator/shutdown需要post请求才能访问 package com.java265.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<String> requestBody = new HttpEntity<>("", headers); String e = restTemplate.postForObject(url, requestBody, String.class); return "Result: " + e; } }
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。