Dockerizing Java MicroProfile Applications
Take a look at how Dockerizing and using MicroProfile adds additional functionality to your cloud-native applications.
Join the DZone community and get the full member experience.Join For Free
for cloud-native applications, kubernetes and istio deliver a lot of important functionality out of the box, like ensuring resiliency and scalability. this functionality works generically for microservices, no matter in which language they have been implemented and independent from the application logic.
some cloud-native functionality, like application-specific failover functionality, metrics, and fine-grained authorization, cannot be handled by kubernetes and istio, since it needs to be handled in the business logic of the microservices.
that's why i started to look into eclipse microprofile which is an extension to javaee to build microservices-based architectures and a great programming model for istio . in addition to the application-specific logic that istio cannot handle, it also comes with convenience functions that you typically need when developing microservices, like invoking rest apis and implementing rest apis including their documentation.
get the code of the cloud-native starter sample.
the first thing you need to run microprofile applications on kubernetes is an image. microprofile is supported by several java application servers, different jvms can be used and there are different versions of all of these components. because of this, there are many different images you need to choose from.
from openliberty/open-liberty:microprofile2-java8-openj9 add https://github.com/wasdev/sample.opentracing.zipkintracer/releases/download/1.2/liberty-opentracing-zipkintracer-1.2-sample.zip / run unzip liberty-opentracing-zipkintracer-1.2-sample.zip -d /opt/ol/wlp/usr/ \ && rm liberty-opentracing-zipkintracer-1.2-sample.zip copy liberty/server.xml /config/ add target/articles.war /config/dropins/
the image contains these components:
there are several different images available for open liberty. i picked a community image since it comes with openj9 instead of the ibm version of openj9. unfortunately, that image doesn't seem to support microprofile 2.2 yet (at least i haven't found it).
additionally, i download and copy a file needed for zipkin tracing onto the image which you need to do manually at this point if you want to use the tracing functionality built into microprofile. this functionality is pretty useful since it allows you to see the chains of invocations between microservices.
this screenshot shows the jaeger dashboard. the bff (backend for frontend) 'web-api' microservice invokes another 'articles' service:
variations of the dockerfile
in order to avoid downloading the zipkin file every time a new image is built, i've created a slightly different dockerfile where the file is added from a local directory. the image is built with a script that downloads the file if it doesn't exist locally. alternatively, you can download the file via maven (check out the example pom.xml and example dockerfile ).
additionally, i have created another variation of the docker image so that my sample application can be installed even by people who don't have java and maven installed locally (or who have wrong java/maven versions). this dockerfile uses a multistage build .
from maven:3.5-jdk-8 as build copy src /usr/src/app/src copy pom.xml /usr/src/app run mvn -f /usr/src/app/pom.xml clean package from openliberty/open-liberty:microprofile2-java8-openj9 add liberty-opentracing-zipkintracer-1.2-sample.zip / run unzip liberty-opentracing-zipkintracer-1.2-sample.zip -d /opt/ol/wlp/usr/ \ && rm liberty-opentracing-zipkintracer-1.2-sample.zip copy liberty/server.xml /config/ copy --from=build /usr/src/app/target/articles.war /config/dropins/
$ git clone https://github.com/nheidloff/cloud-native-starter.git $ scripts/check-prerequisites.sh $ scripts/deploy-articles-java-jee.sh $ scripts/deploy-web-api-java-jee.sh
Published at DZone with permission of Niklas Heidloff, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.