Publié · en amélioration
Guide Spring Boot · 6/6
Ce chapitre n'est disponible qu'en anglais pour le moment.
A Spring Boot application is packaged as a single executable jar that includes its web server. You can run that jar directly on a server, or build a container image and deploy it to a platform such as Kubernetes. This chapter covers packaging, container images, production configuration and Actuator health checks.
# Gradle: build/libs/demo-0.0.1-SNAPSHOT.jar
./gradlew bootJar
# Maven: target/demo-0.0.1-SNAPSHOT.jar
./mvnw package
java -jar build/libs/demo-0.0.1-SNAPSHOT.jarGradle's build task also produces a -plain.jar without dependencies; deploy the jar without plain in its name. The target machine needs only a JRE.
Treat the configuration inside the jar as defaults and pass environment-specific values at launch through environment variables or command-line arguments. Never bake secrets into images or commit them to the repository.
export SPRING_PROFILES_ACTIVE=prod
export SPRING_DATASOURCE_URL=jdbc:postgresql://db:5432/demo
export SPRING_DATASOURCE_PASSWORD=change-me
java -jar demo.jar --server.port=8080You can build an image without writing a Dockerfile, using Cloud Native Buildpacks. Docker must be running; the buildpack chooses the JRE, splits the image into layers and tunes memory settings for you.
./gradlew bootBuildImage --imageName=myorg/demo
./mvnw spring-boot:build-image -Dspring-boot.build-image.imageName=myorg/demo
docker run -p 8080:8080 myorg/demoIf you want full control over the image, write a Dockerfile. Extracting the jar into layers means a code-only change does not invalidate the dependency layers, which makes rebuilds and pulls faster.
FROM eclipse-temurin:21-jre AS builder
WORKDIR /builder
COPY build/libs/demo-0.0.1-SNAPSHOT.jar application.jar
RUN java -Djarmode=tools -jar application.jar extract --layers --destination extracted
FROM eclipse-temurin:21-jre
WORKDIR /application
COPY --from=builder /builder/extracted/dependencies/ ./
COPY --from=builder /builder/extracted/spring-boot-loader/ ./
COPY --from=builder /builder/extracted/snapshot-dependencies/ ./
COPY --from=builder /builder/extracted/application/ ./
ENTRYPOINT ["java", "-jar", "application.jar"]spring-boot-starter-actuator adds production endpoints. Only /actuator/health is exposed over HTTP by default; expose others selectively.
management:
endpoints:
web:
exposure:
include: health,info
endpoint:
health:
probes:
enabled: true
show-details: when-authorizedcurl http://localhost:8080/actuator/health returns {"status":"UP"}, which turns into DOWN if, for example, the database becomes unreachable. Enabling probes adds /actuator/health/liveness and /actuator/health/readiness; they are enabled automatically when the app detects it is running on Kubernetes.
# Part of a container spec in a Kubernetes Deployment
livenessProbe:
httpGet:
path: /actuator/health/liveness
port: 8080
readinessProbe:
httpGet:
path: /actuator/health/readiness
port: 8080spring.jpa.hibernate.ddl-auto to validate or none and change the schema only through Flyway.bootJar or package and run it with java -jar.bootBuildImage (Buildpacks) or a layered Dockerfile.
0 commentaire
Se connecter · Connectez-vous pour laisser un commentaire.
Soyez le premier à commenter.