1.说明

本文详细介绍Spring Boot集成Actuator监控管理的方法,
基于已经创建好的Spring Boot工程,
然后引入Actuator依赖,
介绍监控管理相关功能的使用。
Spring Boot提供的准生产环境下的应用监控和管理功能,
可以通过HTTP,JMX协议来进行操作,
自动获得审计、运行状态及度量指标等信息。
本文主要介绍通过HTTP进行监控和管理,
另外通过配置SSL,能够支持HTTPS协议操作。

2.引入依赖

修改pom.xml文件,
引入spring-boot-starter-actuator依赖:

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

实际使用的版本是2.3.1.RELEASE,
后面的演示说明都是基于这个版本。

3.启动服务

配置文件和启动类都无需修改,
只要是一个Spring Boot微服务就行。
在application.yml文件指定了服务启动端口,
方便后面使用HTTP访问:

server:
port: 8011

启动成功日志:

.   ____          _            __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.3.1.RELEASE) 17:17:10.366 [restartedMain] INFO com.yuwen.spring.actuator.ActuatorApplication - Starting ActuatorApplication on yuwen-asiainfo with PID 9740 (D:\Code\Learn\SpringBoot\spring-boot-demo\actuator\target\classes started by yuwen in D:\Code\Learn\SpringBoot\spring-boot-demo\actuator)
17:17:10.371 [restartedMain] INFO com.yuwen.spring.actuator.ActuatorApplication - No active profile set, falling back to default profiles: default
17:17:10.425 [restartedMain] INFO org.springframework.boot.devtools.env.DevToolsPropertyDefaultsPostProcessor - Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
17:17:10.425 [restartedMain] INFO org.springframework.boot.devtools.env.DevToolsPropertyDefaultsPostProcessor - For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
17:17:11.107 [restartedMain] INFO org.springframework.boot.web.embedded.tomcat.TomcatWebServer - Tomcat initialized with port(s): 8011 (http)
17:17:11.118 [restartedMain] INFO org.apache.coyote.http11.Http11NioProtocol - Initializing ProtocolHandler ["http-nio-8011"]
17:17:11.118 [restartedMain] INFO org.apache.catalina.core.StandardService - Starting service [Tomcat]
17:17:11.118 [restartedMain] INFO org.apache.catalina.core.StandardEngine - Starting Servlet engine: [Apache Tomcat/9.0.36]
17:17:11.164 [restartedMain] INFO org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/] - Initializing Spring embedded WebApplicationContext
17:17:11.164 [restartedMain] INFO org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext - Root WebApplicationContext: initialization completed in 738 ms
17:17:11.326 [restartedMain] INFO org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor - Initializing ExecutorService 'applicationTaskExecutor'
17:17:11.497 [restartedMain] INFO org.springframework.boot.devtools.autoconfigure.OptionalLiveReloadServer - LiveReload server is running on port 35729
17:17:11.500 [restartedMain] INFO org.springframework.boot.actuate.endpoint.web.EndpointLinksResolver - Exposing 2 endpoint(s) beneath base path '/actuator'
17:17:11.513 [restartedMain] INFO org.apache.coyote.http11.Http11NioProtocol - Starting ProtocolHandler ["http-nio-8011"]
17:17:11.526 [restartedMain] INFO org.springframework.boot.web.embedded.tomcat.TomcatWebServer - Tomcat started on port(s): 8011 (http) with context path ''
17:17:11.566 [restartedMain] INFO com.yuwen.spring.actuator.ActuatorApplication - Started ActuatorApplication in 1.414 seconds (JVM running for 2.41)
17:17:12.939 [RMI TCP Connection(9)-127.0.0.1] INFO org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/] - Initializing Spring DispatcherServlet 'dispatcherServlet'
17:17:12.939 [RMI TCP Connection(9)-127.0.0.1] INFO org.springframework.web.servlet.DispatcherServlet - Initializing Servlet 'dispatcherServlet'
17:17:12.942 [RMI TCP Connection(9)-127.0.0.1] INFO org.springframework.web.servlet.DispatcherServlet - Completed initialization in 3 ms

请注意如下日志:

Exposing 2 endpoint(s) beneath base path '/actuator'

说明在基本路径'/actuator'下暴露2个端点。

4.通过日志查看暴露的端点

上面的日志只说明了暴露了2个端点,
具体对外暴露的端点可以通过配置日志查看。

修改log4j2.xml日志级别为Trace:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="TRACE">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>

重启服务可以看到如下日志:

17:20:34.665 [restartedMain] INFO  org.springframework.boot.actuate.endpoint.web.EndpointLinksResolver - Exposing 2 endpoint(s) beneath base path '/actuator'
17:20:34.666 [restartedMain] TRACE org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'webEndpointServletHandlerMapping' to allow for resolving potential circular references
17:20:34.666 [restartedMain] TRACE org.springframework.beans.factory.support.DefaultListableBeanFactory - Invoking afterPropertiesSet() on bean with name 'webEndpointServletHandlerMapping'
17:20:34.666 [restartedMain] TRACE org.springframework.boot.actuate.endpoint.web.servlet.WebMvcEndpointHandlerMapping - Register "{GET /actuator/health, produces [application/vnd.spring-boot.actuator.v3+json || application/vnd.spring-boot.actuator.v2+json || application/json]}" to java.lang.Object org.springframework.boot.actuate.endpoint.web.servlet.AbstractWebMvcEndpointHandlerMapping$OperationHandler.handle(javax.servlet.http.HttpServletRequest,java.util.Map<java.lang.String, java.lang.String>)
17:20:34.666 [restartedMain] TRACE org.springframework.boot.actuate.endpoint.web.servlet.WebMvcEndpointHandlerMapping - Register "{GET /actuator/health/**, produces [application/vnd.spring-boot.actuator.v3+json || application/vnd.spring-boot.actuator.v2+json || application/json]}" to java.lang.Object org.springframework.boot.actuate.endpoint.web.servlet.AbstractWebMvcEndpointHandlerMapping$OperationHandler.handle(javax.servlet.http.HttpServletRequest,java.util.Map<java.lang.String, java.lang.String>)
17:20:34.666 [restartedMain] TRACE org.springframework.boot.actuate.endpoint.web.servlet.WebMvcEndpointHandlerMapping - Register "{GET /actuator/info, produces [application/vnd.spring-boot.actuator.v3+json || application/vnd.spring-boot.actuator.v2+json || application/json]}" to java.lang.Object org.springframework.boot.actuate.endpoint.web.servlet.AbstractWebMvcEndpointHandlerMapping$OperationHandler.handle(javax.servlet.http.HttpServletRequest,java.util.Map<java.lang.String, java.lang.String>)
17:20:34.667 [restartedMain] TRACE org.springframework.boot.actuate.endpoint.web.servlet.WebMvcEndpointHandlerMapping - Register "{GET /actuator, produces [application/vnd.spring-boot.actuator.v3+json || application/vnd.spring-boot.actuator.v2+json || application/json]}" to public java.util.Map<java.lang.String, java.util.Map<java.lang.String, org.springframework.boot.actuate.endpoint.web.Link>> org.springframework.boot.actuate.endpoint.web.servlet.WebMvcEndpointHandlerMapping$WebMvcLinksHandler.links(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)

可以看到有4个URL服务端点:

GET /actuator/health
GET /actuator/health/**
GET /actuator/info
GET /actuator

其中一个是/actuator自己,
/actuator下面有3个URL,
但是/health和/health/**其实是1个端点,
所以日志中说是在基本路径下暴露2个端点。

5.通过HTTP查看暴露的端点

浏览器访问监控管理提供的基本路径:

http://localhost:8011/actuator

同样可以看到暴露出来的所有端点:

{
"_links": {
"self": {
"href": "http://localhost:8011/actuator",
"templated": false
},
"health": {
"href": "http://localhost:8011/actuator/health",
"templated": false
},
"health-path": {
"href": "http://localhost:8011/actuator/health/{*path}",
"templated": true
},
"info": {
"href": "http://localhost:8011/actuator/info",
"templated": false
}
}
}

6.查看health端口

访问上面查询出的health端口:

http://localhost:8011/actuator/health

返回结果:

{
"status": "UP"
}

代表监控的微服务状态是正常运行的。

7.查看info端口

访问上面查询出的info端口:

http://localhost:8011/actuator/info

返回结果:

{}

返回结果为空,
因为还没有在application.yml中设置参数。

8.新增info参数

在application.yml中新增info参数:

info:
description: spring boot actuator

在info下新增description参数,
重启服务后访问info端口:

http://localhost:8011/actuator/info

返回结果:

{
"description": "spring boot actuator"
}

可以看到新增的参数能够查询出来了,
实际上在info下可以新增任意参数,
都能够通过info端点查询出来。

9.参考文档

Spring Boot Actuator: Production-ready Features

SpringBoot集成Actuator监控管理的更多相关文章

  1. springboot集成Actuator

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

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

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

  3. SpringBoot系列: Actuator监控

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

  4. SpringBoot集成Actuator端点配置

    1.说明 Actuator端点可以监控应用程序并与之交互. Spring Boot包括许多内置的端点, 比如health端点提供基本的应用程序运行状况信息, 并允许添加自定义端点. 可以控制每个单独的 ...

  5. SpringBoot集成Actuator健康指示器health

    1.说明 本文详细介绍Actuator提供的HealthIndicators, 即健康指示器的配置使用, 利用自动配置的健康指标, 检查正在运行的应用程序的状态, 以及自定义健康指标的方法. 监控软件 ...

  6. SpringBoot集成Swagger接口管理工具

    手写Api文档的几个痛点: 文档需要更新的时候,需要再次发送一份给前端,也就是文档更新交流不及时. 接口返回结果不明确 不能直接在线测试接口,通常需要使用工具,比如postman 接口文档太多,不好管 ...

  7. SpringBoot要点之使用Actuator监控

    Actuator是Springboot提供的用来对应用系统进行自省和监控的功能模块,借助于Actuator开发者可以很方便地对应用系统某些监控指标进行查看.统计等. 在pom文件中加入spring-b ...

  8. java框架之SpringBoot(17)-监控管理

    介绍 SpringBoot 提供了监控管理功能的场景启动器,它可以为我们提供准生产环境下的应用监控和管理功能.我们可以通过HTTP.JMX.SSH协议来进行操作,自动得到审计.健康及指标信息等. 使用 ...

  9. Spring Boot整合actuator实现监控管理

    Spring Boot使用actuator监控管理 1.在pom文件中导入相关的依赖 <dependency> <groupId>org.springframework.boo ...

随机推荐

  1. linux之sar命令详解

    sar(System Activity Reporter系统活动情况报告)是目前Linux上最为全面的系统性能分析工具之一,可以从多个方面对系统的活动进行报告,包括:文件的读写情况.系统调用的使用情况 ...

  2. JVM——垃圾收集算法及垃圾回收器

    一.垃圾回收算法 1.标记-清除算法 1)工作流程 算法分为"标记"和"清除"阶段:首先标记出所有需要回收的对象(标记阶段),在标记完成后统一回收所有被标记的对 ...

  3. JDK1.8新特性(一): 接口的默认方法default

    前言 今天在学习mysql分区优化时,发现一个博客专家大神,对其发布的文章简单学习一下: 一:简介 我们通常所说的接口的作用是用于定义一套标准.约束.规范等,接口中的方法只声明方法的签名,不提供相应的 ...

  4. 【Linux】【Services】任务计划、周期性任务执行

    Linux任务计划.周期性任务执行       未来的某时间点执行一次某任务:at, batch     周期性运行某任务:crontab         执行结果:会通过邮件发送给用户        ...

  5. redis实例cpu占用率过高问题优化

    目录 一.简介 一.简介 前情提要: 最近接了大数据项目的postgresql运维,刚接过来他们的报表系统就出现高峰期访问不了的问题,报表涉及实时数据和离线数据,离线读pg,实时读redis.然后自然 ...

  6. 从离线分析建模到稳健风控升级,为什么说顶象Dinsight实时风控引擎是对的选择?

    随着金融业数字化程度进一步加深,互联网垂直电商.消费金融等领域与人们生活的深度融合,数字科技在安全风险控制上已经成为了重要的基石.如何主动防范化解风险,建立智能化的实时风险监测预警体系,加速业务模式转 ...

  7. 【WP】攻防世界-杂项-Misc

    长期更新一波 攻防世界 的杂项题解 这东西主要靠积累吧 攻防世界:https://adworld.xctf.org.cn 因为攻防世界的题目顺序经常变化,我也不改序号了,顺着之前写的位置往下写,推荐使 ...

  8. 选择…Select…(Power Query 之 M 语言)

    选择行: 筛选Table.SelectRows-文本与数值 筛选Table.SelectRows-日期与时间 保留错误行:= Table.SelectRowsWithErrors( 表, {" ...

  9. 可视报表(Project)

    <Project2016 企业项目管理实践>张会斌 董方好 编著 原来一直有一个执念,做项目的人电脑上一定要安装Project软件,今天突然知道了,原来领导的电脑上,可以不装,因为,Pro ...

  10. CF1036D Vasya and Arrays 题解

    Content 给定两个长度分别为 \(n\) 和 \(m\) 的数列 \(A,B\).你需要将两个数列都恰好分成 \(k\) 份,使得两个数列中第 \(i(i\in[1,k])\) 份的元素和对应相 ...