一、Actuator 介绍

Actuator 是 SpringBoot 项目中一个非常强大一个功能,有助于对应用程序进行监视和管理,通过 restful api 请求来监管、审计、收集应用的运行情况。

Actuator 的核心是端点 Endpoint,它用来监视应用程序及交互,spring-boot-actuator 中已经内置了非常多的 Endpoint(health、info、beans、metrics、httptrace、shutdown等等),同时也允许我们自己扩展自己的 Endpoints。每个 Endpoint 都可以启用和禁用。要远程访问 Endpoint,还必须通过 JMX 或 HTTP 进行暴露,大部分应用选择HTTP,Endpoint 的ID默认映射到一个带 /actuator 前缀的URL。例如,health 端点默认映射到 /actuator/health

二、Actuator 使用

启用 Actuator 最简单方式是添加 spring-boot-starter-actuator ‘Starter’依赖。

1、pom.xml

<!-- 2、监控 —— Actuator插件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

2、application.yml

management:
endpoints:
# 暴露 EndPoint 以供访问,有jmx和web两种方式,exclude 的优先级高于 include
jmx:
exposure:
exclude: '*'
include: '*'
web:
exposure:
# exclude: '*'
include: ["health","info","beans","mappings","logfile","metrics","shutdown","env"]
base-path: /actuator # 配置 Endpoint 的基础路径
cors: # 配置跨域资源共享
allowed-origins: http://example.com
allowed-methods: GET,POST
enabled-by-default: true # 修改全局 endpoint 默认设置
endpoint:
auditevents: # 1、显示当前引用程序的审计事件信息,默认开启
enabled: true
cache:
time-to-live: 10s # 配置端点缓存响应的时间
beans: # 2、显示一个应用中所有 Spring Beans 的完整列表,默认开启
enabled: true
conditions: # 3、显示配置类和自动配置类的状态及它们被应用和未被应用的原因,默认开启
enabled: true
configprops: # 4、显示一个所有@ConfigurationProperties的集合列表,默认开启
enabled: true
env: # 5、显示来自Spring的 ConfigurableEnvironment的属性,默认开启
enabled: true
flyway: # 6、显示数据库迁移路径,如果有的话,默认开启
enabled: true
health: # 7、显示健康信息,默认开启
enabled: true
show-details: always
info: # 8、显示任意的应用信息,默认开启
enabled: true
liquibase: # 9、展示任何Liquibase数据库迁移路径,如果有的话,默认开启
enabled: true
metrics: # 10、展示当前应用的metrics信息,默认开启
enabled: true
mappings: # 11、显示一个所有@RequestMapping路径的集合列表,默认开启
enabled: true
scheduledtasks: # 12、显示应用程序中的计划任务,默认开启
enabled: true
sessions: # 13、允许从Spring会话支持的会话存储中检索和删除(retrieval and deletion)用户会话。使用Spring Session对反应性Web应用程序的支持时不可用。默认开启。
enabled: true
shutdown: # 14、允许应用以优雅的方式关闭,默认关闭
enabled: true
threaddump: # 15、执行一个线程dump
enabled: true
# web 应用时可以使用以下端点
heapdump: # 16、 返回一个GZip压缩的hprof堆dump文件,默认开启
enabled: true
jolokia: # 17、通过HTTP暴露JMX beans(当Jolokia在类路径上时,WebFlux不可用),默认开启
enabled: true
logfile: # 18、返回日志文件内容(如果设置了logging.file或logging.path属性的话),支持使用HTTP Range头接收日志文件内容的部分信息,默认开启
enabled: true
prometheus: #19、以可以被Prometheus服务器抓取的格式显示metrics信息,默认开启
enabled: true

这大抵就是全部默认的 Endpoint 的配置了,怎么样?强大吧!之前做了一个网络监控的项目,就是能够实时查看服务器的 CPU、内存、磁盘、IO 这些(基于 sigar.jar 实现),然后现在发现 SpringBoot 就这样轻松支持了,还更强大,更简便......

默认的 Endpoint 映射前缀是 /actuator,可以通过如上 base-path 自定义设置。

每个 Endpoint 都可以配置开启或者禁用。但是仅仅开启 Endpoint 是不够的,还需要通过 jmx 或者 web 暴露他们,通过 exclude 和 include 属性配置。

3、效果

做好了如上的配置,接下来我们只需要访问对应的 Endpoint 就可以啦,/actuator/[Endpoint ID](http://127.0.0.1:8080/actuator/health)。

三、自定义 Endpoint

自定义 Endpoint 端点,只需要在我们的新建Bean上使用 @Endpoint 注解即可。则 Bean 中的方法就可以通过 JMX 或者 HTTP 公开。除此之外,你还可以使用 @JmxEndpoint@WebEndpoint 编写 EndPoint。但是这些 EndPoint 仅限于各自的公开方式。例如,@WebEndpoint 仅通过HTTP公开,而不通过JMX公开。

那么是不是类中所有的方法都支持对外公开呢?很明显不是的。这里又要提到三个注解,只有加三个注解的方法才支持对外公开,并且每个注解都有支持它的 HTTP method。如下:

Operation HTTP method
@ReadOperation GET
@WriteOperation POST
@DeleteOperation DELETE

Endpoint 上的操作通过参数接收输入。 当通过网络公开时,这些参数的值取自URL的查询参数和JSON请求主体。 通过JMX公开时,参数将映射到MBean操作的参数。参数默认是必需的,可以通过使用 @Nullable 注释使其成为可选的。

可以通过使用 @Selector 注释操作方法的一个或多个参数来进一步定制路径。@Selector 会将路径上的参数作为变量传递给操作方法。这个注解有点诡异,且听我徐徐道来~~

1、Endpoint Bean

@Component
@Endpoint(id = "my", enableByDefault = true) //设置 id,并选择是否默认开启
public class MyEndPoint { @ReadOperation
public List<String> getPaths() {
List<String> list = new ArrayList<>();
list.add("java");
list.add("c++");
list.add("python");
return list;
} @ReadOperation
public String get(@Selector String arg0) {
return arg0;
}
@WriteOperation
public String post() {
return "post";
} @DeleteOperation
public Integer delete() {
return 1;
}
}

2、暴露 Endpoint

设置好了上面的 Endpoint Bean,还不能真正的访问到它们,需要在 application.yml 中将它们暴露出来:

management:
endpoints:
# 暴露 EndPoint 以供访问,有jmx和web两种方式,exclude 的优先级高于 include
jmx:
exposure:
exclude: '*'
include: '*'
web:
exposure:
# exclude: '*'
include: ["health","info","beans","mappings","logfile","metrics","shutdown","env","my"]

做好这些配置后,你就能访问到 Endpoint 了(http://127.0.0.1:8080/actuator/my)

3、@Selector

注意到没有,上面的 Endpoint 有一个 @Selector 参数的方法,并且参数名是 arg0,这个参数名是有学问滴......

原来我给的参数名是 path,原来我设想我可以访问 /actuator/my/[任意字符] 的路径,但是会报 400 参数不匹配错误。但是嘞,/actuator/my/[任意字符]?path=[任意字符] 是正常访问的,真是奇了怪了!。

原来,为了使 @Selector 正常工作,必须使用嵌入的参数名称编译 Endpoint(-parameters),如下。或者将参数名改为 arg0 就能达到目的。这个是 stackoverflow 上的一个解释~

<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>
</configuration>
</plugin>
<!-- 或者:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<parameters>true</parameters>
</configuration>
</plugin>
-->
</plugins>

tips: -parameters 的方式我没有验证通过呀~~汗

演示源代码:https://github.com/JMCuixy/Thymeleaf

SpringBoot 之Actuator.的更多相关文章

  1. springboot集成Actuator

    Actuator监控端点,主要用来监控与管理. 原生端点主要分为三大类:应用配置类.度量指标类.操作控制类. 应用配置类:获取应用程序中加载的配置.环境变量.自动化配置报告等与SpringBoot应用 ...

  2. SpringBoot系列: Actuator监控

    Sprng Boot 2 actuator变动加大, 网上很多资料都都已经过期. ============================配置项============================ ...

  3. SpringBoot 开启 Actuator

    在生产环境中,需要实时或定期监控服务的可用性.spring-boot 的actuator(监控)功能提供了很多监控所需的接口.简单的配置和使用如下: 1.引入依赖: <dependency> ...

  4. SpringBoot集成actuator模块的基本使用

    © 版权声明:本文为博主原创文章,转载请注明出处 1. 版本 SpringBoot:2.0.0.RELEASE 2. 集成 SpringBoot集成actuator模块非常简单,只需要引入actuat ...

  5. SpringBoot集成Actuator监控管理

    1.说明 本文详细介绍Spring Boot集成Actuator监控管理的方法, 基于已经创建好的Spring Boot工程, 然后引入Actuator依赖, 介绍监控管理相关功能的使用. Sprin ...

  6. springboot 监控 Actuator

    springboot 提供了对项目的监控功能. 1.首先添加依赖包 <!-- https://mvnrepository.com/artifact/org.springframework.boo ...

  7. SpringBoot之actuator

    在springBoot中集成actuator可以很方便的管理和监控应用的状态. 暴露的Restful接口有: HTTP方法 路径 描述 鉴权 GET /autoconfig 查看自动配置的使用情况 t ...

  8. SpringBoot系列:五、SpringBoot使用Actuator

    Actuator为springboot提供了运行状态监控的功能 通过集成它我们可以试试获取到应用程序的运行信息 首先,在pom.xml中引入起步依赖 <dependency> <gr ...

  9. Springboot之actuator未授权访问

    copy 子杰的哈,懒的写了 0x01  未授权访问可以理解为需要授权才可以访问的页面由于错误的配置等其他原因,导致其他用户可以直接访问,从而引发各种敏感信息泄露. 0x02 Spring Boot ...

随机推荐

  1. Python练手例子(15)

    85.输入一个奇数,然后判断最少几个 9 除于该数的结果为整数. 程序分析:999999 / 13 = 76923. #!/usr/bin/python #coding=utf-8 if __name ...

  2. FBOSS: Building Switch Software at Scale

    BOSS: 大规模环境下交换机软件构建 本文为SIGCOMM 2018 论文,由Facebook提供. 本文翻译了论文的关键内容. 摘要: 在网络设备(例如交换机和路由器)上运行的传统软件,通常是由供 ...

  3. Mesos源码分析(11): Mesos-Master接收到launchTasks消息

    根据Mesos源码分析(6): Mesos Master的初始化中的代码分析,当Mesos-Master接收到launchTask消息的时候,会调用Master::launchTasks函数.   v ...

  4. MySQL 分区建索引

    200 ? "200px" : this.width)!important;} --> 介绍 mysql分区后每个分区成了独立的文件,虽然从逻辑上还是一张表其实已经分成了多张 ...

  5. 百度地图API 自定义标注图标

    通过Icon类可实现自定义标注的图标,下面示例通过参数MarkerOptions的icon属性进行设置, 也可以使用marker.setIcon()方法. <script type=" ...

  6. Redis结合Lua脚本实现高并发原子性操作

    从 2.6版本 起, Redis 开始支持 Lua 脚本 让开发者自己扩展 Redis … 案例-实现访问频率限制: 实现访问者 $ip 在一定的时间 $time 内只能访问 $limit 次. 非脚 ...

  7. Android OpenSL ES 开发:OpenSL ES利用SoundTouch实现PCM音频的变速和变调

    缘由 OpenSL ES 学习到现在已经知道 OpenSL ES 不仅能播放和录制PCM音频数据,还能改变声音大小.设置左声道或右声道播放.还能变速播放,可谓是播放音频的王者.但是变速有一点不好的就是 ...

  8. [Bash]LeetCode194. 转置文件 | Transpose File

    Given a text file file.txt, transpose its content. You may assume that each row has the same number ...

  9. [Swift]LeetCode263. 丑数 | Ugly Number

    Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...

  10. [Swift]LeetCode665. 非递减数列 | Non-decreasing Array

    Given an array with n integers, your task is to check if it could become non-decreasing by modifying ...