一、简介

1.1、利用Spring Boot的特性进行监控应用的方式

通过HTTP(最简单方便)

通过JMX

通过远程shell

1.2、端点(通过执行器端点可以监控应用及与应用进行交互)

1.端点暴露的方式取决于你采用的监控方式。如果使用HTTP监控,端点的ID映射到一个URL。例如,默认情况下,health端点将被映射到/health。

2.端点会默认有敏感度,根据不同的敏感度是否需要提供用户密码认证

3.如果没启用web安全,则敏感度高的会禁用

4.可以通过配置文件进行配置敏感度

5.默认情况下,除了shutdown外的所有端点都是启用的。


这里我们介绍的是通过HTTP的方式来监控

二、配置操作

2.1、添加依赖

<!--基于HTTP监控-开始-->
<!-- actuator -->
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- security -->
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!--基于HTTP监控-结束-->

到这里为止我们就可以看到一点效果了,重启服务,我们可以看到如下信息:

 wKioL1lIuofjF-cEAAKzydYZW4E006.png

2.2、配置

#端点的配置
endpoints.sensitive=true
endpoints.shutdown.enabled=true

#保护端点
security.basic.enabled=true
security.user.name=liuy
security.user.password=123456
management.security.roles=SUPERUSER

#自定义路径
security.basic.path=/manage
management.context-path=/manage

测试:访问http://localhost:9090/manage/metrics 

 wKioL1lIwWagMWojAABhZmemhe8005.png

输入上面配置的账号、密码:liuy/123456

 wKiom1lIwcrQHY7NAAB2ndpeP_g389.png

三、重点说明

度量: http://localhost:9090/manage/metrics 

wKioL1lIx3PTt-a8AAAQ5Ds7eHY640.jpg

追踪: http://localhost:9090/manage/trace 

wKiom1lIx9qhoWMKAAC2CizgJeA510.jpg

四、常用端点及描述

autoconfig:显示自动配置的信息,get请求。

beans:显示应用程序上下文所有的Springbean,get请求。

configpros:显示所有@ConfigurationProperties的配置属性列表,get请求。

dump:显示线程活动的快照,get请求。

env:显示应用的环境变量,get请求。

health:显示应用程序的健康指标,这些值由HealthIndicator的实现类提供,get请求。

info:显示应用的信息,可使用info.*属性自定义info端点公开的数据,get请求。

mappings:显示所有的URL路径,get请求。

metrics:显示应用的度量标准信息,get请求。

shutdown:关闭应用(默认情况下不启动,如需启用,需设置endpoints.shutdown.enabled=true),post请求。

trace:显示跟踪信息(默认情况下为最近100个http请求),get请求。


wKioL1mmKWrjgiPfAADnd4qKdL4740.jpg


info例子,如:

application.properties配置:

#自定义info端点公开的数据
info.app.name=@project.artifactId@
info.app.encoding=@project.build.sourceEncoding@
info.app.java.source=@java.version@
info.app.java.targer=@java.version@

效果:

 wKiom1ljGpujn5O4AAAXupGN3PM116.jpg

五、异常问题

访问端点时,出现401,如:

 wKioL1mmKNGxEzwJAABa_8YW-1Q313.jpg

在配置文件中加入如下配置即可:

management:
  security:
    enabled: false