Sub topic 2
Health Endpoints and Deployment Options
Assume you know basic Java, Spring Boot, HTTP, ports, and Maven or Gradle. This chapter connects Actuator health checks to safe shutdown and common deployment targets.
Health Endpoints with Actuator
Spring Boot Actuator is a set of production features for monitoring an application. Its health endpoint reports whether the application can serve traffic.
Add the Actuator dependency, then configure health probes:
management.endpoint.health.probes.enabled=true
management.endpoints.web.exposure.include=health,infoThe application exposes /actuator/health/liveness and /actuator/health/readiness when it runs in a supported environment. Liveness means the process should remain running. Readiness means the application is ready to receive traffic, including required dependencies such as a database.
Expose only endpoints that clients need. Do not expose sensitive management endpoints publicly.
Shutting Down Without Dropping Requests
Graceful shutdown lets Spring Boot stop accepting new requests while existing requests finish:
server.shutdown=graceful
spring.lifecycle.timeout-per-shutdown-phase=30sDuring a deployment, the load balancer should stop sending traffic before the process exits. The timeout prevents one stuck request from blocking shutdown forever.
Running the JAR as a systemd Service
On a Linux virtual machine, systemd can supervise the executable JAR. Run it as an unprivileged user and keep configuration in an environment file.
[Service]
User=myapp
EnvironmentFile=/etc/myapp/env
ExecStart=/usr/bin/java -jar /opt/myapp/app.jar
SuccessExitStatus=143
Restart=on-failureExit code 143 represents termination by SIGTERM, which is the normal signal for graceful shutdown. The service manager can restart the application after an unexpected failure.
When You Need a WAR
A WAR is useful when an organization requires an external servlet container, such as an existing Tomcat installation. Extend SpringBootServletInitializer and package the application as a WAR. Prefer an executable JAR for new deployments because it includes the server and has fewer external dependencies.
Packaging a Container Image
A Dockerfile packages the JAR with a Java runtime:
FROM eclipse-temurin:21-jre
RUN useradd --system spring
USER spring
WORKDIR /app
COPY target/myapp-1.0.0.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "app.jar"]Run the image with a platform-managed health check and pass configuration through environment variables. Do not copy passwords into the image.
Exercise
Which probe should remove an instance from a load balancer without restarting the process?
Show answer
The readiness probe.