天天看點

Spring Boot 應用監控端點配置配置資訊可視化微信公衆号:

當一個Spring Boot 應用運作的時候,開發者需要對Spring Boot應用進行實時監控,獲得項目的報警需求,Spring Boot 提供了,actuator 來幫助開發者擷取應用程式運作時的資料。

端點配置

在Spring Boot 中添加端點配置相當的簡單。

隻需要添加 spring-boot-starter-actuator

添加相關的依賴

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-actuator -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
            <version>2.3.1.RELEASE</version>
        </dependency>           

常用的端點如下:

常用端點列舉如下,可以一個個詳細試一下:

/info        應用基本資訊

/health       健康度資訊

/metrics      運作名額

/env        環境變量資訊

/loggers      日志相關

/dump       線程相關資訊

/trace       請求調用軌迹

這些端點大都是預設開啟的,如果想要開啟一個端點,需要在配置檔案中,配置以下内容。

endpoints:
  metrics:
    sensitive: false           

此時sensitive 是關閉的。

舉個例子:

這裡舉個例子,通路是否線上的接口

localhost:8080/actuator/health           

此時浏覽器的輸出結果為:

https://www.iming.info/wp-content/uploads/2020/06/wp_editor_md_caebd1705f2380ec60437c1ba602181a.jpg

端點響應緩存

對于一些不帶參數的端點将會進行緩存。

management:
  endpoint:
    auditevents:
      cache:
        time-to-live: 100s
           

上方的配置說明了緩存達到100s

路徑映射

可以對通路的路徑進行映射。

management:
  endpoints:
    web:
      base-path: /
      path-mapping: 
        health: healthcheck
           

此時通路路徑由原先的 localhost:8080/actuator/health 轉變為 localhost:8080/healthcheck

CORS

進行跨域操作。

可以通過配置檔案,快速的開啟CORS支援。

management:
  endpoints:
    web:
      cors:
        allowed-origins: http://localhost:8091
        allowed-methods: *           

在上方中,允許處理,來自

http://localhost:8091

的任何請求,允許的方法任意。

配置資訊可視化

添加相關的依賴。

<!-- https://mvnrepository.com/artifact/de.codecentric/spring-boot-admin-starter-server -->
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-server</artifactId>
            <version>2.2.3</version>
        </dependency>           

在啟動類上增加相關的注解:

package com.example.demo;

import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableAdminServer
public class DemoApplication {

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

}
           

配置完成以後,輸傳入連結接,進行通路。

http://localhost:8080/index.html           
https://www.iming.info/wp-content/uploads/2020/06/wp_editor_md_699587be7a0eb9fc3719f0098fd444d2.jpg

再次添加client端

<!-- https://mvnrepository.com/artifact/de.codecentric/spring-boot-admin-starter-client -->
<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-client</artifactId>
    <version>2.2.3</version>
</dependency>
           

書寫配置檔案

spring:
  boot:
    admin:
      client:
        url: http://localhost:8080           

此時檢視admin

https://www.iming.info/wp-content/uploads/2020/06/wp_editor_md_16949974fda3e34c3cd6387644a8110f.jpg https://www.iming.info/wp-content/uploads/2020/06/wp_editor_md_b1ebe7cab4735020bb18e8eda921e254.jpg

檢視其健康度

https://www.iming.info/wp-content/uploads/2020/06/wp_editor_md_4537dc4d788ec897ce43d616f8f33020.jpg

微信公衆号:

https://www.iming.info/wp-content/uploads/2020/06/qrcode_for_gh_9901b36b3b0e_258-1.jpg