Created
November 27, 2018 20:54
-
-
Save thomasdarimont/71d669c637148561af1f447d6dde0f10 to your computer and use it in GitHub Desktop.
Dynamic HealthChecks with Spring Boot
This file contains 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
package demo; | |
import org.springframework.boot.SpringApplication; | |
import org.springframework.boot.actuate.health.Health; | |
import org.springframework.boot.actuate.health.HealthIndicator; | |
import org.springframework.boot.actuate.health.HealthIndicatorRegistry; | |
import org.springframework.boot.autoconfigure.SpringBootApplication; | |
import org.springframework.web.bind.annotation.DeleteMapping; | |
import org.springframework.web.bind.annotation.PathVariable; | |
import org.springframework.web.bind.annotation.PutMapping; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
import org.springframework.web.bind.annotation.RestController; | |
import lombok.RequiredArgsConstructor; | |
import lombok.extern.slf4j.Slf4j; | |
@SpringBootApplication | |
public class App { | |
public static void main(String[] args) { | |
SpringApplication.run(App.class, args); | |
} | |
} | |
/** | |
* <pre> | |
* {@code | |
* curl -X PUT http://localhost:8080/healthchecks/gugu | |
* curl -X PUT http://localhost:8080/healthchecks/gaga | |
* | |
* curl http://localhost:8080/actuator/health | |
* | |
* curl -X DELETE http://localhost:8080/healthchecks/gaga | |
* } | |
* </pre> | |
*/ | |
@RestController | |
@RequestMapping("healthchecks") | |
@RequiredArgsConstructor | |
class HealthController { | |
private final HealthIndicatorRegistry registry; | |
@PutMapping("/{name}") | |
public void addHealthCheck(@PathVariable String name) { | |
registry.register(name, new DynamicHealthCheck(name)); | |
} | |
@DeleteMapping("/{name}") | |
public void removeHealthCheck(@PathVariable String name) { | |
registry.unregister(name); | |
} | |
} | |
@Slf4j | |
@RequiredArgsConstructor | |
class DynamicHealthCheck implements HealthIndicator { | |
private final String name; | |
@Override | |
public Health health() { | |
log.info("check health for: {}", name); | |
return Health.up().build(); | |
} | |
} |
This file contains 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
management.endpoint.health.show-details=ALWAYS |
This file contains 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
<?xml version="1.0" encoding="UTF-8"?> | |
<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> | |
<groupId>com.github.thomasdarimont.training</groupId> | |
<artifactId>spring-boot-demo-dynamic-health</artifactId> | |
<version>0.0.1-SNAPSHOT</version> | |
<packaging>jar</packaging> | |
<name>spring-boot-acme-live-1</name> | |
<description>Demo project for Spring Boot</description> | |
<parent> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-parent</artifactId> | |
<version>2.1.0.RELEASE</version> | |
<relativePath /> <!-- lookup parent from repository --> | |
</parent> | |
<properties> | |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> | |
<java.version>1.8</java.version> | |
</properties> | |
<dependencies> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-actuator</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-web</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.projectlombok</groupId> | |
<artifactId>lombok</artifactId> | |
<optional>true</optional> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-devtools</artifactId> | |
<scope>runtime</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-test</artifactId> | |
<scope>test</scope> | |
</dependency> | |
</dependencies> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-maven-plugin</artifactId> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment