spring-boot-actuator 服務監控 健康信息 應用信息

開啓actuator

pom文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.actuator</groupId>
    <artifactId>actuator</artifactId>
    <version>1.0.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--開啓spring-boot-actuator -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>
</project>

程序入口

package com.actuator;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ActuatorApplication {
    public static void main(String[] args) {
        SpringApplication.run(ActuatorApplication.class,args);
    }
}

application

server:
  port: 8080
spring:
  application:
    name: actuator

# spring boot actuator configuration
management:
  endpoints:
    web:
      exposure:
        include: "*" # 配置監控點 * 所有
        base-path: /actuator #配置訪問的基礎路徑
  endpoint:
    health:
      show-details: always
    shutdown:
      enabled: true # 開啓shutdown端點,允許actuator關閉程序

info:
  app:
    name: actuator
    port: 8080
    version: 1.0.0
    author: kino

運行結果

通過postman去訪問相應的接口,不要通過瀏覽器地址欄
在這裏插入圖片描述

總結

通過上面搭建的案例我們可以知道spring-boot-actuator是可以訪問指定路徑來獲取應用程序的相關信息的。比如項目自身的健康信息,JVM信息等。

獲取信息的請求路徑彙總

請求路徑彙總

說明:
A:在 請求路徑(默認) 這欄中,去除 /actuator/ 剩下的部分可以被稱爲 端點(endpoint) ,下面會有相關介紹。
B:actuator中的默認請求路徑會帶有 /actuator 前綴,例如http://ip:port/actuator/endpoint(端點)。
C:修改默認訪問路徑的方式 management.endpoints.web.base-path=/{自定義url}

默認請求方式 請求路徑(默認) 獲取信息 學習時的訪問工具
GET /actuator/info 獲取配置文件(yml/application)中的項目描述信息,以info.app這樣開頭的信息 postma
GET /actuator/health 獲取應用的運行狀態 postma
GET /actuator/beans 獲取應用中所有bean對象的相關信息 postma
GET /actuator/conditions 顯示配置類和自動配置類 postma
GET /actuator/heapdump 獲取JVM堆轉儲文件HeapDump 瀏覽器訪問,下載文件,使用visualvm打開
GET /actuator/mappings 獲取URL與接口的映射關係 postman
GET /actuator/threaddump 獲取線程的相關信息 postman
POST /actuator/shutdown 訪問該路徑,可以直接關閉程序 postman
GET /actuator/configprops 獲取@ConfigurationProperties的集合列表 postman
GET /actuator/env 顯示@ConfigurableEnvironment的屬性 postman
GET /actuator/scheduledtasks 獲取應用程序的計劃任務 postman
GET /actuator/metrics 獲取metrics信息 postman

工具postman/visualvm

接口測試工具postman
下載地址:
https://www.postman.com/downloads/
安裝方式:
直接安裝即可
性能分析工具visualvm
下載地址:
http://visualvm.github.io/download.html
安裝方式:
免安裝,進入bin目錄,點擊visualvm.exe運行即可
使用方式:在這裏插入圖片描述

端點(endpoints)

在前面我們通過postman工具訪問了一些actuator中的接口,其實這些接口的訪問路徑被稱爲端點,這些端點在默認情況下除了shutdown端點其他端點默認全部是開啓的。
開啓端點:
management.endpoints.{endpoint-name}.enabled=true
禁用端點:
management.endpoint.enabled-by-default=true
禁用端點會將端點程序的上下文中刪除
暴露/隱藏端點:

優先級:exclude > include

屬性 默認說明
management.endpoints.jmx.exposure.exclude
management.endpoints.jmx.exposure.include jmx暴露所有
management.endpoints.web.exposure.exclude
management.endpoints.web.exposure.include info/health

端點彙總

端點 JMX開啓 WEB開啓
events Yes No
beans Yes No
conditions Yes No
configprops Yes No
env Yes No
flyway Yes No
health Yes Yes
heapdump No
httptrace Yes No
info Yes Yes
jolokia Yes No
logfile Yes No
loggers Yes No
liquibase Yes No
metrics Yes No
mappings Yes No
prometheus No
scheduledtasks Yes No
sessions Yes No
shutdown Yes No
threaddump Yes No

可視化

通過上面對spring-boot-actuator瞭解,actuator是用來監控系統健康情況的,但是在上面我們是通過postman等工具獲取的json格式的字符串,不太友好,下面會結合spring-boot-admin實現監控的可視化。

待續…

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章