我为数据库测试创建了弹簧启动执行器的示例项目。我点击网址后运行应用程序
`
响应:
{ “状态”: “DOWN”, “磁盘空间”:{ “状态”: “UP”, “总”:493767094272, “免费”:404929720320, “阈值”:10485760}, “蒙戈”:{” status“:”DOWN“,”error“:”org.springframework.dao.DataAccessResourceFailureException:在等待与ReadPreferenceServerSelector {readPreference = primary}匹配的服务器后30000 ms后超时。群集状态的客户端视图为{type = UNKNOWN, servers = [{address = localhost:27017,type = UNKNOWN,state = CONNECTING,exception = {com.mongodb.MongoSocketOpenException:Exception opening socket},由{java.net.ConnectException:Connection refused:connect}}]引起;嵌套异常是com.mongodb.MongoTimeoutException:在等待与ReadPreferenceServerSelector {readPreference = primary}匹配的服务器后30000毫秒后超时。集群状态的客户端视图是{type = UNKNOWN,servers = [{address = localhost:27017,type = UNKNOWN,state = CONNECTING,exception = {com.mongodb.MongoSocketOpenException:异常打开套接字},由{java.net.ConnectException:Connection refu引起sed:connect}}]“}}
问题:当我的mongodb未运行时,为什么应用程序状态为“DOWN”。我希望我的申请状态“UP”天气mongodb是“DOWN”或“UP”。
以下是我的春季启动应用程序的主类。
package com.company.testing;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
的pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>SpringBootSample</artifactId>
<version>0.0.1</version>
<name>Spring Boot Sample</name>
<description>Spring Boot Sample for spring boot actuator</description>
<groupId>com.company.testing</groupId>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
</dependencies>
<properties>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
答案 0 :(得分:3)
Spring Boot Actuator使用HealthIndicatorAutoconfiguration配置各种与健康相关的bean。其中一个bean称为healthAggregator
,它使用实现或OrderedHealthAggregator。它将使用所有健康指标的最低状态来提供整体应用程序状态(这就是为什么您为整体应用程序获得DOWN的原因。
您可以关闭MongoDb监控(management.health.mongo.enabled=false
)或编写自己的AbstractHealthAggregator实现,例如忽略MongoDb并将其提供给您的配置:
@Bean
public MyHealthAggregator healthAggregator() {
return new MyHealthAggregator();
}
答案 1 :(得分:1)
<强> 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;
}
}