SpringBoot高级——监控管理

监控管理

 通过引入spring-boot-starter-actuator,可以使用Spring Boot为我们提供的准生产环境下的应用监控和管理功能。我们可以通过HTTP,JMX,SSH协议来进行操作,自动得到审计、健康及指标信息等

使用方法

 1、引入依赖

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

  引入actuator依赖后,在启动应用时通过日志可以看到多了很多请求映射:这些请求是由actuator模块处理的:

{[/heapdump || /heapdump.json],methods=[GET],produces=[application/octet-stream]}
{[/configprops || /configprops.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}
{[/beans || /beans.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}
{[/loggers || /loggers.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}
{[/info || /info.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}
{[/trace || /trace.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}
{[/health || /health.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}
{[/auditevents || /auditevents.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}
{[/env || /env.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}
{[/mappings || /mappings.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}
{[/dump || /dump.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}
{[/metrics/{name:.*}],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}
{[/metrics || /metrics.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}
{[/autoconfig || /autoconfig.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}

 2、开启监控管理功能,在主配置文件中加入如下配置:因为默认监控管理请求是受保护的,不允许随意请求,设置为false之后,再请求就可看到结果了

#默认是受保护的
management.security.enabled=false

 3、请求监控的内容
  /heapdump:下载内存快照
在这里插入图片描述
  /configprops:监控管理功能的属性配置,通过这个可以看到哪些监控管理功能的请求是放开的,哪些是关闭的,以及这些功能的开启和关闭属性值设置
在这里插入图片描述
   示例:关闭/metrics请求

endpoints.metrics.enabled=false

   关闭后请求/metrics就会报错:
在这里插入图片描述
  /beans:容器中有哪些bean,以及这些bean的类型,依赖情况等
在这里插入图片描述
  /loggers:日志配置信息
在这里插入图片描述
  /info:应用信息或者在GitHub上展示Git信息等
   在主配置文件中配置:

info.app.id=abc
info.app.name=test
info.lastName=xiaoqi

   展示结果:
在这里插入图片描述
   展示Git信息,新建git.properties配置文件,并进行如下配置:因为GitProperties是继承于InfoProperties的

git.branch=master
git.commit.id=xjkd33s
git.commit.time=2017-12-12 12:12:56

   展示结果:
在这里插入图片描述
  /trace:该应用处理的所有请求信息,包括请求路径、请求方式、请求头、响应等
在这里插入图片描述
  /health:应用健康状态
在这里插入图片描述
  /auditevents:审计事件
  /env:应用的环境信息,包括端口、jdk版本、应用的位置、虚拟机的版本等
在这里插入图片描述
  /mappings:请求映射及映射处理的bean和method信息
在这里插入图片描述
  /dump:线程状态信息
在这里插入图片描述
  /metrics:应用的各项指标
在这里插入图片描述
  /autoconfig:哪些自动配置类起作用,哪些没起作用
在这里插入图片描述
  /shutdown:远程关闭应用,要先开启该功能,然后远程发送post请求/shutdown即可远程关闭应用,可使用postman等发post请求
   配置:

endpoints.shutdown.enabled=true

   发请求关闭应用:
在这里插入图片描述

定制监管

 定制端点一般通过endpoints + 端点名(即监控的请求映射名,如beans、health、autoconfig等) + 属性名 来设置:
 比如修改beans端点的id:仅修改此属性配置时,再想获取到bean的信息就要请求/mybeans

endpoints.beans.id=mybeans

 修改某个请求的请求路径,比如beans:此时只能通过访问/bean来查看bean的信息

endpoints.beans.path=/bean

 关闭端点访问:禁止获取bean的信息

endpoints.beans.enabled=false

 开启端点访问:

#开启远程应用关闭功能
endpoints.shutdown.enabled=true

 关闭所有端点:

#关闭所有端点
endpoints.enabled=false
#关闭所有端点后开启某个端点
endpoints.beans.enabled=true

 定制端点访问根路径:这样在访问所有端点请求前需要加/manage,此配置可结合SpringSecurity做安全验证等

management.context-path=/manage

 设置端点访问端口:有了此设置,访问这些端点时就需要在8181端口下,这样可以更好的保护这些端点

management.port=8181

在这里插入图片描述

定制HealthIndicator

 访问/health,默认只有diskSpace信息:
在这里插入图片描述
 在actuate包下的health下可以看到应用的哪些健康状况可以被配置和检查:
在这里插入图片描述
在这里插入图片描述
 但只有我们将这些模块的依赖引入之后才会监控到相应的健康状况信息,以redis为例:
  引入redis依赖:

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

在这里插入图片描述
 健康检查之所以生效是因为actuate为我们提供了健康指示器:即上方截图中的actuate包下的health下的类

public class RedisHealthIndicator extends AbstractHealthIndicator {
...
}

 自定健康指示器:
  ①编写一个指示器:实现 HealthIndicator 接口
  ②指示器的名字必须以HealthIndicator结尾:如XxxHealthIndicator
  ③将自定义的指示器加入容器中
  示例:

@Component
public class MyAppHealthIndicator implements HealthIndicator {

    @Override
    public Health health() {

        //自定义的检查方法
        //Health.up().build()代表健康
        return Health.down().withDetail("msg","服务异常").build();
    }
}

在这里插入图片描述

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值