当 mongo 与 spring 引导执行器一起关闭时,应用程序状态关闭
Application status down, when mongo is down with spring boot actuator
我已经为 spring 引导执行器创建了带有数据库测试的示例项目。 运行 申请的时候我打了 URL
- 当 Mongo DB 为 运行
- URL: http://localhost:8080/health
- 回应:
`{"status":"UP","diskSpace":{"status":"UP","total":493767094272,[=56=]:404928278528,"threshold":10485760},"mongo":{"status":"UP","version":"3.0.2"}}
`
- 当 Mongo DB 不是 运行
- URL:
http://localhost:8080/health
响应:
{"status":"DOWN","diskSpace":{"status":"UP","total":493767094272,[=56=] :404929720320,"threshold":10485760},"mongo":{"status":"DOWN","error":"org.springframework.dao.DataAccessResourceFailureException: Timed out after 30000 ms while waiting for a server that matches ReadPreferenceServerSelector{readPreference=primary}. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused: connect}}]; nested exception is com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches ReadPreferenceServerSelector{readPreference=primary}. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused: connect}}]"}}
问题:为什么申请状态是"DOWN",而我的mongodb不是运行。我希望我的申请状态 "UP" 天气 mongodb 是 "DOWN" 或 "UP".
下面是 class 用于 spring 引导应用程序的主要 class。
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>
Spring Boot Actuator 使用 HealthIndicatorAutoconfiguration to configure various health related beans. One of the beans is called healthAggregator
which uses the implementation or OrderedHealthAggregator。它将使用所有运行状况指标中的最低状态来提供整体应用程序状态(这就是为什么您对整个应用程序感到失望。
您可以关闭 MongoDb 监控 (management.health.mongo.enabled=false
) 或编写您自己的 AbstractHealthAggregator 实现,例如忽略 MongoDb 正在关闭并在你的配置:
@Bean
public MyHealthAggregator healthAggregator() {
return new MyHealthAggregator();
}
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 引导执行器创建了带有数据库测试的示例项目。 运行 申请的时候我打了 URL
- 当 Mongo DB 为 运行
- URL: http://localhost:8080/health
- 回应: `{"status":"UP","diskSpace":{"status":"UP","total":493767094272,[=56=]:404928278528,"threshold":10485760},"mongo":{"status":"UP","version":"3.0.2"}}
`
- 当 Mongo DB 不是 运行
- URL: http://localhost:8080/health
响应:
{"status":"DOWN","diskSpace":{"status":"UP","total":493767094272,[=56=] :404929720320,"threshold":10485760},"mongo":{"status":"DOWN","error":"org.springframework.dao.DataAccessResourceFailureException: Timed out after 30000 ms while waiting for a server that matches ReadPreferenceServerSelector{readPreference=primary}. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused: connect}}]; nested exception is com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches ReadPreferenceServerSelector{readPreference=primary}. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused: connect}}]"}}
问题:为什么申请状态是"DOWN",而我的mongodb不是运行。我希望我的申请状态 "UP" 天气 mongodb 是 "DOWN" 或 "UP".
下面是 class 用于 spring 引导应用程序的主要 class。
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>
Spring Boot Actuator 使用 HealthIndicatorAutoconfiguration to configure various health related beans. One of the beans is called healthAggregator
which uses the implementation or OrderedHealthAggregator。它将使用所有运行状况指标中的最低状态来提供整体应用程序状态(这就是为什么您对整个应用程序感到失望。
您可以关闭 MongoDb 监控 (management.health.mongo.enabled=false
) 或编写您自己的 AbstractHealthAggregator 实现,例如忽略 MongoDb 正在关闭并在你的配置:
@Bean
public MyHealthAggregator healthAggregator() {
return new MyHealthAggregator();
}
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;
}
}