01.03 Springboot2.0學習7 使用actuator配置監控點

一、概述

微服務架構中,由於子系統眾多,集群的監控運維變得非常重要。SpringBoot在Starter POMs中提供了spring-boot-starter-actuator模塊,這個模塊可以自動為SpringBoot構建的應用提供一系列和於監控的端點。

二、使用

1. 準備工作

  • 新建一個springboot webservice項目。
  • jdk1.8
  • idea
  • gradle或maven

2. 添加依賴

maven項目裡在pom.xml加入依賴:

<code><dependencies>
<dependency>
<groupid>org.springframework.boot/<groupid>
<artifactid>spring-boot-starter-actuator/<artifactid>
/<dependency>
/<dependencies>/<code>

或在gradle項目裡 build.gradle加入:

<code>    // https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-actuator
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-actuator', version: '2.2.2.RELEASE'/<code>

加入模塊後,重啟應用,在控制檯看到輸出的一些端點定義:

Springboot2.0學習7 使用actuator配置監控點

在瀏覽器輸入: http://localhost:9090/actuator,看到輸出:

<code>{
"_links":{
"self":{
"href":"http://localhost:9090/actuator",
"templated":false
},
"health-path":{
"href":"http://localhost:9090/actuator/health/{*path}",
"templated":true
},
"health":{
"href":"http://localhost:9090/actuator/health",
"templated":false
},
"info":{
"href":"http://localhost:9090/actuator/info",
"templated":false
}
}
}/<code>

三、開啟驗證

1. 配置actuator端口

在application.yaml裡設置:

<code>spring:
application:
name: demoservice
server:
port: 9090
management:
port: 54001/<code>

訪問端口的地址:http://localhost:54001/actuator

2. 修改訪問地址

<code>spring:
application:
name: demoservice

management:
endpoints:
web:
base-path: "/monitor"/<code>

訪問平臺地址改為:
http://localhost:8080/monitor

3. 開啟安全認證

添加依賴

<code><dependency>
<groupid>org.springframework.boot/<groupid>
<artifactid>spring-boot-starter-security/<artifactid>
<version>${boot.starter.version}/<version>
/<dependency>/<code>

<code>implementation group: 'org.springframework.boot', name: 'spring-boot-starter-security', version: '2.2.2.RELEASE'/<code>

配置文件設置:

<code>spring:
application:
name: demoservice
server:
port: 9090
management:
server:
port: 54001
security:
enabled: true/<code>

重啟應用,控制檯可以看到打印的密碼:

Springboot2.0學習7 使用actuator配置監控點


默認賬號是user

5. 設置訪問賬號

<code>spring:
application:
name: demoservice
security:
user:
name: admin
password: admin/<code>
<code>spring:
security:
basic:
path: /bean/<code>

四、原生端口說明

原生端點分成三大類:

  • 應用配置類
  • 度量指標類
  • 操作控制類

1. 應用配置類

Springboot採用包掃描和自動化配置的機制加載配置,通過應用配置類端點可以獲取關於Spring應用配置內容的詳細報告,比如自動化配置的報告、Bean創建的創造、環境屬性的報告等。

/autoconfig

用來獲取應用的自動化配置報告,包括所有自動化配置的候選項,同時還列出每個候選項是否滿足自動化配置的各個先決條件。該端點可以幫助找到一些自動化配置為什麼沒有生效的具體原因。
該報告把自動化配置分為兩個部分:

  • positive返回條件匹配成功的自動化配置
  • negativeMatches 返回條件匹配不成功的自動化配置

/beans

該端點用來獲取應用上下文中創建的所有Bean
訪問: http://localhost:8080/actuator/beans

Springboot2.0學習7 使用actuator配置監控點

/env

用來獲取應用所有可用的環境屬性報告,包括:

  • 環境變量
  • JVM屬性
  • 應用的配置屬性
  • 命令行參數

當配置文件裡包含一些敏感信息,返回內容中會用*顯示。

/mappings

用來返回所有SpringMVC的控制器映射關係報告

/info

用來返回一些應用自定義的信息。默認情況下,該端點只會返回一個空的JSON內容。
可以在application.yaml加入:

<code>info:
app:
name: spring-boot-acuator
version: v1.0.0/<code>

再訪問http://localhost:54001/actuator/info 節點,可以看到返回報告。

Springboot2.0學習7 使用actuator配置監控點

2. 度量指標類

/metrics

用來返回當前應用的各類重要度量指標,如內存、線程、垃圾回收信息。
新版本中有些節點默認不能直接訪問,在yaml中按如下配置:

<code>spring:
application:
name: demoservice
management:
endpoints:
web:
exposure:
include: "*" //或者 include: metrics,info,health 用,隔開多個/<code>
Springboot2.0學習7 使用actuator配置監控點

/health

獲取應用的各類健康指標信息。

/dump

用來暴露程序運行中的線程信息,它使用java.lang.management.ThreadMXBean的dumpAllThreads方法來返回所有含有同步信息的活動線程詳情。

/threaddump

線程dump信息

/trace

該端點用來返回基本的HTTP跟蹤信息。默認情況下,跟蹤信息的存儲採用org.springframework.boot.actuate.trace.InMemoryTraceRepository實現的內存方式,始終保留最近的100條請求記錄。

3. 操作控制類

/shutdown

關閉應用
要啟動該功能,在配置文件輸入:

<code>spring:
application:
name: demoservice
management:
endpoint:
shutdown:
enabled: true
sensitive: false
endpoints:
web:
exposure:
include: "*"/<code>

使用POST請求 http://localhost:8080/acuator/shutdown

Springboot2.0學習7 使用actuator配置監控點


分享到:


相關文章: