一 说明

Actuator 的定义

actuator 是一个制造术语,指的是用于移动或控制某物的机械装置。执行器可以通过一个小的变化产生大量的运动。


要将 actuator 添加到基于 Maven 的项目,请添加以下“Starter”依赖项:

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

Endpoints

Actuator endpoints 让您监控 application 并与之交互。 Spring Boot 包含许多 built-in endpoints 并允许您添加自己的。例如,health端点提供基本的 application 健康信息。

每个端点都可以是启用或禁用。它控制是否在 application context 中创建端点并且其 bean 是否存在。要远程访问,端点也必须是通过 JMX 或 HTTP 公开。大多数 applications 选择 HTTP,其中端点的 ID 以及/actuator的前缀映射到 URL。对于 example,默认情况下,health端点映射到/actuator/health

以下 technology-agnostic endpoints 可用:

ID 描述 默认情况下启用
auditevents 公开当前 application 的 audit events 信息。
beans 显示 application 中所有 Spring beans 的完整列表。
caches 暴露可用的缓存。
conditions 显示在 configuration 和 auto-configuration classes 上评估的条件以及它们执行或不执行 match 的原因。
configprops 显示所有@ConfigurationProperties的整理列表。
env 从 Spring 的ConfigurableEnvironment公开 properties。
flyway 显示已应用的任何 Flyway 数据库迁移。
health 显示 application 健康信息。
httptrace 显示 HTTP 跟踪信息(默认情况下,最后 100 个 HTTP request-response 交换)。
info 显示任意 application 信息。
integrationgraph 显示 Spring Integration 图。
loggers 显示并修改 application 中 loggers 的 configuration。
liquibase 显示已应用的任何 Liquibase 数据库迁移。
metrics 显示当前 application 的'metrics'信息。
mappings 显示所有@RequestMapping paths 的整理列表。
scheduledtasks 显示 application 中的计划任务。
sessions 允许从 Spring Session-backed session store 中检索和删除用户会话。使用 Spring Session 支持 reactive web applications 时不可用。
shutdown 让 application 正常关闭。 没有
threaddump 执行线程转储。

如果 application 是 web application(Spring MVC,Spring WebFlux 或 Jersey),则可以使用以下附加 endpoints:

ID 描述 默认情况下启用
heapdump 返回hprof堆转储文件。
jolokia 通过 HTTP 公开 JMX beans(当 Jolokia 在 classpath 上时,不适用于 WebFlux)。
logfile 返回日志文件的内容(如果已设置logging.filelogging.path properties)。支持使用 HTTP Range标头来检索 log 文件内容的一部分。
prometheus 以 Prometheus 服务器可以抓取的格式公开 metrics。

要了解有关 Actuator 的 endpoints 及其请求和响应格式的更多信息,请参阅单独的 API 文档(HTMLPDF)。

启用 Endpoints

默认情况下,启用除shutdown之外的所有 endpoints。要配置端点的启用,请使用其management.endpoint..enabled property。以下 example 启用shutdown端点:

management.endpoint.shutdown.enabled=true

如果您希望端点启用为 opt-in 而不是 opt-out,请将management.endpoints.enabled-by-default property 设置为false并使用单个端点enabled properties 重新加入。以下 example 启用info端点并禁用所有其他 endpoints:

management.endpoints.enabled-by-default=false
management.endpoint.info.enabled=true

已禁用 endpoints 完全从 application context 中删除。如果只想更改端点所暴露的技术,请改用包含和排除 properties

暴露 Endpoints

由于 Endpoints 可能包含敏感信息,因此应仔细考虑何时公开它们。以下 table 显示 built-in endpoints 的默认曝光:

ID JMX Web
auditevents 没有
beans 没有
caches 没有
conditions 没有
configprops 没有
env 没有
flyway 没有
health
heapdump N/A 没有
httptrace 没有
info
integrationgraph 没有
jolokia N/A 没有
logfile N/A 没有
loggers 没有
liquibase 没有
metrics 没有
mappings 没有
prometheus N/A 没有
scheduledtasks 没有
sessions 没有
shutdown 没有
threaddump 没有

要更改公开的 endpoints,请使用以下 technology-specific includeexclude properties:

属性 默认
management.endpoints.jmx.exposure.exclude  
management.endpoints.jmx.exposure.include *
management.endpoints.web.exposure.exclude  
management.endpoints.web.exposure.include info, health

include property 列出公开的 endpoints 的 ID。 exclude property 列出不应公开的 endpoints 的 ID。 exclude property 优先于include property。 includeexclude properties 都可以配置端点 ID 列表。

对于 example,要停止通过 JMX 公开所有 endpoints 并仅显示healthinfo endpoints,请使用以下 property:

management.endpoints.jmx.exposure.include=health,info

*可用于选择所有 endpoints。对于 example,要通过 HTTP 公开除envbeans endpoints 之外的所有内容,请使用以下 properties:

management.endpoints.web.exposure.include=*
management.endpoints.web.exposure.exclude=env,beans

*在 YAML 中有特殊含义,因此如果要包含(或排除)所有 endpoints,请务必添加引号,如下面的示例所示:

management:
endpoints:
web:
exposure:
include: "*"

如果您的 application 公开曝光,我们强烈建议您也保护你的 endpoints

如果要在公开 endpoints 时实现自己的策略,可以注册EndpointFilter bean。

保护 HTTP Endpoints

您应该像处理任何其他敏感 URL 一样注意保护 HTTP endpoints。如果存在 Spring Security,则默认使用 Spring Security 的 content-negotiation 策略保护 endpoints。如果您希望为 HTTP endpoints 配置自定义安全性,对于 example,只允许具有特定角色的用户访问它们,Spring Boot 提供了一些方便的RequestMatcher objects,可以与 Spring Security 结合使用。

典型的 Spring Security configuration 可能类似于以下 example:

@Configuration
public class ActuatorSecurity extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
http.requestMatcher(EndpointRequest.toAnyEndpoint()).authorizeRequests()
.anyRequest().hasRole("ENDPOINT_ADMIN")
.and()
.httpBasic();
}

}

前面的 example 使用EndpointRequest.toAnyEndpoint()来匹配任何端点的请求,然后确保所有端点都具有ENDPOINT_ADMIN角色。其他几种匹配方法也可以在EndpointRequest上找到。有关详细信息,请参阅 API 文档(HTMLPDF)。

如果在防火墙后部署 applications,您可能希望无需身份验证即可访问所有 actuator endpoints。您可以通过更改management.endpoints.web.exposure.includeproperty 来执行此操作,如下所示:

application.properties.

management.endpoints.web.exposure.include=*

此外,如果存在 Spring Security,则需要添加自定义安全性 configuration,以允许对 endpoints 进行未经身份验证的访问,如下面的示例所示:

@Configuration
public class ActuatorSecurity extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
http.requestMatcher(EndpointRequest.toAnyEndpoint()).authorizeRequests()
.anyRequest().permitAll();
}

}

配置 Endpoints

Endpoints 自动缓存对不带任何参数的读取操作的响应。要配置端点将缓存响应的 time 数量,请使用其cache.time-to-live property。以下 example 将beans端点缓存的 time-to-live 设置为 10 秒:

application.properties.

management.endpoint.beans.cache.time-to-live=10s

前缀management.endpoint.用于唯一标识正在配置的端点。

在进行经过身份验证的 HTTP 请求时,Principal被视为端点的输入,因此不会缓存响应。

Actuator Web Endpoints 的超媒体

添加了“发现页面”,其中包含指向所有 endpoints 的链接。默认情况下,“发现页面”在/actuator上可用。

配置自定义 management context 路径后,“发现页面”会自动从/actuator移动到 management context 的根目录。对于 example,如果 management context 路径为/management,则发现页面可从/management获得。当 management context 路径设置为/时,将禁用发现页面以防止与其他映射冲突的可能性。

CORS 支持

Cross-origin 资源共享(CORS)是一个W3C 规范,它允许您以灵活的方式指定哪种 cross-domain 请求被授权。如果您使用 Spring MVC 或 Spring WebFlux,则可以配置 Actuator 的 web endpoints 以支持此类方案。

默认情况下禁用 CORS 支持,仅在设置了management.endpoints.web.cors.allowed-origins property 后才启用 CORS 支持。以下 configuration 允许来自example.com域的GETPOST calls:

management.endpoints.web.cors.allowed-origins=http://example.com
management.endpoints.web.cors.allowed-methods=GET,POST

二 演示

  • pom.xml

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

    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
    <exclusions>
    <exclusion>
    <groupId>org.junit.vintage</groupId>
    <artifactId>junit-vintage-engine</artifactId>
    </exclusion>
    </exclusions>
    </dependency>
    </dependencies>
  • 启动配置类,发会发现(Exposing 2 endpoint(s),就是上面说的)

    http://localhost:8080/actuator/health

    http://localhost:8080/actuator/info

    2019-10-29 23:14:40.757  INFO 3372 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1270 ms
    2019-10-29 23:14:41.057 INFO 3372 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
    2019-10-29 23:14:41.268 INFO 3372 --- [ main] o.s.b.a.e.web.EndpointLinksResolver : Exposing 2 endpoint(s) beneath base path '/actuator'
    2019-10-29 23:14:41.329 INFO 3372 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
    2019-10-29 23:14:41.331 INFO 3372 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 2.442 seconds (JVM running for 5.186)
  • application.yml

    management:
    endpoints:
    web:
    exposure:
    include: "*"
    info:
    hello: world
    2019-10-29 23:33:21.756  INFO 4512 --- [           main] o.s.b.a.e.web.EndpointLinksResolver      : Exposing 13 endpoint(s) beneath base path '/actuator'
    2019-10-29 23:33:21.812 INFO 4512 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
    2019-10-29 23:33:21.816 INFO 4512 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 3.021 seconds (JVM running for 5.481)

    http://localhost:8080/actuator/metrics

    {
    "names": [
    "jvm.memory.max",
    "jvm.threads.states",
    "jvm.gc.memory.promoted",
    "jvm.memory.used",
    "jvm.gc.max.data.size",
    "jvm.gc.pause",
    "jvm.memory.committed",
    "system.cpu.count",
    "logback.events",
    "http.server.requests",
    "jvm.buffer.memory.used",
    "tomcat.sessions.created",
    "jvm.threads.daemon",
    "system.cpu.usage",
    "jvm.gc.memory.allocated",
    "tomcat.sessions.expired",
    "jvm.threads.live",
    "jvm.threads.peak",
    "process.uptime",
    "tomcat.sessions.rejected",
    "process.cpu.usage",
    "jvm.classes.loaded",
    "jvm.classes.unloaded",
    "tomcat.sessions.active.current",
    "tomcat.sessions.alive.max",
    "jvm.gc.live.data.size",
    "jvm.buffer.count",
    "jvm.buffer.total.capacity",
    "tomcat.sessions.active.max",
    "process.start.time"
    ]
    }

    http://localhost:8080/actuator/caches

    {"cacheManagers":{}}

    http://localhost:8080/actuator/info

{
"hello": "world"
}

SpringBoot2.X整合Actuator的更多相关文章

  1. SpringBoot2.x整合Prometheus+Grafana【附源码+视频】

    图文并茂,新手入门教程,建议收藏 SpringBoot2.x整合Prometheus+Grafana[附源码+视频] 附源码+视频 目录 工程简介 简介 Prometheus grafana Spri ...

  2. 【SpringBoot】息队列介绍和SpringBoot2.x整合RockketMQ、ActiveMQ

    ========================13.消息队列介绍和SpringBoot2.x整合RockketMQ.ActiveMQ ======================= 1.JMS介绍和 ...

  3. 第二篇:SpringBoot2.0整合ActiveMQ

    本篇开始将具体介绍SpringBoot如何整合其它项目. 如何创建SpringBoot项目 访问https://start.spring.io/. 依次选择构建工具Maven Project.语言ja ...

  4. 消息队列介绍和SpringBoot2.x整合RockketMQ、ActiveMQ 9节课

    1.JMS介绍和使用场景及基础编程模型     简介:讲解什么是小写队列,JMS的基础知识和使用场景     1.什么是JMS: Java消息服务(Java Message Service),Java ...

  5. SpringBoot2.x整合Redis实战 4节课

    1.分布式缓存Redis介绍      简介:讲解为什么要用缓存和介绍什么是Redis,新手练习工具 1.redis官网 https://redis.io/download          2.新手 ...

  6. SpringBoot2.0 整合 QuartJob ,实现定时器实时管理

    一.QuartJob简介 1.一句话描述 Quartz是一个完全由java编写的开源作业调度框架,形式简易,功能强大. 2.核心API (1).Scheduler 代表一个 Quartz 的独立运行容 ...

  7. SpringBoot2.0 整合 Swagger2 ,构建接口管理界面

    一.Swagger2简介 1.Swagger2优点 整合到Spring Boot中,构建强大RESTful API文档.省去接口文档管理工作,修改代码,自动更新,Swagger2也提供了强大的页面测试 ...

  8. SpringBoot2.x 整合Spring-Session实现Session共享

    SpringBoot2.x 整合Spring-Session实现Session共享 1.前言 发展至今,已经很少还存在单服务的应用架构,不说都使用分布式架构部署, 至少也是多点高可用服务.在多个服务器 ...

  9. SpringBoot2.0 整合 Dubbo框架 ,实现RPC服务远程调用

    一.Dubbo框架简介 1.框架依赖 图例说明: 1)图中小方块 Protocol, Cluster, Proxy, Service, Container, Registry, Monitor 代表层 ...

随机推荐

  1. 一个有意思的js块作用域问题

    1.问题 首先把问题放出来,昨天看了一个掘友发的一个问题,然后跟我同事一起研究了一下,没找出来是为什么,然后我回来一直在想为什么,然后各种找资料研究,从各个方面找为什么,比如js上下文,作用域,js垃 ...

  2. html实现打印预览效果

    前面说到利用lodop插件进行打印设置,那个应用于打印快递面单,或者跟快递面单相似场景的情况. 今天的利用html快速打印出A4纸大小的场景,例如:合同.静态文本等. 效果如下: 方式一 1.设置di ...

  3. 十大排序算法JavaScript实现总结

    花费了几周的时间断断续续的练习和模仿与使用JavaScript代码实现了十大排序算法. 里面有每种算法的动图和静态图片演示,看到图片可以自己先按照图片的思路实现一下. github中正文链接,点击查看 ...

  4. 一个PHP文件搞定微信H5支付

     / 更新于 2018-07-02 / 8 条评论 过年期间也坚持要撸码啊接着给博客除草,在这个小除夕是情人节的一天,祝大家新年快乐,情人节能够顺利脱单~~~ 回归正题,这篇文章介绍一下微信H5支付, ...

  5. tiki-graph_formula.php代码执行漏洞复现(或许??)

    1.不知道从哪里翻出来的虚拟机镜像(不知道甚么时候出现在磁盘里面的) 打开配置一下是tikiwiki这个东西 2.遇到陌生的玩意总是忍不住好奇心的,打开nikto扫描一下,发现有些奇怪的东西 本来没抱 ...

  6. 五、springboot 简单优雅是实现邮件服务

    前言 spring boot 的项目放下小半个月没有更新了,终于闲下来可以开心的接着写啦. 之前我们配置好mybatis 多数据源的,接下来我们需要做一个邮件服务.比如你注册的时候,需要输入验证码来校 ...

  7. ADB命令无法导出文件到物理机上处理办法

    因为想查看一下脚本生成的sqlite文件.就想导出文件,,结果导出adb pull命令一直报错.使用su也是错误的..最后发现adb pull 不能再adb的命令状态下执行.需要退出adb命令.然后直 ...

  8. BeetleX服务网关之限流和缓存

    限流和缓存相关是网关中两个非常重要的功能,前者是保障服务更可靠地运行,后者则可以大大提高应用的吞吐能力.Beetlex.Bumblebee微服务网关提供了两个扩展插件来实现这两个功能,分别是Beetl ...

  9. 1.Eclipse下载、常用配置、快捷键

    Eclipse官网下载:https://www.eclipse.org/downloads/packages/ 自动补全 位置:Eclipse——Window——Perferences——Java—— ...

  10. HTML5 video视频字幕的使用和制作

    一.video支持视频格式: 以下是三种最常用的格式 1. ogg格式:带有Theora视频编码(免费)+Vorbis音频编码的Ogg文件(免费) 支持的浏览器:firefox.chrome.oper ...