EN VI

Spring-boot - SpringBoot Actuator version 1.X readiness and liveness probes?

2024-03-14 03:30:05
How to Spring-boot - SpringBoot Actuator version 1.X readiness and liveness probes

I'm exploring options to activate readiness and liveness probes for SpringBoot 1.X. It's essential to have both endpoints enabled for seamless Kubernetes deployments. Any insights on achieving this?

/actuator/health/liveness
/actuator/health/readiness

Is it feasible to obtain those features in the upcoming Spring version, or should I proceed with implementing them myself?

Thanks in advance.

Solution:

For Spring Boot 1.X, you won't have these endpoints out of the box. However, you can implement custom health check endpoints to achieve similar functionality.

Create Custom Health Indicators: Implement custom health indicators by extending the HealthIndicator interface provided by Spring Boot. You can write logic to determine the health status of different components of your application.

    import org.springframework.boot.actuate.health.Health;
    import org.springframework.boot.actuate.health.HealthIndicator;
    import org.springframework.stereotype.Component;

    @Component
    public class CustomHealthIndicator implements HealthIndicator {

    @Override
    public Health health() {
        // Logic to check the health of your application
        if (isApplicationHealthy()) {
            return Health.up().build();
        } else {
            return Health.down().build();
        }
    }

    private boolean isApplicationHealthy() {
        // Check the health of your application components
        return true; // Return true if the application is healthy, false otherwise
    }
}

Expose Custom Endpoints: Expose custom endpoints in your application controller to expose the health status information.

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.actuate.health.Health;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;

    @RestController
    public class HealthCheckController {

    @Autowired
    private CustomHealthIndicator customHealthIndicator;

    @GetMapping("/custom/health/liveness")
    public Health liveness() {
        return customHealthIndicator.health();
    }

    @GetMapping("/custom/health/readiness")
    public Health readiness() {
        return customHealthIndicator.health();
    }
}

Configure Probes in Kubernetes: Once you have implemented these endpoints, you can configure Kubernetes to use them for readiness and liveness probes in your deployment configuration.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: your-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: your-app
  template:
    metadata:
      labels:
        app: your-app
    spec:
      containers:
        - name: your-app
          image: your-app-image:tag
          ports:
            - containerPort: 8080
          readinessProbe:
            httpGet:
              path: /custom/health/readiness
              port: 8080
          livenessProbe:
            httpGet:
              path: /custom/health/liveness
              port: 8080
Answer

Login


Forgot Your Password?

Create Account


Lost your password? Please enter your email address. You will receive a link to create a new password.

Reset Password

Back to login