使用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方法邏輯。也能夠增長自定義監控方法。
  • 查看詳細介紹,請移步 官方文檔
相關文章
相關標籤/搜索