Spring Boot 提供的端点不能满足我们的业务需求时,我们可以自定义一个端点。

本文,我将演示一个简单的自定义端点,用来查看服务器的当前时间,它将返回两个参数,一个是标准的包含时区的当前时间格式,一个是当前时间的时间戳格式。

继承 AbstractEndpoint 抽象类

首先,我们需要继承 AbstractEndpoint 抽象类。因为它是 Endpoint 接口的抽象实现,此外,我们还需要重写 invoke 方法。

值得注意的是,通过设置 @ConfigurationProperties(prefix = “endpoints.servertime”),我们就可以在 application.properties 中通过 endpoints.servertime 配置我们的端点。

  1. @ConfigurationProperties(prefix="endpoints.servertime")
  2. public class ServerTimeEndpoint extends AbstractEndpoint<Map<String, Object>> {
  3. public ServerTimeEndpoint() {
  4. super("servertime", false);
  5. }
  6. @Override
  7. public Map<String, Object> invoke() {
  8. Map<String, Object> result = new HashMap<String, Object>();
  9. DateTime dateTime = DateTime.now();
  10. result.put("server_time", dateTime.toString());
  11. result.put("ms_format", dateTime.getMillis());
  12. return result;
  13. }
  14. }

上面的代码,我解释下,两个重要的细节。

  • 构造方法 ServerTimeEndpoint,两个参数分别表示端点 ID 和是否端点默认是敏感的。我这边设置端点 ID 是 servertime,它默认不是敏感的。
  • 我们需要通过重写 invoke 方法,返回我们要监控的内容。这里我定义了一个 Map,它将返回两个参数,一个是标准的包含时区的当前时间格式,一个是当前时间的时间戳格式。

创建端点配置类

创建端点配置类,并注册我们的端点 ServerTimeEndpoint。

  1. @Configuration
  2. public class EndpointConfig {
  3. @Bean
  4. public static Endpoint<Map<String, Object>> servertime() {
  5. return new ServerTimeEndpoint();
  6. }
  7. }

运行

启动应用

  1. @RestController
  2. @EnableAutoConfiguration
  3. @ComponentScan(basePackages = { "com.lianggzone.springboot" })
  4. public class RestfulApiWebDemo {
  5. @RequestMapping("/home")
  6. String home() {
  7. return "Hello World!";
  8. }
  9. public static void main(String[] args) throws Exception {
  10. SpringApplication.run(RestfulApiWebDemo.class, args);
  11. }
  12. }

访问 http://localhost:8080/servertime ,此时,你会服务器的当前时间。

  1. {
  2. "ms_format": 1484271566958,
  3. "server_time": "2017-01-13T09:39:26.958+08:00"
  4. }

源代码

相关示例完整代码: springboot-action

(完)

如果觉得我的文章对你有帮助,请随意打赏。

Spring Boot 揭秘与实战(九) 应用监控篇 - 自定义监控端点的更多相关文章

  1. Spring Boot 揭秘与实战(九) 应用监控篇 - HTTP 健康监控

    文章目录 1. 内置 HealthIndicator 监控检测 2. 自定义 HealthIndicator 监控检测 3. 源代码 Health 信息是从 ApplicationContext 中所 ...

  2. Spring Boot 揭秘与实战(九) 应用监控篇 - HTTP 应用监控

    文章目录 1. 快速开始 2. 监控和管理端点3. 定制端点 2.1. health 应用健康指标 2.2. info 查看应用信息 2.3. metrics 应用基本指标 2.4. trace 基本 ...

  3. Spring Boot 揭秘与实战(一) 快速上手

    文章目录 1. 简介 1.1. 什么是Spring Boot 1.2. 为什么选择Spring Boot 2. 相关知识 2.1. Spring Boot的spring-boot-starter 2. ...

  4. Spring Boot 揭秘与实战 附录 - Spring Boot 公共配置

    Spring Boot 公共配置,配置 application.properties/application.yml 文件中. 摘自:http://docs.spring.io/spring-boot ...

  5. Spring Boot 揭秘与实战 自己实现一个简单的自动配置模块

    文章目录 1. 实战的开端 – Maven搭建 2. 参数的配置 - 属性参数类 3. 真的很简单 - 简单的服务类 4. 自动配置的核心 - 自动配置类 5. spring.factories 不要 ...

  6. Spring Boot 揭秘与实战 源码分析 - 工作原理剖析

    文章目录 1. EnableAutoConfiguration 帮助我们做了什么 2. 配置参数类 – FreeMarkerProperties 3. 自动配置类 – FreeMarkerAutoCo ...

  7. Spring Boot 揭秘与实战 源码分析 - 开箱即用,内藏玄机

    文章目录 1. 开箱即用,内藏玄机 2. 总结 3. 源代码 Spring Boot提供了很多”开箱即用“的依赖模块,那么,Spring Boot 如何巧妙的做到开箱即用,自动配置的呢? 开箱即用,内 ...

  8. Spring Boot 揭秘与实战(八) 发布与部署 - 远程调试

    文章目录 1. 依赖 2. 部署 3. 调试 4. 源代码 设置远程调试,可以在正式环境上随时跟踪与调试生产故障. 依赖 在 pom.xml 中增加远程调试依赖. <plugins> &l ...

  9. Spring Boot 揭秘与实战(八) 发布与部署 - 开发热部署

    文章目录 1. spring-boot-devtools 实现热部署 2. Spring Loaded 实现热部署 3. 模板文件热部署 4. 源代码 Spring Boot 支持页面与类文件的热部署 ...

随机推荐

  1. OCP-1Z0-051-V9.02-13题 单引号的使用

    13. View the Exhibit and examine the structure of the PRODUCTS table. You need to generate a report ...

  2. Oracle Log Block Size

    Although the size of redo entries is measured in bytes, LGWR writes the redo to the log files on dis ...

  3. CAS5.3-搭建https服务器

    在上一篇文章中https://www.cnblogs.com/zhi-leaf/p/10417627.html.我们使用http://127.0.0.1:8080/cas/登录发现页面显示如下警告.该 ...

  4. 银联接口C#

    银联支付: ChinaPay的会员商户接入支付平台,以方便商户开展网上支付交易. 持卡人从商户网站中生成订单信息,通过公共支付交易平台中的支付网关子系统进行支付的过程,其交易流程包括订单确认.支付处理 ...

  5. pyhton字符串

    a = 5 # 1 + 1 = 10 + 1 = 11 + 1 = 100 + 1 = 101print(a.bit_length()) # 计算一个数字的二进制长度. a = 10# print(t ...

  6. mysql插入中文乱码

    https://www.cnblogs.com/zhchoutai/p/7364835.html 最简单的一招,不用修改my.ini文件: 1.停掉mysql服务 2.启动:X:\%path%\MyS ...

  7. HTML5 ①

    <!DOCTYPE html> <!--声明当前页面是H5--> html框架: <html> <head lang="en"> & ...

  8. OO作业总结报告3

    规格化设计的发展史 下面部分来源:https://www.cnblogs.com/eggert/p/9098446.html: 随着计算机硬件的飞速发展,以及应用复杂度越来越高,软件规模越来越大,原有 ...

  9. day042 前端CSS选择器

    今日内容: 高级选择器 1.子类选择器 用 > 表示 类比于相对路径 选择的是前一级标签的子标签 2后代选择器 用空格表示 选择的是前一级标签的后代标签 3并集选择器 用,逗号表示 选择的是用逗 ...

  10. 《Python》网络编程之黏包

    黏包 一.黏包现象 同时执行多条命令之后,得到的结果很可能只有一部分,在执行其他命令的时候又接收到之前执行的另外一部分结果,这种显现就是黏包. server端 import socket sk = s ...