转自:https://www.jianshu.com/p/c043d3c71f47git
这是一个研发老司机与运维同窗都会很是喜欢的东西,随着点融集团的扩张,点融网的业务愈来愈复杂、服务愈来愈多,不管是微服务仍是SOA都会大大加剧运维的负担。这时在应用层提供统1、强大的监控接口对于自动化运维来说就显得很是重要!github
对于研发来说,能够在应用启动后拿到应用的各类数据,很是便于调试应用、分析应用的运行情况,而且不须要研发去实现这些监控功能。web
对于运维来说,能够监控应用的健康信息、统计应用的瞬时信息。发现应用挂掉了、发现瞬时信息不正常均可以发送报警信息, 也能够将信息拉到监控系统的数据系统中,再展现到漂亮的UI上实时监控应用的运行状态。这些actions 无疑将会大大保证系统的总体质量。spring
而集成了actuator的springboot应用会在约定的endpoints上暴露本身应用的内部信息,又强大又统一标准,知足复杂分布式系统的监控需求,一些endpoints简介以下:api
研发会重点关注绿色的五项、而运维更关注深绿色的两项,其余项笔者暂时没有发现有特别的用处...springboot
compileapp
'org.springframework.boot:spring-boot-starter-actuator:1.3.6.RELEASE'运维
1) 每一个endpoint均可以在application.properties里面用 endpoints.[endpoint].[id|sensitive|enabled]来重定义其默认值。分布式
例如:spring-boot
这样就将autoconfig这个endpoint的path修改成了auto_cfg,本着约定优于配置原则,没有特殊需求,笔者不建议作此类修改。
2) 自定义 /health endpoint
当springboot应用跑起来以后,你能够经过访问该接口得到应用的健康状态。
例如,图片中的 “ status:"UP" ” 即代表应用的健康状态,diskSpace 当中的单位为bytes. 代表了该应用目前硬盘的健康状态:
红色箭头为我在health endpoint中自定义的 rabbitMQ healthCheckIndicator. 用于检查个人应用所依赖的rabbitMQ的健康状态。具体实现方式以下:
您须要实现HealthIndicator 接口或者继承AbstractHealthIndicator
3) 自定义 /metrics endpoint
正如一的表格当中所讲的, metrics是最重要的endpoint之一。
红色箭头所指向两个gauge指标是我fake的一个queue所对应有几个consumer, 有几条还在queue中未被消费的消息数量,具体自定义方式以下:
若是想要自定义一些metrics, 如api的访问次数、消息数量成功处理次数、消息处理失败次数则能够自定义以下,也会展现在metrics接口中:
4) 自定义 /info endpoint
在application.properties中, 添加自定义字段,以下:
访问效果:
您须要实现Endpoint 接口或者继承AbstractEndpoint。
访问效果:
还有其余的一些自定义,开发能够不用特别关心 。
添加依赖:
compile('org.springframework.boot:spring-boot-actuator'){ exclude group: 'org.springframework.boot',
module:'spring-boot-starter-logging'}
1) 使用 AnnotationConfigWebApplicationContext :
并在 Spring初始化config bean的上面添加@EnableAutoConfiguration 便可。详情见附录3。
2) 使用 XML 方式的nonboot - spring 应用
在任一Spring生命周期的Component bean上 添加@EnableAutoConfiguration 便可。
也能够@Autowire 这些Endpoint自定义访问路径。详情见附录4
附录:
集成springboot actuator到非springboot spring应用中 : https://stackoverflow.com/questions/26913087/use-spring-boot-actuator-without-a-spring-boot-application
本文所用的springboot源码: https://github.com/Agileaq/springboot-actuator
非springboot的annotationConfig spring web应用源码: https://github.com/Agileaq/nonboot-actuator-example
非springboot的xml base web应用源码: https://github.com/Agileaq/nonboot-xml-spring-actuator
English version of Spring Actuator introduction: http://www.baeldung.com/spring-boot-actuators
黑帮文章——神荼之眼监控系统(点融黑帮后台回复【神荼之眼】便可查看)
黑帮文章——高颜值绘图工具Grafana(点融黑帮后台回复【Grafana】便可查看)