Striveonger

vuePress-theme-reco Mr.Lee    2015 - 2025
Striveonger Striveonger
主页
分类
  • 文章
  • 笔记
  • 工具
标签
时间轴
author-avatar

Mr.Lee

264

Article

134

Tag

主页
分类
  • 文章
  • 笔记
  • 工具
标签
时间轴

SpringCloud 学习笔记-Actuator监控(四)

vuePress-theme-reco Mr.Lee    2015 - 2025

SpringCloud 学习笔记-Actuator监控(四)

Mr.Lee 2021-04-26 17:16:23 SpringCloudEureka

SpringCloud 学习笔记, 第四章 Actuator监控

# Actuator

management:
  endpoint:
    # 服务可以被关闭
    shutdown:
      enabled: true
    # 现实健康详情
    health:
    	showDetails: always
  endpoints:
    web:
      exposure:
        # https://segmentfault.com/a/1190000015309478
        # 开启所有健康端点(注意呦: 这里直接写 * 是会报错的哦~)
        include: "*"
1
2
3
4
5
6
7
8
9
10
11
12
13
14

详细的配置项, 看这里: https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-features.html

# 控制服务的上下线

官方关于Eureka的配置: https://docs.spring.io/spring-cloud/docs/Hoxton.SR9/reference/html/

# 关闭服务

http://localhost:8901/actuator/shutdown

方式太粗暴了, 直接服务就挂掉了. 有没有更温柔一些的方法呢?

# application.yml

eureka:
  client:
    # 上报自己真实的状态(心跳包)
    healthcheck:
      enabled: true
1
2
3
4
5

# HealthStatus

package com.striveonger.demo.serive.web.config;

import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.context.annotation.Configuration;

/**
 * @author Mr.Lee
 * @description: 健康状态配置项
 * @date 2021-04-26 9:24 AM
 */
@Configuration
public class HealthStatus implements HealthIndicator {

    // 0: down, 1: up
    private int status = 1; // 默认为上线

    @Override
    public Health health() {
        Health.Builder builder = new Health.Builder();
        if (status == 0) {
            return builder.down().build();
        }
        return builder.up().build();
    }

    public void up() {
        this.status = 1;
    }

    public void down() {
        this.status = 0;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

# SystemController

/**
 * @author Mr.Lee
 * @description:
 * @date 2021-03-31 4:31 PM
 */
@RestController
@RequestMapping("/system")
public class SystemController {

    @Autowired
    HealthStatus healthStatus;

    @PostMapping("/status/{status}")
    public String status(@PathVariable("status") String status) {
        if ("up".equals(status)) {
            healthStatus.up();
            return "up...";
        } else if ("down".equals(status)) {
            healthStatus.down();
            return "down...";
        } else {
            return "error...";
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 调用下线服务的接口

调用接口

# 执行日志

执行日志

# 结果

结果

实际上我们改的是, provider-hello:8801 服务的健康状态. 查看(http://localhost:8801/actuator/health):

{
    "status": "DOWN"
}
1
2
3