作者:追梦1819

原文:https://www.cnblogs.com/yanfei1819/p/11226397.html

版权声明:本文为博主原创文章,转载请附上博文链接!

引言

  很多文章都将 SpringBoot Actuator 的 Endpoint 翻译为 "端点"。不过我认为这这翻译失去了原有的意思。故本文中的 endpoint 依旧是 endpoint,不翻译为"端点"。

  通过引入 spring-boot-starter-actuator ,可以使用 SpringBoot 为我们提供的准生产环境下的应用监控和管理功能。我们可以通过 HTTP、JMX、SSH协议进行操作。自动得到审计、监控和指标操作。

步骤:

  1. 引入maven依赖;
  2. 通过 HTTP 方式访问监控端点;
  3. 可进行 shutdown(post提交,此端点默认关闭)。

原生endpoint

  SpringBoot 的 Actuator 有很多原生的端点,详细查看官网。Spring Boot 2.0 中的端点和之前的版本有较大不同,使用时需注意。启动时不是可以直接访问,需要先将其暴露出来。

本文中,我们讲述几个常用的端点。

  1. health

    主要用来检查应用的运行状态。如果应用有异常,同时给我们反馈异常原因。比如数据库连接异常,磁盘空间过小等异常。

  2. info

    自定义应用程序的配置信息。

    例如,在配置文件中配置如下信息:

    info.app.name=actuator
    info.app.versoin=1.0.0
    info.app.data=2019-06-25 12:00:00
    info.app.author=yanfei1819

    启动项目,访问http://localhost:8080/actuator/info,可以得到如下响应:

    {"app":{"name":"actuator","versoin":"1.0.0","data":"2019-06-25 12:00:00","author":"yanfei1819"}}
  3. beans

    该 endpoint 展示了 bean 的别名、类型、是否单例、类的地址、依赖等信息。

  4. conditions

    Spring Boot 的自动配置功能非常便利,但有时候也意味着出问题比较难找出具体的原因。使用 conditions 可以在应用运行时查看代码了某个配置在什么条件下生效,或者某个自动配置为什么没有生效。

  5. heapdump

    展示Jvm 的堆文件 heapdump。

  6. shutdown

    远程关闭应用的端点,不过需要注意两点:

    • 需要在配置文件中配置 management.endpoint.shutdown.enabled=true
    • 只支持 POST 请求。
  7. mappings

    程序中所有的 URI 路径,以及与控制器的关系。

  8. threaddump

    查看线程信息,例如线程名、线程ID、线程的状态、是否等待锁资源等。

使用

创建项目,引入 maven 依赖:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

启动项目,控制台打印信息:

可以看出此时只暴露了两个 endpoint。

访问 http://localhost:8080/actuator ,可以看到两个端点是:

{
"_links": {
"self": {
"href": "http://localhost:8080/actuator",
"templated": false
},
"health": {
"href": "http://localhost:8080/actuator/health",
"templated": false
},
"health-component": {
"href": "http://localhost:8080/actuator/health/{component}",
"templated": true
},
"health-component-instance": {
"href": "http://localhost:8080/actuator/health/{component}/{instance}",
"templated": true
},
"info": {
"href": "http://localhost:8080/actuator/info",
"templated": false
}
}
}

如果我们需要访问所有的原生 endpoint,需要在配置文件中加入:management.endpoints.web.exposure.include=*

重新启动项目,控制台日志是:

访问 http://localhost:8080/actuator ,可以看到所有端点是:

{
"_links": {
"self": {
"href": "http://localhost:8080/actuator",
"templated": false
},
"auditevents": {
"href": "http://localhost:8080/actuator/auditevents",
"templated": false
},
"beans": {
"href": "http://localhost:8080/actuator/beans",
"templated": false
},
"caches-cache": {
"href": "http://localhost:8080/actuator/caches/{cache}",
"templated": true
},
"caches": {
"href": "http://localhost:8080/actuator/caches",
"templated": false
},
"health": {
"href": "http://localhost:8080/actuator/health",
"templated": false
},
"health-component": {
"href": "http://localhost:8080/actuator/health/{component}",
"templated": true
},
"health-component-instance": {
"href": "http://localhost:8080/actuator/health/{component}/{instance}",
"templated": true
},
"conditions": {
"href": "http://localhost:8080/actuator/conditions",
"templated": false
},
"configprops": {
"href": "http://localhost:8080/actuator/configprops",
"templated": false
},
"env": {
"href": "http://localhost:8080/actuator/env",
"templated": false
},
"env-toMatch": {
"href": "http://localhost:8080/actuator/env/{toMatch}",
"templated": true
},
"info": {
"href": "http://localhost:8080/actuator/info",
"templated": false
},
"loggers": {
"href": "http://localhost:8080/actuator/loggers",
"templated": false
},
"loggers-name": {
"href": "http://localhost:8080/actuator/loggers/{name}",
"templated": true
},
"heapdump": {
"href": "http://localhost:8080/actuator/heapdump",
"templated": false
},
"threaddump": {
"href": "http://localhost:8080/actuator/threaddump",
"templated": false
},
"metrics": {
"href": "http://localhost:8080/actuator/metrics",
"templated": false
},
"metrics-requiredMetricName": {
"href": "http://localhost:8080/actuator/metrics/{requiredMetricName}",
"templated": true
},
"scheduledtasks": {
"href": "http://localhost:8080/actuator/scheduledtasks",
"templated": false
},
"httptrace": {
"href": "http://localhost:8080/actuator/httptrace",
"templated": false
},
"mappings": {
"href": "http://localhost:8080/actuator/mappings",
"templated": false
}
}
}

  读者可以逐个访问,查看对应的返回信息。

  当然,也可以通过配置 management.endpoints.web.exposure.exclude=info,trace 选择部分 endpoint 暴露。

  同时,Actuator 默认所有的监控点路径都在/actuator/*,当然如果有需要这个路径也支持定制。management.endpoints.web.base-path=/manage

自定义endpoint

以下:

package com.yanfei1819.actuator.endpoint;

import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.context.annotation.Configuration;
import java.util.HashMap;
import java.util.Map; /**
* Created by 追梦1819 on 2019-06-25.
*/
@Configuration
@Endpoint(id = "customize-endpoint") // 构建 rest api 的唯一路径
public class CustomizeEndPoint {
@ReadOperation
public Map<String, Object> endpoint() {
Map<String, Object> map = new HashMap<>(16);
map.put("message", "this is customize endpoint");
return map;
}
}

在配置文件中使其暴露:

management.endpoints.web.exposure.include=customize-endpoint

启动程序,访问 management.endpoints.web.exposure.include=customize-endpoint ,可以得到endpoint:

{
"_links": {
"self": {
"href": "http://localhost:8080/actuator",
"templated": false
},
"customize-endpoint": {
"href": "http://localhost:8080/actuator/customize-endpoint",
"templated": false
}
}
}

再访问返回的endpoint地址,得到相应:

{"message":"this is customize endpoint"}

可验证自定义 endpoint 成功。

总结

  对于作者来说,这个功能核心是对 endpoints 的理解(我对该功能的使用总结,大部分时间也是耗在了这个上面)。理解了每一个 endpoint ,基本大的方向就掌握了。剩下的就是细节问题了(细节问题无非就是"慢工出细活",简单)。

  另一个问题, Actuctor 的功能是实现了,可是大家有没有觉得用起来很别扭?查看一个监控信息,就访问一个路径,得到的就一连串的JSON,繁琐、复杂、不够直观。这实属让运维同学抓狂的问题。有没有好的解决方案呢?且听下回分解。

参考

SpringBoot官网

![](https://img2018.cnblogs.com/blog/1183871/201907/1183871-20190722155821232-374443563.png)

SpringBoot第二十二篇:应用监控之Actuator的更多相关文章

  1. SpringBoot第二十四篇:应用监控之Admin

    作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/11457867.html 版权声明:本文为博主原创文章,转载请附上博文链接! 引言   前一章(S ...

  2. Python开发【第二十二篇】:Web框架之Django【进阶】

    Python开发[第二十二篇]:Web框架之Django[进阶]   猛击这里:http://www.cnblogs.com/wupeiqi/articles/5246483.html 博客园 首页 ...

  3. SpringBoot非官方教程 | 第二十二篇: 创建含有多module的springboot工程

    转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springbot22-modules/ 本文出自方志朋的博客 这篇文 ...

  4. 【SpringCloud】第十二篇: 断路器监控(Hystrix Turbine)

    前言: 必需学会SpringBoot基础知识 简介: spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选. ...

  5. 史上最简单的SpringCloud教程 | 第十二篇: 断路器监控(Hystrix Dashboard)(Finchley版本)

    转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f12-dash/ 本文出自方志朋的博客 在我的第四篇文章断路 ...

  6. SpringCloud教程 | 第十二篇: 断路器监控(Hystrix Dashboard)

    版权声明:本文为博主原创文章,欢迎转载,转载请注明作者.原文超链接 ,博主地址:http://blog.csdn.net/forezp. http://blog.csdn.net/forezp/art ...

  7. 史上最简单的SpringCloud教程 | 第十二篇: 断路器监控(Hystrix Dashboard)

    转载请标明出处: 首发于:https://www.fangzhipeng.com/springcloud/2017/07/12/sc12-hystix-dashbd/ 本文出自方志朋的博客 最新Fin ...

  8. 第二十二篇、IO多路复用 一

    一.简介io多路复用 可以监听多个文件描述符(socket对象)(文件句柄),一旦文件句柄出现变化,就会感知到 Linux中的 select,poll,epoll(内核2.6以上) 都是IO多路复用的 ...

  9. SpringBoot | 第二十二章:定时任务的使用

    前言 上两章节,我们简单的讲解了关于异步调用和异步请求相关知识点.这一章节,我们来讲讲开发过程也是经常会碰见的定时任务.比如每天定时清理无效数据.定时发送短信.定时发送邮件.支付系统中的定时对账等等, ...

随机推荐

  1. gcc与vs2013的三个charset编译选项

    以gcc为例,它有三个命令选项:-finput-charset=gb18030-fexec-charset=utf-8-fwide-exec-charset=utf32顾名思议,input-chars ...

  2. QML学习【一】Basic Types

      QML入门教程(1) QML是什么? QML是一种描述性的脚本语言,文件格式以.qml结尾.语法格式非常像CSS(参考后文具体例子),但又支持javacript形式的编程控制.它结合了QtDesi ...

  3. Realm_King 之 .NET 打包详细教程(B)

    上篇(Realm_King 之 .NET 打包详细教程(A))给大家讲述了打包基本的操作,接下来帮助大家如何覆盖安装,希望大家仔细阅读... (一)看到你的解决方案,选中你的安装程序,点击F4会弹出改 ...

  4. 海康威视频监控设备Web查看系统(二):服务器篇

    声明:本系列文章只提供交流与学习使用.文章中所有涉及到海康威视设备的SDK均可在海康威视官方网站下载得到.文章中所有除官方SDK意外的代码均可随意使用,任何涉及到海康威视公司利益的非正常使用由使用者自 ...

  5. python三大主流web框架之Django安装、项目搭建

    这一篇我们将迎来python强大的web框架Django,相信大家都已经不陌生,本篇将介绍Django的安装及基础项目搭建,大神略过~ Django是需要我们手动pip安装的,首先我们来安装Djang ...

  6. 洛谷 题解 UVA658 【这不是bug,而是特性 It's not a Bug, it's a Feature!】

    [题意] 补丁在修正\(BUG\)时,有时也会引入新的\(BUG\),假定有\(n(n<=20)\)个潜在\(BUG\),和\(m(m<=100)\)个补丁,每个补丁用两个长度为\(n\) ...

  7. excel for mac打开csv文件不分列

    参考链接:http://www.1207.me/archives/247.html excel for mac在打开csv文件(逗号分隔的文本文件)的时候,不能像windows那样分列,而且全都挤在一 ...

  8. 【记录】mybatis中获取常量类中数据

    部分转载,已注明来源: 1.mybatis中获取常量类中数据 <update id="refuseDebt"> UPDATE dt_debt a SET         ...

  9. CI框架中的奇葩

    今天在win下开发,使用ci框架,本来是没有任何问题,然后转向了mac上开发,结果出现了个奇葩的问题,就是在ci框架中,控制器命名以"Admin_"为前缀的,在url中,控制器也必 ...

  10. 为什么string是引用类型 值还不可以修改

    C#把数据类型分为值类型和引用类型.值类型操作简单,引用类型更省空间. C#一共有15个预定义类型,其中13个值类型(8个整型.2个浮点类型.decimal.bool.char),2个引用类型(str ...