springboot 使用actuator監控


spring-boot-starter-actuator是一個用於暴露自身信息的模塊,主要用於監控與管理。

 

加入依賴

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

 

訪問

使用前綴/actuator加上端點ID來訪問。例如,在默認情況下,health端點映射到/actuator/health。

 

 雖然大部分端點在默認情況下都是啟用狀態,但是在Spring Boot應用中,默認只開啟info端點和health端點。其余端點都需要通過聲明屬性來開啟

management:
  endpoints:
    web:
      exposure:
        include: ["*"] #開啟全部端點相關代碼

 

保護HTTP端點

這時我們可以像使用任何其他敏感URL一樣注意保護HTTP端點。若存在Spring Security,則默認使用Spring Security的內容協商策略來保護端點。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

在配置文件中為Spring Security設置一個安全用戶

spring:
  security:
    user: # 配置安全用戶
      name: admin
      password: 123456

 

健康信息

health端點是查看Spring Boot應用程序健康狀況的端點,如果沒有特殊設置,顯示的信息就比較少

可以通過在配置文件中設置management.endpoint.health.show-details來決定health端點的細節信息是否展示。以下為health端點的細節屬性。

• never:細節信息詳情永遠都不展示。

• when-authorized:細節詳情只對授權用戶顯示。

• always:細節詳情顯示給所有用戶。

management:
  endpoint:
    health:
      show-details: always #health端點的細節屬性是否展示

健康信息的內容是從HealthIndicators中收集應用程序中定義的所有bean中的上下文信息,其中包含一些自動配置的HealthIndicators,也可以編寫自己的健康信息bean。Spring Boot默認會自動配置以下HealthIndicators。

• CassandraHealthIndicator:檢查Cassandra數據庫是否已啟動。

• DiskSpaceHealthIndicator:檢查磁盤空間是否不足。

• DataSourceHealthIndicator:檢查是否可以獲得連接的DataSource。

• ElasticsearchHealthIndicator:檢查Elasticsearch集群是否已啟動。

• InfluxDbHealthIndicator:檢查InfluxDB服務器是否已啟動。

• JmsHealthIndicator:檢查JMS代理是否已啟動。

• MailHealthIndicator:檢查郵件服務器是否已啟動。

• MongoHealthIndicator:檢查Mongo數據庫是否已啟動。

• Neo4jHealthIndicator:檢查Neo4j數據庫是否已啟動。

• RabbitHealthIndicator:檢查Rabbit服務器是否已啟動。

• RedisHealthIndicator:檢查Redis服務器是否已啟動。

• SolrHealthIndicator:檢查Solr服務器是否已啟動。

 

自定義健康狀況信息

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;

/**
 * 自定義健康狀況信息
 * @author tl19638
 * @date 2020/10/5
 */
@Component
public class MyHealthIndicator implements HealthIndicator {

    @Value("${server.port}")
    private String serverPort;

    @Override
    public Health health() {
        if (!"80".equals(serverPort)) {
            return Health.down().withDetail("端口不是80", "是" + serverPort).build();
        }
        return Health.up().build();
    }

}

http://192.168.199.1:8081/actuator/health

 

 

自定義應用程序信息

info: # 公開自定義info信息
  encoding: UTF-8
  jdk:
    version: 1.8

http://192.168.199.1:8081/actuator/info

 

 

參考:

官網地址:https://docs.spring.io/spring-boot/docs/2.1.8.RELEASE/actuator-api//html/

Spring Boot 2實戰之旅-8.1 使用actuator監控

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM