【HAVENT 原創】SpringBoot 健康檢測接口配置 + k8s 探針和釘釘消息提醒

一、SpringBoot 健康檢測接口配置

1. 添加 Actuator 依賴
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2. ⾃定義db檢測接⼝(默認的db檢測接⼝有可能導致程序崩潰)
@Component
public class PostgresMonitor implements HealthIndicator {
    @Autowired
    private UserService userService;
    @Override
    public Health health() {
        try {
            userService.count();
            return Health.up().build();
        } catch (Exception e){
            return Health.down().withException(e).build();
        }
    }
}
3. 添加 application.yml 配置
management:
    health:
        db:
            enabled: false 
        redis:
            enabled: false
    endpoint:
        health:
            show-details: always
        status:
            http-mapping:
                down: 503 # 默認返回狀態 200

二、k8s HTTP 探針和釘釘消息提醒

      containers:
      - name:  test-app-container
        image: 127.0.0.1:5000/test-server:1.0.0
        imagePullPolicy: Always
        livenessProbe:
          httpGet:
            path: /admin/actuator/health
            port: 7001
            httpHeaders:
            - name: Test-Header
              value: test-k8s
          initialDelaySeconds: 120
          periodSeconds: 30
        lifecycle:
          preStop:
            exec:
              command:
              - /bin/sh
              - '-c'
              - >-
                wget --header="Content-Type: application/json"
                --post-data="{\"msgtype\":\"markdown\",\"markdown\":
                {\"title\":\"HAVENT 發佈\",\"text\":\"> **發佈提醒:** \n > test-server stoped!\"}}" --output-document=-
                https://oapi.dingtalk.com/robot/send?access_token=XXXXXX;
                sleep 10
        ports:
        - containerPort: 7001
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章