是否可以在Springboot弹簧执行器健康端点中停用MongoHealthIndicator?

时间:2015-06-10 17:07:19

标签: spring spring-boot

在我工作的springboot项目中,有一个对spring-data-mongodb的传递maven依赖。因此,MongoHealthIndicator似乎会自动激活,尽管该项目实际上并没有使用mongodb。是否可以在不停用执行器健康终点的情况下专门停用此HealthIndicator?我找到的解决方法是排除依赖关系。但我想知道是否可以对MongoHealthIndicator进行特定的停用。

3 个答案:

答案 0 :(得分:14)

自:

http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html

# HEALTH INDICATORS (previously health.*)
...
management.health.mongo.enabled=true
...

您应该能够将其设置为false以禁用运行状况指示器。来自org.springframework.boot.actuate.autoconfigure.HealthIndicatorAutoConfiguration.java

@Configuration
@ConditionalOnBean(MongoTemplate.class)
@ConditionalOnProperty(prefix = "management.health.mongo", name = "enabled", matchIfMissing = true)
public static class MongoHealthIndicatorConfiguration {

答案 1 :(得分:3)

在application.properties

中试试
management.health.mongo.enabled=false

答案 2 :(得分:2)

<强> application.properties

  

management.health.mongo.enabled =假
  endpoints.mongo.enabled =真

<强> 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;
        }
    }
}