Created
January 23, 2018 01:10
-
-
Save keith-miller/21743a9c14d4b63d08c16a2a602c96a1 to your computer and use it in GitHub Desktop.
DropWizard HealthCheck with Spring Boot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Configuration | |
public class DataSourceConfiguration { | |
@Autowired | |
private WebApplicationContext context; | |
@Value("${spring.datasource.url}") | |
private String url; | |
@Value("${spring.datasource.username}") | |
private String username; | |
@Value("${spring.datasource.password}") | |
private String password; | |
@Value("${spring.datasource.type}") | |
private String type; | |
@Value("${spring.jpa.hibernate.hikari.minimum-idle}") | |
private int minimumIdle; | |
@Value("${spring.jpa.hibernate.hikari.maximum-pool-size}") | |
private int maximumPoolSize; | |
@Value("${spring.jpa.hibernate.hikari.idle-timeout}") | |
private int idleTimeout; | |
@Bean | |
public DataSource getDataSource() { | |
// set the datasource properties | |
HikariConfig hikariConfig = new HikariConfig(); | |
hikariConfig.setJdbcUrl(url); | |
hikariConfig.setUsername(username); | |
hikariConfig.setPassword(password); | |
hikariConfig.setMinimumIdle(minimumIdle); | |
hikariConfig.setMaximumPoolSize(maximumPoolSize); | |
hikariConfig.setIdleTimeout(idleTimeout); | |
// now grab the healthCheckRegistry | |
HealthCheckRegistry registry = context.getBean("healthCheckRegistry", HealthCheckRegistry.class); | |
hikariConfig.setHealthCheckRegistry(registry); | |
// return the data source | |
return new HikariDataSource(hikariConfig); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component | |
public class HealthCheck implements HealthIndicator { | |
private static final Logger LOGGER = LoggerFactory.getLogger(HealthCheck.class); | |
@Autowired | |
private WebApplicationContext context; | |
@Override | |
public Health health() { | |
HealthCheckRegistry registry = context.getBean("healthCheckRegistry", HealthCheckRegistry.class); | |
for (Map.Entry<String, Result> entry : registry.runHealthChecks().entrySet()) { | |
LOGGER.debug("Key: {} Result: {}", entry.getKey(), entry.getValue().isHealthy()); | |
} | |
return Health.up().build(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Configuration | |
public class HealthCheckConfiguration { | |
@Bean("healthCheckRegistry") | |
@Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON) | |
public HealthCheckRegistry getHealthCheckRegistry() { | |
return new HealthCheckRegistry(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment