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();
}
}
management.endpoint.health.show-details=ALWAYS