Add health check with actuator

This commit is contained in:
Serafeim Chatzopoulos 2024-06-05 23:58:15 +03:00
parent 1089f68d91
commit dc8d4a5827
6 changed files with 75 additions and 2 deletions

View File

@ -164,6 +164,12 @@
<version>2.0.1.Final</version> <version>2.0.1.Final</version>
</dependency> </dependency>
<!-- Add actuator dependency -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- Add dump schema dependency --> <!-- Add dump schema dependency -->
<dependency> <dependency>
<groupId>eu.dnetlib.dhp</groupId> <groupId>eu.dnetlib.dhp</groupId>

View File

@ -0,0 +1,45 @@
package eu.openaire.api.config;
import eu.openaire.api.repositories.SolrRepository;
import org.apache.solr.client.solrj.SolrServerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;
import java.io.IOException;
@Component
public class HealthCheck implements HealthIndicator {
@Autowired
private SolrRepository solrRepository;
@Override
public Health health() {
System.out.println("HEALTH CHECK");
// Custom health check logic
boolean isHealthy = checkHealth();
if (isHealthy) {
return Health.up().withDetail("message", "Successfully pinged Solr instances").build();
} else {
return Health.down().withDetail("message", "Could not ping Solr instances; please check the logs for details").build();
}
}
private boolean checkHealth() {
// Ping Solr cluster to check health
try {
if (solrRepository.ping().getStatus() != 0) {
return false;
}
} catch (SolrServerException | IOException e) {
return false;
}
return true;
}
}

View File

@ -0,0 +1,2 @@
package eu.openaire.api.config.health;public class SolrStartupHealthCheck {
}

View File

@ -10,6 +10,7 @@ import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException; import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse; import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument; import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrException;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import java.io.IOException; import java.io.IOException;
@ -62,4 +63,8 @@ public class SolrRepository {
return solrConnectionManager.getSolrClient().query(query); return solrConnectionManager.getSolrClient().query(query);
} }
public void ping() throws SolrServerException, SolrException, IOException {
solrConnectionManager.ping();
}
} }

View File

@ -8,6 +8,7 @@ import org.apache.logging.log4j.Logger;
import org.apache.solr.client.solrj.SolrClient; import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrServerException; import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.CloudSolrClient; import org.apache.solr.client.solrj.impl.CloudSolrClient;
import org.apache.solr.common.SolrException;
import org.springframework.context.annotation.Scope; import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -46,10 +47,15 @@ public class SolrConnectionManager {
// set the appropriate Solr collection // set the appropriate Solr collection
((CloudSolrClient) solrClient).setDefaultCollection(solrProperties.getCollection()); ((CloudSolrClient) solrClient).setDefaultCollection(solrProperties.getCollection());
log.info("Creating Solr client - Ping response: " + solrClient.ping().toString()); // uncomment ping below, to crash app on startup if Solr is not available
// this.ping();
} }
public void ping() throws IOException, SolrException, SolrServerException {
log.info("Solr ping response: " + solrClient.ping().toString());
}
@PreDestroy @PreDestroy
private void closeClient() throws IOException { private void closeClient() throws IOException {
@ -58,6 +64,5 @@ public class SolrConnectionManager {
if (solrClient != null) { if (solrClient != null) {
solrClient.close(); solrClient.close();
} }
} }
} }

View File

@ -12,3 +12,13 @@ logging.level.org.springframework.web=DEBUG
#removes 'trace' field from error responses; alternatively remove 'spring-boot-devtools' dependency #removes 'trace' field from error responses; alternatively remove 'spring-boot-devtools' dependency
server.error.include-stacktrace=never server.error.include-stacktrace=never
# Enable the health endpoint
management.endpoint.health.enabled=true
# Expose the health endpoint
management.endpoints.web.exposure.include=health,info
# Customize health endpoint settings
management.endpoint.health.show-details=always
management.endpoint.health.show-components=always