WildFly Swarm Microservice Development: JAX-RS App Deployed to a Docker Container
WildFly is a neat approach to microservices. Don't need the entire Java EE stack? WildFly Swarm gives you more control.
Join the DZone community and get the full member experience.Join For Free
wildfly swarm is an interesting approach to the ‘small is good’ current trend that is ignoring the traditional, overly large java ee app servers, and instead deploying self-contained, executable jars without needing a whole app server to execute.
rather than requiring the whole java ee stack, wildfly swarm lets you chose only the implementations of the parts that your service needs to execute. need jax-rs? then pull it in via a maven dependency. need jpa? pull that in too. any other required transitive dependencies are automatically pulled in via maven. pulling in jax-rs also requires a servlet container and apis, but this is pulled in for you via maven transitive dependencies. the end result is a self-contained, packaged jar that contains everything it needs to run with a ‘java -jar’ command.
i’ve been spending some time recently looking at getting weblogic portal 10.3.6 running in a docker container (check my work in progress changes on github here ). as ee containers go, it’s big. it’s heavy. if you want to describe anything as a monolith, then this is your perfect example. so switching gears i wanted to go to the other extreme and look at how you would build a lightweight java based service, and wildffly swarm looked pretty interesting.
i attended one of the sessions at javaone this year giving an intro to swarm, so it’s been on my todo list to take a look.
getting started with swarm is actually pretty easy, as it’s all driven by maven dependencies and plugins. there’s an easy to follow tutorial here . the example apps showing different wildfly components packaged using swarm are also worth a look here .
i worked through the examples putting together a jax-rs helloworld app, and also a dockerfile to package and deploy it to a docker container. it was actually pretty easy, and my app ended up looking much like the provided examples.
my example jax-rs resource is pretty simple, nothing complicated here:
if you’re looking for the different wildfly services that you can package with swarm, browsing the mvnrepository is a good place to start to quickly grab the mvn deps, or browse the examples or source .
for the maven war and wildfly-storm plugins:
<build> <plugins> <plugin> <groupid>org.apache.maven.plugins>/groupid> <artifactid>maven-war-plugin>/artifactid> <configuration> <failonmissingwebxml>false</failonmissingwebxml> <packagingexcludes>web-inf/lib/wildfly-swarm-*.jar>/packagingexcludes> </configuration> </plugin> <plugin> <groupid>org.wildfly.swarm</groupid> <artifactid>wildfly-swarm-plugin</artifactid> <executions> <execution> <goals> <goal>package</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
next, adding a dependency for the wildfly swarm jax-rs:
<dependency> <groupid>org.wildfly.swarm</groupid> <artifactid>wildfly-swarm-jaxrs</artifactid> <version>1.0.0.alpha5</version> </dependency>
to build the self-contained executable jar, build with ‘mvn package’ – this will build the app as normal to target, but also include a *-swarm.jar – this the self-contained jar containing all the wildfly dependencies, and can be run standalone with ‘java -jar helloworld-swarm-0.0.1-snapshot-swarm.jar’
building a docker container
given that we’ve now got a simple, single, self-contained jar, deploying this into a docker container is also pretty easy as we have no other dependencies to worry about (we just need a jvm).
an example dockerfile would look like (scroll to the right):
from java:openjdk-8-jdk add target/helloworld-swarm-0.0.1-snapshot-swarm.jar /opt/helloworld-swarm.jar expose 8080 entrypoint ["java", "-jar", "/opt/helloworld-swarm.jar"]
all we’re doing here is:
1. creating an image based on the official openjdk image from dockerhub
2. adding our built wildfly swarm jar containing our app into /opt in the image
3. exposing port 8080
4. defining the entrypoint that executes when the container starts
to create an image based on this dockerfile:
docker build -t helloworld .
to start a container based on this image:
docker run -d -p 8080:8080 imageid
… this creates a new container based on the new container, runs it as a daemon, with port 8080 in the container exposed to 8080 on the host. done!
hit the url of our endpoint using the ip of the running docker-machine:
to watch the logs of the running container, do:
docker logs -f containerid
in this case you see the output as the wildfly undertow servlet container starts up, and initializes our jax-rs based app:
pretty simple! we’ve got a minimal wildfly server starting up inside a docker container in about 10s, and our app deploying in 2s. that’s pretty good if you ask me.
Published at DZone with permission of Kevin Hooke, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.