【发布时间】:2020-09-22 21:53:15
【问题描述】:
我有一个不是网络服务的微服务。
它是一个 Spring Boot (1.5) CommandLineRunner 应用程序,不需要公开 API 或使用 http 做任何事情。
但是,我需要给它一个 Kubernetes 的活性探测。
这是否可以在不将其重构为 Web 服务应用程序的情况下完成?
我添加了这个配置来启用 Spring 的 info 端点
management:
endpoint:
health:
enabled: true
info:
enabled: true
# https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#production-ready-endpoints
info:
app:
name: foo-parser
description: parses binary files from S3 and updates the database
我实现了这个健康检查类
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
import org.springframework.boot.actuate.health.Health.Builder;
@Component
public class HealthCheck extends AbstractHealthIndicator {
Logger log = LoggerFactory.getLogger("jsonLogger");
private final MySQLAccessor mySQLAccessor;
private static final String FOO_TABLE = "foo";
@Autowired
public HealthCheck(final MySQLAccessor mySQLAccessor) {
this.mySQLAccessor = mySQLAccessor;
}
@Override
protected void doHealthCheck(Builder builder) throws Exception {
boolean result = mySQLAccessor.healthCheck(FOO_TABLE);
if (result) {
log.info("HELLO! the health check is good!");
builder.up().withDetail("test", "good");
}
else {
log.info("HELLO! OH NOES the health check is ungood!");
builder.down().withDetail("test", "bad");
}
}
}
这个想法可行吗?还是我必须重构它以服务 Web 请求?
感谢您提供任何线索
【问题讨论】:
-
您可以定义一个 exec liveness probe 来运行 cmdline 命令来检查它是否有效。
-
我想我应该澄清一下。我需要探针来检查与数据库的连接。从 Java 中可以很容易地做到这一点......但不确定如何正确连接它。我将编辑问题
-
这很有挑战性,我见过一些应用程序实际上拉入执行器和一些 Web 组件只是为了进行这种监控。
标签: java spring-boot kubernetes health-check