前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SpringBoot系列之actuator监控管理极速入门与实践

SpringBoot系列之actuator监控管理极速入门与实践

作者头像
SmileNicky
发布2020-07-24 16:37:50
1.6K0
发布2020-07-24 16:37:50
举报
文章被收录于专栏:Nicky's blog

SpringBoot系列之actuator监控管理极速入门与实践

SpringBoot官方提供了spring-boot-starter-actuator场景启动器用于系统的监控管理,可以通过HTTP,JMX,SSH协议来进行操作,自动得到审计、健康及指标信息等

环境准备:

  • JDK 1.8
  • SpringBoot2.2.1
  • Maven 3.2+
  • 开发工具
    • IntelliJ IDEA
    • smartGit

创建一个SpringBoot Initialize项目,详情可以参考我之前博客:SpringBoot系列之快速创建项目教程

要将执行器添加到基于Maven的项目中,请检查添加以下“ Starter”依赖项:

代码语言:javascript
复制
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
</dependencies>

项目启动成功后,如果没设置context-path,项目会自动加入/actuator作为前缀,大部分端点是默认启动的,不过要通过web浏览器方式访问的只有health、info端点

可以通过配置修改默认前缀

代码语言:javascript
复制
management.endpoints.web.base-path=/actuator

通用的端点(http、Jms、ssh方式都能访问):

ID

描述

默认启用

auditevents

暴露当前应用程序的审计事件信息。

beans

显示应用程序中所有 Spring bean 的完整列表。

caches

暴露可用的缓存。

conditions

显示在配置和自动配置类上评估的条件以及它们匹配或不匹配的原因。

configprops

显示所有 @ConfigurationProperties 的校对清单。

env

暴露 Spring ConfigurableEnvironment 中的属性。

flyway

显示已应用的 Flyway 数据库迁移。

health

显示应用程序健康信息

httptrace

显示 HTTP 追踪信息(默认情况下,最后 100 个 HTTP 请求/响应交换)。

info

显示应用程序信息。

integrationgraph

显示 Spring Integration 图。

loggers

显示和修改应用程序中日志记录器的配置。

liquibase

显示已应用的 Liquibase 数据库迁移。

metrics

显示当前应用程序的指标度量信息。

mappings

显示所有 @RequestMapping 路径的整理清单。

scheduledtasks

显示应用程序中的调度任务。

sessions

允许从 Spring Session 支持的会话存储中检索和删除用户会话。当使用 Spring Session 的响应式 Web 应用程序支持时不可用。

shutdown

正常关闭应用程序。POST请求方式

threaddump

执行线程 dump。

GET方式调用health端点,返回json信息

Web 应用程序(Spring MVC、Spring WebFlux 或 Jersey),则可以使用以下附加端点,这个应该是2.x版本才加上的

ID

描述

默认启用

heapdump

返回一个 hprof 堆 dump 文件。

jolokia

通过 HTTP 暴露 JMX bean(当 Jolokia 在 classpath 上时,不适用于 WebFlux)。

logfile

返回日志文件的内容(如果已设置 logging.file 或 logging.path 属性)。支持使用 HTTP Range 头来检索部分日志文件的内容。

prometheus

以可以由 Prometheus 服务器抓取的格式暴露指标。

启用端点,修改配置,语法management.endpoint.[端点名称].enabled=true

代码语言:javascript
复制
management.endpoint.shutdown.enabled=true

下表显示了内置端点和默认暴露情况,以JMX、WEB(Http)做对比:

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

要更改暴露的端点,请使用以下特定的 includeexclude 属性:

属性

默认

management.endpoints.jmx.exposure.exclude

management.endpoints.jmx.exposure.include

*

management.endpoints.web.exposure.exclude

management.endpoints.web.exposure.include

info, health

include 属性列出了暴露的端点的 ID。exclude 属性列出了不应暴露的端点的 ID。exclude 属性优先于 include 属性。

例子:

关闭jmx访问所有端点的权限,只让其能访问health、info

代码语言:javascript
复制
management.endpoints.jmx.exposure.include=health,info

启用web访问所有端点,除env之外的权限

代码语言:javascript
复制
management.endpoints.web.exposure.include=*
management.endpoints.web.exposure.exclude=env

注意

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

代码语言:javascript
复制
management:
  endpoints:
    web:
      exposure:
        include: "*"

自定义InfoContributor

代码语言:javascript
复制
package com.example.springboot.actuator.actuate.health;

import java.util.Collections;

import org.springframework.boot.actuate.info.Info;
import org.springframework.boot.actuate.info.InfoContributor;
import org.springframework.stereotype.Component;

@Component
public class ExampleInfoContributor implements InfoContributor {

    @Override
    public void contribute(Info.Builder builder) {
        builder.withDetail("example",
                Collections.singletonMap("key", "value"));
    }

}

可以在浏览器或者postman调用:

跨域支持配置

代码语言:javascript
复制
management.endpoints.web.cors.allowed-origins=http://localhost
management.endpoints.web.cors.allowed-methods=GET,POST

定置端点:

代码语言:javascript
复制
management.endpoint.info.enabled=true
management.endpoint.info.cache.time-to-live=10s

ok,actuator的知识点比较多,详情请参考官方文档,本博客参考官方文档,做了简单记录,仅仅作为入门参考手册

代码例子下载:code download

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/07/23 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • SpringBoot系列之actuator监控管理极速入门与实践
相关产品与服务
数据传输服务
腾讯云数据传输服务(Data Transfer Service,DTS)可帮助用户在业务不停服的前提下轻松完成数据库迁移上云,利用实时同步通道轻松构建高可用的数据库多活架构,通过数据订阅来满足商业数据挖掘、业务异步解耦等场景需求。同时,DTS 还提供私有化独立输出版本 DTS-DBbridge,支持异构数据库和同构数据库之间迁移和同步,可以帮助企业实现完整数据库迁移(如 Oracle)。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档