springboot-actuator
简介:
SpringBoot提供了一些额外的特性,帮助我们管理和监视应用的运行情况!我们可以使用Http EndPoint、JMX来管理和监视应用的 运行情况!
SpringBoot-actuator这个模块,提供了一个SpringBoot应用的所有运行信息,我们可以通过这个模块来查看所有应用信息!
其启动器如下:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
|
Endpoints :端点
actuator中提供了大量的构建好的EndPoint,让开发者对用用进行监视!
以上这些便是,提供好的endpoint,我们可以把endpoint看作是一个控制器入口!访问以便可以获得对应的返回。我们访问一下!
拿到所有定时任务信息!
拿到所有配置文件的属性
如果是web应用的话,还有以下节点可供使用:
Enabling EndPoint:增加端点
通常情况下,用来关停应用的shutdown断点是没被加载的,我们需要设置一下才可以让容器加载这个端点
Exposing Endpoint:暴露断点
你以为让Spring记载这些断点就完事了么?actuator作者认为这些应用信息太敏感,所以在2.x版本中,web应用下
默认只暴露两个断点,如下图所示
默认只暴漏health和info两个节点。我们测试一下,下面的配置使用缺省配置
启动时的日志信息如下,只暴露了health和info两个endpoint和上上图一致
大家也看到了!!让断点 enabled:true 只是一步,想要访问,还需要进行断点暴露!这也是1.x和2.x的区别
1.x时,默认被加载的都会被暴露出去!
使用如下配置进行断点的屏蔽或者暴露:
如下图所示,我们选择暴露所有节点
启动项目
Configuring EndPoint :对断点进行配置
有时候我们想偷偷改一下断点的默认路径什么的咋整?
management.endpoints.web.base-path=/ 默认basepath为 /actuator 可使用左边配置进行更改
management.endpoints.web.path-mapping.health=healthcheck
我们把shutdown的路径更换为godie 把basepath更换为/lc
从启动日志可以看到更换成功!
CORS Support:跨域支持
可以通过简单的设置,让endpoint的访问支持跨域
management.endpoints.web.cors.allowed-origins=http://example.com 可允许域名
management.endpoints.web.cors.allowed-methods=GET,POST 方法
实现自己的EndPoint:
如果想关闭endpoints则咋整?
设置 management.server.port=-1 即可
如何填充返回信息?
能干吗?
actuator的应用实例便是SpringAdmin,我们可以学习这篇笔记!boot-admin它对actuator进行了数据的可视化!但是好像没有分析功能,如果加上 jvm的分析功能,就完美了! 认真学习了SpringBoot-actuator后,对SpringBoot admin也是突然豁然开朗!