Skip to content

Extend Base Services in Projects Part II with Spring Boot 2.3

Heiko Scherrer edited this page Aug 19, 2020 · 5 revisions

Before SpringBoot 2.3 we needed to run our services in a kind of extracted mode and attach new strategies, algorithms or extensions as JAR files to the classpath. Read more about this here in Part I

The feature of Layered Jars has been introduced in Spring Boot 2.3 and also custom buildpacks (Details see here...). This offers a better way to extend services similar we did bevor with an open extracted classpath now on OCI container level. So we build our base images with the standard layering like it is foreseen by Spring Boot and use those images as base images for our extensions. The CI server simply builds an extension as a JAR file and also an image that is based on the base service' image and copies the extensions JAR to the proper folder.

Example: Take the OpenWMS.org COMMON Service as a base with the following Dockerfile:

FROM adoptopenjdk/openjdk11-openj9:jre-11.0.7_10_openj9-0.20.0-alpine as builder
WORKDIR application
ARG JAR_FILE=target/openwms-common-service-exec.jar
COPY ${JAR_FILE} application.jar
RUN java -Djarmode=layertools -jar application.jar extract

FROM adoptopenjdk/openjdk11-openj9:jre-11.0.7_10_openj9-0.20.0-alpine
WORKDIR application
COPY --from=builder application/dependencies/ ./
COPY --from=builder application/spring-boot-loader/ ./
COPY --from=builder application/snapshot-dependencies/ ./
COPY --from=builder application/application/ ./
ENTRYPOINT ["java", "-Xshareclasses -Xquickstart -noverify", "org.springframework.boot.loader.JarLauncher"]

In the first upper part the application is extracted in the builder image. In the below part the builder image, with its extracted structure is taken as a base to run the application.

Now if we want to extend the COMMON Service with a custom strategy, like a Putaway algorithm, a new project for Putaway is created with the following Dockerfile:

FROM openwms/org.openwms.common.service:latest
ARG JAR_FILE=target/openwms-wms-putaway.jar
COPY ${JAR_FILE} BOOT-INF/lib/openwms-wms-putaway.jar
ENTRYPOINT ["java", "-Xshareclasses -Xquickstart -noverify", "org.springframework.boot.loader.JarLauncher"]

So here we take the (extracted) COMMON Service as the base image and copy the Putaway strategy JAR into the proper folder.

Advantage in contrast to solution Part I: Not on a builder level, here on container level

What is still not that nice:

  • This solution is kinda Decorator pattern. An image can only be extended by one strategy at a time. So if multiple strategies are required they must each extend the other.
  • Hard at development time. Because the extension has no Boot starter class and cannot simply be started. The starter is still in the base image.