如何在 spring 启动时创建自定义 mongo 健康检查?
How to create custom mongo health check in spring boot?
如何在 spring 引导执行器中禁用默认 mongo 健康检查并创建自定义 mongo 健康检查 url 就像 ../manage/mongo?
application.properties
management.health.mongo.enabled=false
endpoints.mongo.enabled=true
MongoDBHealthCheckEndPoint.java
@ConfigurationProperties(prefix = "endpoints.mongo", ignoreUnknownFields = true)
@Component
public class MongoDBHealthCheckEndPoint extends AbstractEndpoint<Map<String, String>>
{
@Inject
MongoTemplate mongoTemplate;
private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
private static final Map<String, String> UP = new HashMap<String, String>() {{
put("mongo.status", "UP");
}};
private static final Map<String, String> DOWN = new HashMap<String, String>() {{
put("mongo.status", "DOWN");
}};
public MongoDBHealthCheckEndPoint() {
super("mongo", false);
}
public MongoDBHealthCheckEndPoint(Map<String, ? extends Object> mongo) {
super("mongo", false);
}
public Map<String, String> invoke() {
try {
return (new MongoHealthIndicator(mongoTemplate).health().getStatus().equals(Status.UP)) ? UP : DOWN;
} catch (Exception e) {
log.error("mongo database is down", e);
return DOWN;
}
}
}
如何在 spring 引导执行器中禁用默认 mongo 健康检查并创建自定义 mongo 健康检查 url 就像 ../manage/mongo?
application.properties
management.health.mongo.enabled=false
endpoints.mongo.enabled=true
MongoDBHealthCheckEndPoint.java
@ConfigurationProperties(prefix = "endpoints.mongo", ignoreUnknownFields = true)
@Component
public class MongoDBHealthCheckEndPoint extends AbstractEndpoint<Map<String, String>>
{
@Inject
MongoTemplate mongoTemplate;
private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
private static final Map<String, String> UP = new HashMap<String, String>() {{
put("mongo.status", "UP");
}};
private static final Map<String, String> DOWN = new HashMap<String, String>() {{
put("mongo.status", "DOWN");
}};
public MongoDBHealthCheckEndPoint() {
super("mongo", false);
}
public MongoDBHealthCheckEndPoint(Map<String, ? extends Object> mongo) {
super("mongo", false);
}
public Map<String, String> invoke() {
try {
return (new MongoHealthIndicator(mongoTemplate).health().getStatus().equals(Status.UP)) ? UP : DOWN;
} catch (Exception e) {
log.error("mongo database is down", e);
return DOWN;
}
}
}