大數據

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

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

端點響應緩存

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

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

再次添加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


查看其健康度

微信公眾號:

Leave a Reply

Your email address will not be published. Required fields are marked *