使用SpringBoot Actuator监控应用

Actuator是Spring Boot提供的对应用系统的自省和监控的集成功能,能够对应用系统进行配置查看、相关功能统计等。html

使用Actuator

引入依赖便可spring

  • Maven
<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
复制代码
  • Gradle
compile('org.springframework.boot:spring-boot-starter-actuator')
复制代码

Endpoints

列举一些主要的endpoints 安全

endpoints.png

配置文件属性介绍

地址和端口的配置

  • management.port:指定访问这些监控方法的端口,与逻辑接口端口分离。若是不想将这些暴露在http中,能够设置 management.port = -1
  • management.address:指定地址,好比只能经过本机监控,能够设置 management.address = 127.0.0.1

敏感信息访问限制

根据上面表格,鉴权为false的,表示不敏感,能够随意访问,不然就是作了一些保护,不能随意访问。bash

endpoints.mappings.sensitive=falseapp

这样须要对每个都设置,比较麻烦。敏感方法默认是须要用户拥有ACTUATOR角色,所以,也能够设置关闭安全限制:maven

management.security.enabled=falsespring-boot

或者配合Spring Security作细粒度控制。post

自定义系统信息

能够经过访问/info获取信息,须要在配置文件设置ui

info:
  aaa:
    name: xxx
    email: xxx@qq.com
  bbb:
    age: 25
    hobbies: running
  build:
    artifact: "@project.artifactId@"
    name: "@project.name@"
    version: "@project.version@"
复制代码

此时访问localhost:8080/info返回一下信息 spa

这里写图片描述

若是使用maven,能够访问pom.xml文件的信息,用法以下:

// 获取pom.xml中project节点下artifactId属性 artifact: "@project.artifactId@"

其余

/shutdown这个须要post方式,经过请求来关闭应用。 这个操做比较敏感,要想真正生效,须要如下配置:

endpoints.shutdown.enabled: true

  • 咱们能够经过实现HealthIndicator接口,编写本身的/health方法逻辑。也能够增长自定义监控方法。
  • 查看详细介绍,请移步 官方文档
相关文章
相关标签/搜索