Search code examples
javaspring-bootapache-zookeepershutdown

Shutdown Spring boot application after zookeeper connection is lost


I am running a spring boot application in a clustered zookeeper setup . I am using leader election to ensure that only one instance of the springboot application is running. Once the leader goes down , then other node(follower) takes up the leader and starts the process. If the leader goes down because we shutdown the application , then new leader can run. but if the network is lost with the leader then the process in the old leader will be still running and a new leader will also run. So two instances will be running. Is there a way to stop the application in the old leader? or am i asking a wrong question. I am new to springboot and zookeeper. Correct me if i am wrong!!


Solution

  • You can do this with spring-boot-actuator. It has a /shutdown endpoint that does exactly what it says on the tin. Note this endpoint is not enabled by default so you need to enable it by setting the following in application.properties:

    endpoints.shutdown.enabled=true
    

    You probably want to configure some security around this too, as it's not a wise idea to let anyone just shut down your application.

    For more info see the docs