0
点赞
收藏
分享

微信扫一扫

SpringBoot第三特性:Actuator监控



文章目录


  • ​​一、前言​​
  • ​​二、Endpoints​​

  • ​​2.1 health健康信息​​
  • ​​2.2 Actuator各种各样的endpoint​​
  • ​​2.3 从属性到操作,开启shutdown操作​​
  • ​​2.4 连接redis​​
  • ​​2.5 dubbo​​

  • ​​三、Metrics 检测系统当前运行指标​​
  • ​​四、Loggers​​

  • ​​4.1 查看日志级别​​
  • ​​4.2 修改日志级别​​
  • ​​附:info​​

  • ​​五、自定义actuctor端点​​

  • ​​5.1 @Endpoint方式(和上面的dubbo那个一样)​​
  • ​​5.2 继承AbstractHealthIndicator类方式(和redis一样)​​

  • ​​六、Actuator两种监控方式​​

  • ​​6.1 Actuator两种监控方式​​
  • ​​6.2 jmx​​
  • ​​6.3 查看jmx中的内容​​
  • ​​6.4 写入内容给jmx查看​​
  • ​​6.5 从模拟到实际 从MBean到springboot​​
  • ​​6.6 从实际到模拟,自己实现一个bean Register,使用spring启动代替main测试类启动​​

  • ​​七、小结​​


一、前言

Springboot Actuator是它的一种重要特性,用来访问health信息,metrics信息。本篇博客不涉及Actuator的底层原理,仅涉及如何使用Actuator用于帮助开发实践

对照springboot actuator的官方文档

SpringBoot第三特性:Actuator监控_spring

本文分别阐述 Endpoints Metrics Loggers 两种监控方式(http和jmx)。


官方文档地址
​​ https://docs.spring.io/spring/docs/​​​​ https://docs.spring.io/spring/docs/5.2.7.RELEASE/spring-framework-reference/​​​​ https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-features.html#production-ready-health​​


二、Endpoints

2.1 health健康信息

新建一个springboot项目,并导入依赖spring-boot-starter-actuator

SpringBoot第三特性:Actuator监控_spring_02

SpringBoot第三特性:Actuator监控_监控方式_03


金手指:
management.endpoint.health.show-details=always 显示/actuator/health 详细信息


运行,

SpringBoot第三特性:Actuator监控_redis_04

2.2 Actuator各种各样的endpoint

spring cloud 有很多endpoints,其中,只有healths结点和info结点是默认开放的,可访问的,其他的借点不可访问,需要在application.properties中配置​​management.endpoints.web.exposure.include=*​​,才能开放所有的结点,当然,笔者这里使用阿里的springboot骨架,所以默认有这句了。

SpringBoot第三特性:Actuator监控_监控方式_05

查看所有的beans

SpringBoot第三特性:Actuator监控_监控方式_06

SpringBoot第三特性:Actuator监控_redis_07


金手指:使用jsonview,让chrome查看json数据格式化
github下载地址:https://github.com/gildas-lormeau/JSONView-for-Chrome
chrome://extensions/ 添加插件即可
注意,一定要选中WebContent这个目录添加才能成功,其父目录和子目录都不成功


SpringBoot第三特性:Actuator监控_spring_08

SpringBoot第三特性:Actuator监控_监控方式_09

2.3 从属性到操作,开启shutdown操作

未开启shutdown操作之前,为404

SpringBoot第三特性:Actuator监控_spring_10

开启shutdown

SpringBoot第三特性:Actuator监控_监控方式_11

SpringBoot第三特性:Actuator监控_监控方式_12


金手指:浏览器上只能测试get请求,idea上可以测试get post请求,最好测试还是postman


SpringBoot第三特性:Actuator监控_监控方式_13

SpringBoot第三特性:Actuator监控_spring_14

SpringBoot第三特性:Actuator监控_redis_15

还是不行,使用postman测试

SpringBoot第三特性:Actuator监控_spring_16

2.4 连接redis

application.properties中添加

SpringBoot第三特性:Actuator监控_redis_17

导入依赖

SpringBoot第三特性:Actuator监控_spring_18

依赖添加成功

SpringBoot第三特性:Actuator监控_spring_19

启动本机redis

SpringBoot第三特性:Actuator监控_spring_20

再次查看浏览器 localhost:8081/actuator/health 不仅显示spring信息,也显示redis信息


金手指:
springboot默认内置了很多第三方组件的监控,所有这里不仅显示springboot的健康信息,也显示redis信息


SpringBoot第三特性:Actuator监控_监控方式_21

要想检测健康信息,就要继承 AbstractHealthIndictor类,这里RedisHealthIndicator继承于AbstractHealthIndictor并实现doHealthCheck方法

SpringBoot第三特性:Actuator监控_redis_22

2.5 dubbo

SpringBoot第三特性:Actuator监控_redis_23

dubbo-spring-boot-actuator

dubbo-spring-boot-autoconfigure

dubbo-spring-boot-parent

dubbo-spring-boot-samples

dubbo-spring-boot-starter

SpringBoot第三特性:Actuator监控_监控方式_24

SpringBoot第三特性:Actuator监控_spring_25


dubbo作为第三放组件,可以使用Springboot的@Endpoint


三、Metrics 检测系统当前运行指标

提供当前应用上的指标,类似汽车的仪表盘,显示当前汽车信息。

没有监控的应用,很难高可用和风险规避。

SpringBoot第三特性:Actuator监控_redis_26

SpringBoot第三特性:Actuator监控_spring_27

要看一个name 的值

SpringBoot第三特性:Actuator监控_redis_28

metrics 包含多个维度的内容,如下:

JVM(垃圾收集器、内存、堆)

系统(运行时间、平均负载、处理器的信息)

线程池信息

tomcat会话信息

四、Loggers

4.1 查看日志级别

SpringBoot第三特性:Actuator监控_spring_29

SpringBoot第三特性:Actuator监控_redis_30

4.2 修改日志级别

如果线上排查问题,想要看到日志详细信息时,可以修改某个包路径下日志级别,如:

ROOT下日志级别为INFO,修改为DEBUG

SpringBoot第三特性:Actuator监控_监控方式_31

SpringBoot第三特性:Actuator监控_redis_32

SpringBoot第三特性:Actuator监控_spring_33

设定为post请求,请求url就是刚才浏览器上查看的url,body raw json类型参数,键值对为修改参数configuredLevel(注意不是effectiveLevel),修改为DEBUG,最后send

SpringBoot第三特性:Actuator监控_监控方式_34

附:info

SpringBoot第三特性:Actuator监控_spring_35

SpringBoot第三特性:Actuator监控_redis_36

SpringBoot第三特性:Actuator监控_redis_37


不知道为什么失败了,以后再看


五、自定义actuctor端点

5.1 @Endpoint方式(和上面的dubbo那个一样)

新建一个类,使用@Endpoint注解,这样它就变成一个actuator端点

SpringBoot第三特性:Actuator监控_监控方式_38

@Configuration标注一个类变为配置类,然后将刚刚新建的类变为一个bean

SpringBoot第三特性:Actuator监控_监控方式_39

SpringBoot第三特性:Actuator监控_spring_40


@Endpoint需要一个配置类,不要少了配置类,注意


5.2 继承AbstractHealthIndicator类方式(和redis一样)

略,以后补充。

六、Actuator两种监控方式

6.1 Actuator两种监控方式

SpringBoot第三特性:Actuator监控_监控方式_41

Actuator两种形态的监控:http(web)和jmx,上面的都是web方法,都是在浏览器上测试,现在看jmx方式。

6.2 jmx

jmx 英文全称 java management extension,即java管理扩展,包括远程监控和本机监控

management.endpoints.jmx.exposure.include=*
management.endpoints.web.exposure.include=*

SpringBoot第三特性:Actuator监控_spring_42


第一句,打开jmx包含的所有端点;
第二句,要将spring的jmx展示。



jmx和web都是展示Actuator信息,只是展示形式不一样,web一般用浏览器就可以看,jmx使用jdk中的jconsole查看。


6.3 查看jmx中的内容

SpringBoot第三特性:Actuator监控_redis_43

SpringBoot第三特性:Actuator监控_spring_44

SpringBoot第三特性:Actuator监控_spring_45

SpringBoot第三特性:Actuator监控_监控方式_46

6.4 写入内容给jmx查看

新建一个接口 后缀为MBean,新建一个类,实现接口,类名不需要MBean,其他和接口名相同

SpringBoot第三特性:Actuator监控_监控方式_47

SpringBoot第三特性:Actuator监控_spring_48

然后,使用main方法启动,将信息暴露出去给小程序收集,小程序收集后上报

SpringBoot第三特性:Actuator监控_spring_49

运行成功(注意:bean包括两种,属性和操作)

SpringBoot第三特性:Actuator监控_redis_50


JMX的意义,相对于http的好处 目的:监控的作用在于可以追溯到某一时刻的状态 JMX:一个工具jconsole
http和jmx都是方式,两种方式
总之,web浏览器中看到的jmx应用程序都可以看到,这是两种不同方式,http和jmx
比如:
SpringBoot第三特性:Actuator监控_redis_51


6.5 从模拟到实际 从MBean到springboot

我们刚刚写了一个后缀为MBean的接口,并且实现它,然后在Main中发布,实际上,在springboot中也是这样的,让我们来看一看。

SpringBoot第三特性:Actuator监控_redis_52

SpringBoot第三特性:Actuator监控_监控方式_53

SpringBoot第三特性:Actuator监控_监控方式_54


这个SpringApplicationAdminMXBeanRegistrar实现类这么多接口,一定在IOC容器中。



金手指:使用约定的好处,看代码很简单了
SpringApplicationAdminMXBean找到SpringApplicationAdminJmxAutoConfiguration


SpringBoot第三特性:Actuator监控_spring_55

SpringApplicationAdminJmxAutoConfiguration中定位“包名+类名”,和Main类中一样的

SpringBoot第三特性:Actuator监控_spring_56

注册类

SpringBoot第三特性:Actuator监控_监控方式_57

SpringBoot第三特性:Actuator监控_spring_58

SpringBoot第三特性:Actuator监控_spring_59

SpringBoot第三特性:Actuator监控_redis_60

springboot 关于jmx 的实现完成了,接口+实现类+配置类

6.6 从实际到模拟,自己实现一个bean Register,使用spring启动代替main测试类启动

注册类

SpringBoot第三特性:Actuator监控_redis_61

SpringBoot第三特性:Actuator监控_redis_62

注册类写好了,运行并查看jconsole

SpringBoot第三特性:Actuator监控_监控方式_63

七、小结

本文介绍Springboot第三特性:Actuator监控,分别是:Endpoints Metrics Loggers 自定义端点 两种监控方式。

天天打码,天天进步!!!




举报

相关推荐

0 条评论