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. Codeforces Round #220 (Div. 2)

    链接 毒瘤场..... A题:,真码农题,直接干爆,枚举,注意越界问题,wa37的看这组数据1 10 1 5 2 2,应该是no //#pragma comment(linker, "/st ...

  2. vue2项目中better-scroll 插件使用时候页面不滚动

    参考这里 1.外面包裹层的高度没有设置或者条目的高度没有超过外面包裹层的高度 2.初始化BetterScroll的时机不对,当前元素还没有正常渲染出来,导致BetterScroll的高度的计算错误

  3. mysql 字段属性 与 排序

    mysql中常见的数据类型:varchar(n).float.int(n).bigint(n).date.datetime.text 字段属性 默认值:DEFAULT '默认值' 非空:NOT NUL ...

  4. 【scala】异常处理

    Scala 的异常处理和其它语言比如 Java 类似. 抛出异常 Scala 抛出异常的方法和 Java一样,使用 throw 方法 throw new IllegalArgumentExceptio ...

  5. html5新增语义化标签

    注意:body.section.nav 需要h1-h6. div.header则不需要. 1):<article> 显示一个独立的文章内容. 例如一篇完整的论坛帖子,一则网站新闻,一篇博客 ...

  6. JSON和list之间的转换

    谷歌的Gson.jar: //list转换为json Gson gson = new Gson(); List<Person> persons = new ArrayList<Per ...

  7. Python基础学习(第2天)

    第三课:序列(sequence) 1.序列是一种有顺序的元素的集合 序列可以包含1个或多个元素,也可以不包括任何元素: 序列中的元素可以是[基础数据类型]中任一种,也可以是[别的序列]. s1 = ( ...

  8. Django和pymysql搭建学员管理系统

    学员管理系统 项目规划阶段 项目背景 近年来老男孩教育的入学学员数量稳步快速增长,传统的excel统计管理学员信息的方式已经无法满足日渐增长的业务需求.因此公司急需一套方便易用的“学员管理系统”,来提 ...

  9. Android Issue分析方法(用anr来说明)

    Log的产生大家都知道 , 大家也都知道通过DDMS来看log , 但什么时候会产生log文件呢 ?一般在如下几种情况会产生log文件 . 1,程序异常退出 , uncaused exception ...

  10. H264子宏块的划分有哪些?

    每个分割或子宏块都有一个独立的运动补偿.每个 MV 必须被编码.传输,分割的选择也需编 码到压缩比特流中.对大的分割尺寸而言,MV 选择和分割类型只需少量的比特,但运动补偿残差 在多细节区域能量将非常 ...