← All Articles

Dive into reactive microservices with Eclipse Vert.x and deploy it with Cloud 66

Daniël van GilsDaniël van Gils
Jan 16th 18Updated Jul 6th 22
Ruby on Rails

microservices-with-vertx-and-cloud66

Cloud 66 supports all kinds of languages and frameworks.

This blog post will explore the world of Vert.X and how you can use this awesome framework to design, build and deploy your microservices with Cloud 66.

Let get our hands dirty!

Example code used in this blog post can be found on Github.

What is Vert.x

Eclipse Vert.x is event-driven and non-blocking. This means your app can handle a lot of concurrencies using a small number of kernel threads. Vert.x lets your app scale with minimal hardware.

Vert.x isn't opinionated and got a big list of components you can use in your Verticle , your service. All those cool components will speed up your development and make new microservices a breeze.

And on top of this, Vert.x support polyglot development. You can use Vert.x with multiple languages including Java , JavaScript , Groovy , Ruby , Ceylon , Scala and Kotlin.

Create your first microservice

A simple service called a Verticle in the Vert.x world, written in Java looks like this:

hello-verticle.java

import io.vertx.core.AbstractVerticle; public class Server extends AbstractVerticle { public void start() { vertx.createHttpServer().requestHandler(req -> { req.response() .putHeader("content-type", "text/plain") .end("Hello from Vert.x!"); }).listen(8080); } }

The same service using Ruby :

hello-verticle.rb

$vertx.create_http_server().request_handler() { |req| req.response() .put_header("content-type", "text/plain") .end("A ruby world full of gems!") }.listen(8080)

Time to run this guy.

Run it with Docker

Because we are going to focus on container-based Vert.x development using Docker, this is a Dockerfile to run the Ruby example. You can use the same Dockerfile for all the supporting programming languages. Easy.

# The base image FROM vertx/vertx3 # Set the name of the verticle to deploy ENV VERTICLE_NAME hello-verticle.rb # Set the location of the vertices ENV VERTICLE_HOME /usr/verticles # Run your Vertical on the default port EXPOSE 8080 # Copy your verticle to the container COPY $VERTICLE_NAME $VERTICLE_HOME/ # Launch the verticle WORKDIR $VERTICLE_HOME ENTRYPOINT ["sh", "-c"] CMD ["exec vertx run $VERTICLE_NAME -cp $VERTICLE_HOME/*"]

Build the verticle:

▶ docker build -t sample/vertx-ruby .

Run it:

▶ docker run -t -i -p 8080:8080 sample/vertx-ruby

The port is mapped to port 8080, point your browser to http://localhost:8080 and you run your first Verticle.

microservices-with-vertx-and-cloud66

Like magic!

Use the following for quick testing, no need for building a custom Docker image:

▶ docker run -i -t -p 8080:8080 -v $PWD:/usr/verticles vertx/vertx3 vertx run main-verticle.rb -cp /usr/verticles/

If you don't want to use Docker at this stage and installed the vertx CLI tool. You also can run:

▶ vertx run hello-verticle.rb

We are using the vertx/vertx3-exec base image ( 332MB ), but you can also use the vertx/vertx3-alpine base image ( 160MB ) for the same results:

▶ docker run -i -t -p 8080:8080 -v $PWD:/usr/verticles vertx/vertx3-alpine vertx run main-verticle.rb -cp /usr/verticles/

The differences are alpine linux is using musl libc and you need to check if the packages your are using are compatible with musl libc. Most packages are compatible.

Go small with alpine linux.

Deploy to Production using Cloud 66

With the Vert.X base image it easy to run your Verticle in production using Cloud 66.

microservices-with-vertx-and-cloud66

The only thing you need is a Dockerfile in your Git repo and Cloud 66 will build your image and deploy it on any public cloud or on your own servers. The first step is to bring in your source-code and let SkyCap build your Verticle images.

microservices-with-vertx-and-cloud66

Now the building is done, we can deploy it to production. Of course, we can build as manage microservices as we want and let Cloud 66 handle the builds and the deployments to any cloud (backed by Kubernetes).

microservices-with-vertx-and-cloud66

We deploy our Kubernetes cluster to Digital Ocean and map our port (8080) to the outside world on port 80 and 443. Good to know, we support SSL certificates and SSL termination to secure your services.

microservices-with-vertx-and-cloud66

microservices-with-vertx-and-cloud66

Nowe we hit deploy and Cloud 66 will do all the heavy lifting and deploy your Verticle in production!

microservices-with-vertx-and-cloud66

After Cloud 66 setup your infrastructure, your Verticle is running in production!

microservices-with-vertx-and-cloud66

Now you can explore all the cool features Cloud 66 has to offer! And start scaling your application.

Going as small as possible using Habitus

Of course, you want to reduce image size for security and fast deployment purposes. To achieve this, we need to cut out the middleman (Maven + Vert.x runtime) and go for the fat-jar deployment model.

To achieve this, we need a two-stage build pipeline, you can use Habitus. Habitus is a Build Flow Tool for Docker.

This is how our build.yml looks like:

build: version: 2016-03-14 # version of the build schema. steps: package: name: package dockerfile: Dockerfile.package artifacts: - /src/target/cloud66-client-example-0.1-fat.jar production: name: cloud66-client dockerfile: Dockerfile.production depends_on: - package

We have the package stage and the production stage. In the packaging stage, we use Maven to download our dependencies and pack everything in one Fat Jar. The Fat Jar artifact is copied into the smallest possible Java Runtime image to be executed in production.

This is the Dockerfile.package we use for packaging:

# our base maven image based on alpine and jdk 8 FROM maven:3.5-jdk-8-alpine # create our src folder ENV SOURCE_DIR /src RUN mkdir $SOURCE_DIR WORKDIR $SOURCE_DIR # selectively add the POM file COPY pom.xml $SOURCE_DIR # copy our Vertical sources COPY src $SOURCE_DIR/src # package the thing to generate a Fat Jar RUN mvn package

This is the Dockerfile.production we use for production:

# our java runtime based on alpine and jdk 8 FROM openjdk:8u92-jre-alpine # create our app folder WORKDIR /app # copy our packed Fat Jar COPY cloud66-client-example-0.1-fat.jar /app/cloud66-client-example-0.1-fat.jardocker # set the user as a non-root user USER daemon # run our vertical CMD java -jar cloud66-client-example-0.1-fat.jar

Now we can run habitus to build our image:

▶ habitus

After the build, you get an image named cloud66-client:latest and is ready to test locally or deploy it to production.

▶ docker run -it cloud66-client:latest

Of course, you can use Habitus with Cloud 66. Just use the following entries in service.yml.

services: my-vertical: git_url: http://github.com/your-nick/my-vertical.git git_branch: master use_habitus: true use_habitus_step: production

microservices-with-vertx-and-cloud66

Summary

Vert.X is a great project to build (micro) services fast in the language of choice. Together with Docker and Cloud 66 Skycap and Cloud 66 Maestro you can deploy with speed on any cloud.

Have Fun!

REFERENCE


Try Cloud 66 for Free, No credit card required