Java EE · Programming

Microservices deployment:Wildfy Swarm and Docker

Introduction

In the new era of microservices a key component for deploying them are the so called microcontainers.Currently the best kid in town for it is a software tool named Docker. Docker is based on the Go programming language and offers an elegant way to define lxc(linux containers). Shifting towards deployments on PaaS, the usage of docker containers becomes a need. In this small howto we will describe how you can build a docker container using maven plugin and handle exactly the flow of your development as you used to. The only difference will be that you will be creating a docker container you can use. The new kid in town is called fat-jars. Last  week I have been playing with the 3 major players:Wildfly Swarm,Payara-Pico,Spring-boot.

Fat jars are actual EE application packaged as jars than can be executed with java -jar command in the command line. This concept even if looks like new actually is well known. You programmatically bootstrap the appserver where the war should be deployed. People working with the RedHat supported products and in the JBoss world are familiar with Shrinkwrap and Arquillian for testing EE apps. Not that far from the mentality what Wildfly Swarm brings into the foreground

Wildfly Swarm builds on top of the WildflyAS 9.0 appserver where the architecture design of the appserver has dramatically changed. We welcome the new kid in town with a small howto / article on how it’s target the microservices world can help up improve and simplify our deployment models.

Prerequisites

In order to work with this tutorial you need:

  • Java IDE of choice:IntelliJ||Netbeans||Eclipse
  • JDK 1.7: Oracle Java Development Kit 1.7
  • Maven 3.3.* : The swiss army knife of java development
  • docker-maven-plugin: Spotify team maven plugin for integration with docker.
  • Wildfly swarm : Fat jars based on WildflyAS 9.0

Maven project configuration

In order to use the docker maven plugin from Spotify you simply need to reference the plugin. The configuration:

 

<plugin>
 <groupId>com.spotify</groupId>
 <artifactId>docker-maven-plugin</artifactId>
 <version>${docker.spotify.version}</version>
  <configuration>
   <baseImage>jboss/wildfly</baseImage>
   <imageName>cdi-events</imageName>
   <maintainer>"Nikolaos Ballas mpallas@gmail.com"</maintainer>
    <resources>
      <resource>
       <targetPath>/</targetPath>
        <directory>${project.build.directory}</directory>
        <include>${project.build.finalName}-swarm.jar</include>
       </resource>
      </resources>
  </configuration>
</plugin>

Some brief information.You describe the base image of your docker container to be create and then you simply pass parameters on which files need to be copied.In our case we copy simple the fat jar inside the docker container.Word of caution, this will copy the jar in the $HOME path of the container as described by the definition of each base image.In order to invoke the build for docker container creation please simply append in your maven build command the switch: docker:build. The actions supported are stated in the site.The coolest part is that you can directly push in your registry.

Wildfly Swarm

This cool tool will create the fat jar file which will be “deployed” in your container.To enable swarm in your project you need mostly two stuff:

  • The maven dependencies.
  • A class with a main function which then you have to enable in the MANIFEST-MF file

First step configuration of the swarm maven plugin :

 <plugin>
                <groupId>org.wildfly.swarm</groupId>
                <artifactId>wildfly-swarm-plugin</artifactId>
                <version>${version.wildfly-swarm}</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>create</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

In our case since we are using JEE7 and the features of CDI,JAX-RS in WildflyAS we need to write a minimal class that bootstraps the container parts we want.The source code of the class is so simple as shown below:

package com.dummy.cdi.events.swarm;

import com.dummy.cdi.events.MyApplication;
import org.wildfly.swarm.container.Container;
import org.wildfly.swarm.ee.EeFraction;
import org.wildfly.swarm.jaxrs.JaxRsDeployment;
import org.wildfly.swarm.jaxrs.JaxRsFraction;

public class ApplicationStarter {

    public static void main(String[] args) throws Exception{
        new Container().subsystem(new JaxRsFraction())
                        .subsystem(new EeFraction())
                        .start();
    }
}

Now some explanations. The typical main function of a standalone executable class in Java. The tricky part comes on the definition of the container.We explicitly define that actually we need the fraction of the application server that contains the JaxRS subsystem the EeFraction.The last method implies that when we will java -jar the jar file a new container instance will be started.

Conclusions

As you can see from the simple example presented here, the above environment and tools allow you to define a full microservices architecture based on docker and wildfly swarm.Also the usage of maven allows you to automate the build of the images and can easily be integrated to your CDI environment.I firmly believe that this will be the feature way of developing micro architectures in the cloud. Small services in containers that we can stop/start really easy.

The code

The code is available on Github under the branch wildfly-swarm, on my cdi-event repository.

Hope you enjoy!

Happy javing!!!!!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s