使用 Spring Boot Admin 来监控你的服务

Spring boot 的出现,大大的简化了传统的软件开发流程,同时,软件的架构也在向微服务靠拢。而Spring 基于此,给我们提供了非常强大的基础设施 – Spring cloud 系列项目。本文将基于 Spring cloud 以及 Spring boot, 介绍怎样使用 Spring boot admin 来监控Spring cloud 提供的微服务基础设施。

当我们使用 Spring boot 进行开发之时, 它提供了一个非常有用的模块: spring-boot-starter-actuator , 这个模块提供了以下的 endpoint 用于监控我们的服务:

endpoint 描述 是否敏感
actuator Provides a hypermedia-based “discovery page” for the other endpoints. Requires Spring HATEOAS to be on the classpath. true
auditevents Exposes audit events information for the current application. true
autoconfig Displays an auto-configuration report showing all auto-configuration candidates and the reason why they ‘were’ or ‘were not’ applied. true
beans Displays a complete list of all the Spring beans in your application. true
configprops Displays a collated list of all @ConfigurationProperties. true
dump Performs a thread dump. true
env Exposes properties from Spring’s ConfigurableEnvironment. true
flyway Shows any Flyway database migrations that have been applied. true
health Shows application health information (when the application is secure, a simple ‘status’ when accessed over an unauthenticated connection or full message details when authenticated). false
info Displays arbitrary application info. false
loggers Shows and modifies the configuration of loggers in the application. true
liquibase Shows any Liquibase database migrations that have been applied. true
metrics Shows ‘metrics’ information for the current application. true
mappings Displays a collated list of all @RequestMapping paths. true
shutdown Allows the application to be gracefully shutdown (not enabled by default). true
trace Displays trace information (by default the last 100 HTTP requests). true

如果使用了Spring MVC, 还额外提供了以下的 endpoint:

ID Description Sensitive Default
docs Displays documentation, including example requests and responses, for the Actuator’s endpoints. Requires spring-boot-actuator-docs to be on the classpath. false
heapdump Returns a GZip compressed hprof heap dump file. true
jolokia Exposes JMX beans over HTTP (when Jolokia is on the classpath). true
logfile Returns the contents of the logfile (if logging.file or logging.path properties have been set). Supports the use of the HTTP Range header to retrieve part of the log file’s content. true

但是光有对应的 API 而没有界面, 这样也不便于我们查看,所以这里介绍一个开源项目: Spring Boot Admin ,它提供了对 Spring boot 的 endpoint UI 界面支持, 同时也提供了对 Spring cloud 的一些支持。

因为用到的配置项和涉及的模块有点多,所以在此就不具体介绍配置,只是简单介绍一下我们会使用到的几个服务,其他的配置项,可以自行查看源码

在本篇文章中,我们只需用到以下三个模块:

  • mcloud-blog 这个模块是个简单的博客系统,这里仅用作示例
  • mcloud-monitoring 最重要的就是这个模块,使用了 Spring Boot Admin 实现了整个服务的监控
  • mcloud-eureka 非常简单的一个模块,主要提供了服务注册与发现功能,用于 Spring Boot Admin 查找可用的服务

下载源码并启动上面三个模块后,访问 http://localhost:18080/monitoring/ ,就可以看到最终的效果:

qq 20171218200700

qq 20171218200720

对于 Spring Boot Admin 就简单的介绍到这,整个 MCloud 系列项目还有很多模块,后续会一一介绍。