Spring Boot Actuator微服务服务监控

1、Spring Boot Actuator介绍

     Spring Boot Actuator是Spring Boot中一个比较强大的特性功能,能够帮助你监控和管理你的应用程序,通过提供的restful api接口来监管、审计、收集应用的运行情况,针对微服务而言它是必不可少的一个组件。

有如下特性:

  • Endpoints

  Spring Boot Actuator的核心部分,它用来监视应用程序及交互,spring-boot-actuator中已经内置了非常多的Endpoints(health、info、beans、httptrace、shutdown)等等,同时也允许我们自己扩展自己的端点。例如:health端点提供了应用程序的基本健康信息。

    使用了actuator后,并不代表所有内置的端点都能够访问,启用了并不代表可以直接访问,还需要配置参数(management.endpoints.web.exposure.include)将其暴露出来。

内置Endpoints

id 描述  
auditevents 显示当前应用程序的审计事件信息  
beans 显示应用Spring Beans的完整列表  
caches 显示可用缓存信息  
conditions 显示自动装配类的状态及及应用信息  
configprops 显示所有 @ConfigurationProperties 列表  
env 显示 ConfigurableEnvironment 中的属性  
flyway 显示 Flyway 数据库迁移信息  
health 显示应用的健康信息(未认证只显示status,认证显示全部信息详情)  
info 显示任意的应用信息(在资源文件写info.xxx即可)  
liquibase 展示Liquibase 数据库迁移  
metrics 展示当前应用的 metrics 信息  
mappings 显示所有 @RequestMapping 路径集列表  
scheduledtasks 显示应用程序中的计划任务  
sessions 允许从Spring会话支持的会话存储中检索和删除用户会话。  
shutdown 允许应用以优雅的方式关闭(默认情况下不启用)  
threaddump 执行一个线程dump  
httptrace 显示HTTP跟踪信息(默认显示最后100个HTTP请求 - 响应交换)  
  • Metrics 

    Spring Boot Actuator通过集成Micrometer来提供一些监控指标标准,详细可参考:https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-metrics.html

  • Audit 

     Spring Boot Actuator有一个比较灵活的审计框架,那就是Audit,可以将事件发布到AuditEventRepository。一旦Spring Security正在运行,它会默认自动发布身份验证事件。这对于认证审计非常有用,并且还可以基于身份验证失败实施锁定策略,或自定义扩展一些审计功能,你可以实现已提供的接口AbstractAuthenticationAuditListener 和AbstractAuthorizationAuditListener。

 

     看完上面这些特性,是不是有一种马上上手尝试一下的冲动了吧。Spring Boot Actuator将会为你的服务监控和管理提供很多的便利,不必闭门造车了。

2、开发配置

  •    导入依赖包

   在pom.xml中添加spring-boot-starter-actuator的依赖。


    org.springframework.boot
    spring-boot-starter-actuator
  • 属性配置

      在application.yml文件中配置actuator的相关配置,其中info开头的属性,就是访问info端点中显示的相关内容,值得注意的十spring boot2.x中,默认只开放了infohealth两个端点,其余的需要自己通过配置management.endpoints.web.exposure.include属性来加载(有include自然就有exclude)。如果想单独操作某个端点可以使用management.endpoint.端点.enabled属性进行启用或者禁用。

info:
  head: head
  body: body
management:
  endpoints:
    web:
      exposure:
        #加载所有的端点,默认只加载了info、health
        include: '*'
  endpoint:
    health:
      show-details: always
    #可以关闭指定的端点
    shutdown:
      enabled: false

3、总结

     Spring Boot Actuator组件作为微服务中服务监控的核心,Spring Boot Admin也是基于这个Actuator开发的,所以当涉及到服务监控相关的,可以优先考虑从Actuator入手。

参考文档:

https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-metrics.html

https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-auditing.html

你可能感兴趣的:(SpringBoot)