1、编写监控信息:

HealthIndicator:

package com.gcz.indicator;

import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;

/**
 * @author guocz
 * @date 20210712
 * 信息中心健康指示监控
 */
@Component
public class MessageCenterHealthIndicator implements HealthIndicator {

    public MessageCenterHealthIndicator(){}

    @Override
    public Health health() {
        int errorCode = check();
        if (errorCode != 0) {
            return Health.down().withDetail("Message", "error:" + errorCode).build();
        }
        return Health.up().build();
    }

    protected int check() {
        // 模拟返回一个错误状态
        return 1;
    }
}

效果:

{
    "status": "DOWN",
    "components": {
        "diskSpace": {
            "status": "UP",
            "details": {
                "total": 338772881408,
                "free": 278791880704,
                "threshold": 10485760,
                "exists": true
            }
        },
        "messageCenter": {
            "status": "DOWN",
            "details": {
                "Message": "error:1"
            }
        },
        "ping": {
            "status": "UP"
        }
    }
}

Health对象的up方法表示健康,对象正常,down表示异常,可以通过withDetail添加任意信息,对象的名称默认是类名称去掉HealthIndicator后缀。