1

I have a Kafka consumer built using spring boot and spring-kafka. It is not a Web Application (only spring-boot-starter dependency) and hence there is no port that is exposed by the application. And I donot want to expose a port just for the sake of health checks.

This kafka consumer application is being packaged as a docker image. The CI/CD pipeline has a stage that verifies if the container is up and the service is started. One option I thought was to check for an active java process that uses the service jar file.

ps aux | grep java ...

But the catch here is that a Kafka consumer can keep running for a while if the Kafka broker is not up and eventually stop with errors. So, using the process based approach is not reliable always.

Are there any other alternative options to find out if the application is up and running fine, given that the application is a standalone non-web app?

Mohan
  • 61
  • 8

3 Answers3

2

you need to schedule a job in the spring boot application that checks whatever needs to be checked

and write the health check result to a file in the container

you can have a cronjob on container level to check output of the spring application in the file and make a final decision about the health status of the container

Emad
  • 82
  • 3
  • 1
    While other solutions were good, this is what I went ahead with. Works well in production. – Mohan Jul 25 '22 at 13:36
0

Popular way for checking application's health is using Spring Boot actuator module it checks different aspects of application, It seems that you should use this module and implement custom end point for checking your application health:

Health Indicators in Spring Boot

I have not any ready source code for calling actuator methods manually but you can try this: Define a command line argument for running actuator health check. Disable actuator end points:

management.endpoints.enabled-by-default=false

Call actuator health check:

@Autowired
private HealthEndpoint healthEndpoint;

public Health getAlive() {
    return healthEndpoint.health();
}

Parse returned Health object and print a string in command line that indicates health status of application. Grab the printed health status string by grep command.

Eskandar Abedini
  • 2,090
  • 2
  • 13
  • I explored Spring Boot actuator for my use case. But it works with a API endpoint exposed. And that would need my application to be exposing a port, which is what I am trying to avoid. – Mohan May 22 '22 at 05:48
  • You can run your application with custom parameters via public static void main(String[] args) that based on given argument runs actuator methods and returns some string in command line that can be grabbed by grep command. – Eskandar Abedini May 22 '22 at 05:52
  • Would you be kind enough to share an example in the current context ? I am not able to wrap my head around this. I already have a main method that starts my spring boot application. How can I extend this so that I can run the actuator methods ? – Mohan May 22 '22 at 05:56
0

As outlined in the Spring Boot reference documentation, you can use the built-in liveness and readiness events.

You could add a custom listener for readiness state events to your application. As soon as your application is ready (after startup), you could create a file (and write stuff to it).

@Component
public class MyReadinessStateExporter {

    @EventListener
    public void onStateChange(AvailabilityChangeEvent<ReadinessState> event) {
        switch (event.getState()) {
        case ACCEPTING_TRAFFIC:
            // create file /tmp/healthy
            break;
        case REFUSING_TRAFFIC:
            // remove file /tmp/healthy
            break;
        }
    }

}

As explained in the same section, you can publish an AvailabilityChangeEvent from any component - the exporter will delete the file and let other systems know that it's not healthy.

Brian Clozel
  • 56,583
  • 15
  • 167
  • 176