前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用Spring Boot实现动态健康检查HealthChecks

使用Spring Boot实现动态健康检查HealthChecks

作者头像
lyb-geek
发布2018-12-27 14:48:15
3.8K0
发布2018-12-27 14:48:15
举报
文章被收录于专栏:Linyb极客之路Linyb极客之路
代码语言:javascript
复制
import org.springframework.boot.SpringApplication;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.boot.actuate.health.HealthIndicatorRegistry;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@SpringBootApplication
public class App {

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

/**
 * <pre>
 * {@code 
 * curl -X PUT http://localhost:8080/healthchecks/gugu
 * curl -X PUT http://localhost:8080/healthchecks/gaga
 * 
 * curl http://localhost:8080/actuator/health
 * 
 * curl -X DELETE http://localhost:8080/healthchecks/gaga
 * }
 * </pre>
 */
@RestController
@RequestMapping("healthchecks")
@RequiredArgsConstructor
class HealthController {

    private final HealthIndicatorRegistry registry;

    @PutMapping("/{name}")
    public void addHealthCheck(@PathVariable String name) {
        registry.register(name, new DynamicHealthCheck(name));
    }

    @DeleteMapping("/{name}")
    public void removeHealthCheck(@PathVariable String name) {
        registry.unregister(name);
    }
}

@Slf4j
@RequiredArgsConstructor
class DynamicHealthCheck implements HealthIndicator {

    private final String name;

    @Override
    public Health health() {

        log.info("check health for: {}", name);

        return Health.up().build();
    }

}

application.properties

management.endpoint.health.show-details=ALWAYS

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-12-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Linyb极客之路 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • application.properties
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档