1. 程式人生 > 其它 >SpringBoot 專案健康檢查與監控

SpringBoot 專案健康檢查與監控

前言

Spring Boot-Actuator 為Spring Boot 專案中提供健康檢查與監控的服務。

https://www.baeldung.com/spring-boot-actuators

使用

Spring Boot 最主要的特性就是AutoConfig(自動配置),而對於我們這些使用者來說也就是各種starter,
Spring Boot-Actuator 也提供了starter,為我們自動配置,在使用上我們只需要新增starter到我們的依賴中,然後啟動專案即可。

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

常用Endpoint

Spring Boot-actuator,提供了許多有用的EndPoint,對Spring Boot應用提供各種監控,下面說一下我常用的EndPoint:

/health 應用的健康狀態
/configprops 獲取應用的配置資訊,因為Spring Boot 可能釋出時是單獨的Jar包,配置檔案可能包含其中, 當我們需要檢查配置檔案時可以使用 ConfigpropsEndPoint 進行檢視一些配置是否正確。
/trace 最近幾次的http請求資訊

HealthEndPoint

當我們訪問 http://localhost:8080/health 時,可以看到 HealthEndPoint 給我們提供預設的監控結果,包含 磁碟檢測和資料庫檢測。

{
	status: "UP",
	diskSpace: {
		status: "UP",
		total: 524287995904,
		free: 515949060096,
		threshold: 10485760
	},
	rabbit: {
		status: "UP",
		version: "3.6.2"
	},
	redis: {
		status: "UP",
		cluster_size: 3,
		slots_up: 16384,
		slots_fail: 0
	},
	db: {
		status: "UP",
		database: "MySQL",
		hello: "x"
	},
	hystrix: {
		status: "UP"
	}
}

其實看 Spring Boot-actuator 原始碼,你會發現 HealthEndPoint 提供的資訊不僅限於此,org.springframework.boot.actuate.health 包下 你會發現 ElasticsearchHealthIndicator、RedisHealthIndicator、RabbitHealthIndicator 等
也就是 HealthEndPoint 也提供 ES, Redis 等元件的健康資訊。

自定義Indicator 擴充套件 HealthEndPoint

看原始碼 其實 磁碟和資料庫健康資訊就是 DiskSpaceHealthIndicator、DataSourceHealthIndicator 來實現的,當我們對一些我們自定義的元件進行監控時, 我們也可以實現個Indicator :

@Component
public class User implements HealthIndicator {
    /**
     * user監控 訪問: http://localhost:8080/health
     *
     * @return 自定義Health監控
     */
    @Override
    public Health health() {

        return new Health.Builder().withDetail("usercount", 10) //自定義監控內容
                .withDetail("userstatus", "up").up().build();
    }
}

這時我們再次訪問: http://localhost:8080/health 這時返回的結果如下,包含了我們自定義的 User 健康資訊。

{
    "status": "UP",
    "user": {
        "status": "UP",
        "usercount": 10,
        "userstatus": "up"
    },
    "diskSpace": {
        "status": "UP",
        "total": 398458875904,
        "free": 315097989120,
        "threshold": 10485760
    },
    "db": {
        "status": "UP",
        "database": "MySQL",
        "hello": 1
    }
}

自定義EndPoint

其實除了擴充套件 HealthEndPoint 來新增一些健康檢查, 我們也可以自定定義一些EndPoint 來提供程式執行時一些資訊的展示:

@Configuration
public class EndPointAutoConfig {
    @Bean
    public Endpoint<Map<String, Object>> customEndPoint() {
        return new SystemEndPoint();
    }
}


@ConfigurationProperties(prefix="endpoints.customsystem")
public class SystemEndPoint extends AbstractEndpoint<Map<String, Object>> {

    public SystemEndPoint(){
        super("customsystem");
    }
    @Override
    public Map<String, Object> invoke() {
        Map<String,Object> result= new HashMap<>();
        Map<String, String> map = System.getenv();
        result.put("username",map.get("USERNAME"));
        result.put("computername",map.get("COMPUTERNAME"));
        result.put("userdomain",map.get("USERDOMAIN"));
        return result;
    }
}

訪問 http://localhost:8088/customsystem 來檢視我們自定義的EndPoint ,返回結果如下:

{
    "username": "xxx",
    "userdomain": "DESKTOP-TFHHWDD",
    "computername": "DESKTOP-TFHHWDD"
}