Graceful Service Shutdown Strategies in Spring Cloud
This article explains several graceful shutdown methods for Spring Cloud services, including using kill PID, the /shutdown, /pause, and /service‑registry actuator endpoints, compares their effectiveness, and provides configuration examples to achieve minimal downtime during service upgrades.
In production environments, ensuring that service upgrades do not disrupt user experience is crucial; graceful shutdown aims to make the upgrade invisible to users.
The concept of graceful shutdown is relative—while kill PID is less abrupt than kill -9 PID, it is still not fully graceful.
Method 1: kill PID
Use kill on the Java process ID. This leverages Spring Boot’s shutdown hook, but when Eureka is used, other services may still see the instance for up to 90 seconds, making it insufficiently graceful.
Method 2: /shutdown endpoint
Spring Boot provides a /shutdown actuator endpoint. Enable it by adding the following configuration to application.yml:
management:
endpoint:
shutdown:
enabled: true
endpoints:
web:
exposure:
include: shutdownTrigger the shutdown with a POST request, e.g., curl -X POST http://<your-service>/actuator/shutdown.
This method fundamentally uses the same shutdown hook as Method 1.
Method 3: /pause endpoint
Spring Boot also offers a /pause endpoint. Enable it (and the required restart endpoint) in application.yml:
management:
endpoint:
# enable pause endpoint
pause:
enabled: true
# enable restart endpoint, required by pause
restart:
enabled: true
endpoints:
web:
exposure:
include: pause,restartSend a POST request: curl -X POST http://<your-service>/actuator/pause. The instance is marked DOWN in the service registry but continues to run, allowing traffic to drain before a final kill.
Method 4: /service-registry endpoint
Expose the /service-registry endpoint via configuration:
management:
endpoints:
web:
exposure:
include: service-registryMark the service as DOWN with a POST request, for example:
curl -X "POST" "http://localhost:8000/actuator/service-registry?status=DOWN" \
-H "Content-Type: application/vnd.spring-boot.actuator.v2+json;charset=UTF-8"This approach is considered the most graceful: first mark the instance DOWN, monitor traffic until it reaches zero, then safely upgrade or stop the service.
In practice, these methods work best when multiple instances are deployed; with a single instance the benefit of graceful shutdown diminishes.
The author also promotes related PDF collections on Spring Cloud, Spring Boot, and MyBatis, encouraging readers to follow the public account for more resources.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Code Ape Tech Column
Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
