Spring Boot 的监视器

此功能用来控制spring boot程序和查看程序信息

依赖

<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>

配置

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("<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.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<String,String> data= new HashMap<String,String>();
data.put("build.version", "1.0.0");
builder.withDetail("buildInfo", data);
}
}

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<String> requestBody = new HttpEntity<>("", headers);
String e = restTemplate.postForObject(url, requestBody, String.class); return "Result: " + e;
}
}

springboot成神之——监视器的更多相关文章

  1. springboot成神之——ioc容器(依赖注入)

    springboot成神之--ioc容器(依赖注入) spring的ioc功能 文件目录结构 lang Chinese English GreetingService MyRepository MyC ...

  2. springboot成神之——springboot入门使用

    springboot创建webservice访问mysql(使用maven) 安装 起步 spring常用命令 spring常见注释 springboot入门级使用 配置你的pom.xml文件 配置文 ...

  3. springboot成神之——mybatis和mybatis-generator

    项目结构 依赖 generator配置文件 properties配置 生成文件 使用Example 本文讲解如何在spring-boot中使用mybatis和mybatis-generator自动生成 ...

  4. springboot成神之——swagger文档自动生成工具

    本文讲解如何在spring-boot中使用swagger文档自动生成工具 目录结构 说明 依赖 SwaggerConfig 开启api界面 JSR 303注释信息 Swagger核心注释 User T ...

  5. springboot成神之——log4j2的使用

    本文介绍如何在spring-boot中使用log4j2 说明 依赖 日志记录语句 log4j2配置文件 本文介绍如何在spring-boot中使用log4j2 说明 log4j2本身使用是非常简单的, ...

  6. springboot成神之——mybatis在spring-boot中使用的几种方式

    本文介绍mybatis在spring-boot中使用的几种方式 项目结构 依赖 WebConfig DemoApplication 方式一--@Select User DemoApplication ...

  7. springboot成神之——application.properties所有可用属性

    application.properties所有可用属性 # =================================================================== # ...

  8. springboot成神之——springboot+mybatis+mysql搭建项目简明demo

    springboot+mybatis+mysql搭建项目简明demo 项目所需目录结构 pom.xml文件配置 application.properties文件配置 MyApplication.jav ...

  9. springboot成神之——websocket发送和请求消息

    本文介绍如何使用websocket发送和请求消息 项目目录 依赖 DemoApplication MessageModel WebConfig WebSocketConfig HttpHandshak ...

随机推荐

  1. 谈一下思考,关于mybatis中<foreach collection="list">中list得来的原因 没看到官方说明

    <foreach> 是在sql语句中进行多个id查询 时用到的,因为mybatis代替jdbc和hibernate, 使用 在xml文件中编写sql语句,这是一个标签文件.然后在 dao层 ...

  2. Juint 单元测试(2)

    单元测试(junit testing),是指对软件中的最小可测试单元进行检查和验证.Java里单元指一个类. JUnit ,是一个开源的Java单元测试框架,是 Java的标准单元测试库,是非常重要第 ...

  3. poscms仿站知识点总结(二)

    1.相同类型div添加不同class 遇到一个样式上的问题,模板页面有8个子项,样式都是一样,至于数据是可以用for循环来添加的,但是for循环的时候,每个div的 类名是无法及时更改的,但是模板页面 ...

  4. Linux 工具套件 —— binutils、readelf

    readelf:Linux 下专门针对 ELF 文件格式的解析器: 0. binutils GNU Binutils gnu binutils 一套二进制工具的集合,主要包含:ld(gnu linke ...

  5. (七)js函数一

    1.函数概念:函数是由事件驱动的或者当它被调用时执行的可重复使用的代码块. 语法:fuction hello(){         code...     } a)手动驱动: eg: hello(); ...

  6. 让cocos h5里的文字可以在手机上被长按复制

    更改CCBoot.js代码: // Adjust mobile css settings if (cc.sys.isMobile) { var fontStyle = document.createE ...

  7. 高级C/C++编译技术之读书笔记(二)之库的概念

    最近有幸阅读了<高级C/C++编译技术>深受启发,该书深入浅出地讲解了构建过程(编译.链接)中的各种细节,从多个角度展示了程序与库文件或代码的集成方法,提出了面向代码复用和系统集成的软件架 ...

  8. vc++ windows 创建桌面快捷方式

    创建桌面快捷方式 在windows软件开发中,软件安装过程中总是需要在桌面创建快捷方式,下面介绍一种创建桌面快捷方式的方法,具体代码如下: /* * 创建快捷方式 * szExePath[in]:要创 ...

  9. 【转载】Allegro Auto Rename器件反标注教程

    Cadence设计时一般不主张在PCB文件中更改Logic(PADS的ECO更改),不过Auto Rename仍然是非常实用的功能,按照布局重排位号,可以让PCB的丝印标识更清晰,容易检查,位号易找, ...

  10. 洛谷 P2945 [USACO09MAR]沙堡Sand Castle

    传送门 题目大意: ai,ai+1,ai+2... 变成 bi,bi+1,bi+2.. 不计顺序,增加和减少a数组均有代价. 题解:贪心+排序 小的对应小的 代码: #include<iostr ...