Spring Boot監控詳解

Spring Boot是一個非常流行的Java企業級Web應用開發框架。除了具備開箱即用、簡化配置等優點外,它還提供了強大的監控功能。這篇文章將圍繞Spring Boot監控展開,從多個方面詳細闡述它的功能與使用。

一、基本監控指標

在應用程序運行過程中,我們需要實時地了解到它的基礎指標數據,比如CPU、內存、磁盤、網絡等。Spring Boot提供了很多監控指標收集器,可以方便地獲取這些數據。

首先,使用Spring Boot Actuator來進行監控:


// pom.xml
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

然後,在配置文件中增加以下內容:


spring:
  application:
    name: demo
management:
  endpoints:
    web:
      exposure:
        include: '*'
  endpoint:
    health:
      show-details: always

通過訪問”/actuator/health”接口,可以獲取到應用程序的健康狀況。訪問”/actuator/metrics”接口,可以獲取到應用程序的基礎指標數據。

除此之外,還可以使用Spring Boot Admin進行監控,它提供了更豐富的監控指標展示和操作。Spring Boot Admin需要引入以下依賴:


// pom.xml
<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-server</artifactId>
</dependency>

然後,在配置文件中增加以下內容:


spring.boot.admin.ui.title: Demo Admin
spring.boot.admin.ui.favicon: /img/favicon.ico
spring.boot.admin.notify.mail.to: admin@demo.com
spring.boot.admin.notify.mail.from: noreply@demo.com
spring.boot.admin.notify.mail.ignore-errors: false
logging.level.de.codecentric.boot.admin.server.domain.services.DefaultNotificationTrigger: DEBUG

通過訪問”http://localhost:8080″界面,可以獲取到應用程序的健康狀況、基礎指標數據、日誌信息等,並且支持郵件、Slack等多種通知方式。

二、自定義指標

除了基礎指標數據外,我們可能還需要自定義一些指標,比如業務邏輯相關的指標。Spring Boot提供了Metrics API,可以自定義指標數據,並將其集成到Actuator中,方便展示與監控。

首先,使用以下依賴:


// pom.xml
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-core</artifactId>
</dependency>
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>

然後,編寫代碼:


@Service
public class CustomMetricsService {
    private final Counter counter;

    public CustomMetricsService(MeterRegistry registry) {
        counter = registry.counter("custom_counter_total", "type", "business");
    }

    public void increase() {
        counter.increment();
    }
}

通過以上代碼,我們定義了一個名為”custom_counter_total”的計數器,用於記錄業務邏輯相關的調用次數。接下來,在業務邏輯代碼中調用CustomMetricsService的increase方法,就可以在Actuator中看到相應的指標數據了。

三、應用狀態檢查

在應用程序運行過程中,隨時可能出現一些異常情況,比如數據庫連接異常、消息隊列連接異常等。這時,我們需要快速地了解到應用程序的健康狀態,並及時進行處理。Spring Boot提供了HealthIndicator接口,可以自定義應用程序狀態檢查邏輯。

首先,定義一個檢查類:


@Component
public class MyHealthIndicator implements HealthIndicator {
    @Override
    public Health health() {
        return Health.up().build();
    }
}

通過實現HealthIndicator接口,定義了一個名為”MyHealthIndicator”的健康檢查器。在health方法中編寫健康檢查邏輯,根據檢查結果返回相應的Health對象。

然後,配置文件中增加以下內容:


management.endpoint.health.show-details: always
management.health.status.order: DOWN, OUT_OF_SERVICE, UNKNOWN, UP

通過以上代碼,我們開啟了健康檢查器的詳細展示,並設置了檢查結果的順序。接下來,訪問”/actuator/health”接口,就可以看到自定義的健康檢查結果了。

四、應用詳細信息

在應用程序開發、部署、運維過程中,我們需要了解到應用程序的詳細信息,比如版本號、Git提交ID等。Spring Boot提供了InfoContributor接口,可以方便地自定義應用程序的詳細信息。

首先,定義一個信息類:


@Component
public class MyInfoContributor implements InfoContributor {
    @Override
    public void contribute(Info.Builder builder) {
        builder.withDetail("version", "1.0.0")
               .withDetail("git", "abcd1234");
    }
}

通過實現InfoContributor接口,定義了一個名為”MyInfoContributor”的信息類。在contribute方法中編寫詳細信息邏輯,向Info.Builder中添加自定義的信息。

然後,訪問”/actuator/info”接口,就可以看到應用程序的詳細信息了。

五、日誌跟蹤

在應用程序出現問題時,我們需要查看相應的日誌,並確定問題的原因。Spring Boot提供了Trace功能,可以方便地跟蹤應用程序的請求與響應,從而快速定位問題。

首先,增加以下配置:


logging.level.org.springframework.boot.actuate.trace.http:
  InMemoryHttpTraceRepository: trace

通過以上配置,我們啟用了Trace功能,並使用InMemoryHttpTraceRepository作為存儲器。接下來,在代碼中添加一行註解:


@Slf4j
@RestController
public class DemoController {
    @GetMapping("/")
    @Trace
    public String hello() {
        log.info("hello world");
        return "hello world";
    }
}

通過添加@Trace註解,我們標示了該方法需要追蹤,Spring Boot就會記錄該方法的入參、出參、異常等信息,並保存到InMemoryHttpTraceRepository中。訪問”/actuator/httptrace”接口,就可以查看Trace記錄信息了。

總結

本文從基本監控指標、自定義指標、應用狀態檢查、應用詳細信息、日誌跟蹤等方面詳細闡述了Spring Boot監控的功能與使用方法。通過集成Actuator、Admin、Metrics、InfoContributor、Trace等組件,我們能夠更方便地獲取應用程序的基礎指標、自定義指標、應用狀態、詳細信息和請求響應信息,從而更加高效地開發、部署和運維我們的應用程序。

原創文章,作者:IGBCW,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/333202.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
IGBCW的頭像IGBCW
上一篇 2025-01-27 13:34
下一篇 2025-01-27 13:35

相關推薦

發表回復

登錄後才能評論