Vert.x and Spring – implementing fully functional microservices eco system in under 15 minutes.

Building Microservices was the final book covered by our company’s training program. I can strongly recommend reading it if you want to get familiar with the trendy topic of developing microservices and the different features of the architectural style itself. At the beginning of the book the author describes a few key benefits and one […]

by Boyko Dimitrov

March 7, 2017

4 min read

JAVA J2EE developer scaled 1 - Vert.x and Spring - implementing fully functional microservices eco system in under 15 minutes.

Building Microservices was the final book covered by our company’s training program. I can strongly recommend reading it if you want to get familiar with the trendy topic of developing microservices and the different features of the architectural style itself.

At the beginning of the book the author describes a few key benefits and one of them is technology heterogeneity.

“With a system composed of multiple, collaborating services, we can decide to use different technologies inside each one. This allows us to pick the right tool for each job, rather than having to select a more standardized, one-size-fits-all approach that often ends up being the lowest common denominator.”

 

Reading this quote, the developer in me started to whisper “Build something… Build something”, so I was wondering how can I rapidly setup microservices eco system with technologies used in my current project.

Core advantage of being a software engineer working for a startup company in the Silicon Valley is the ability to use cutting edge technologies. Recently we adopted Vert.x – ideal framework for creating light-weight, high-performance, microservices. My idea was to use it for creating simple autonomous software unit which gives information about itself.

The other tool which I have already used and feel quite comfortable working with is Spring Boot. It will help me to quickly setup infrastructure for discovering and registering the developed microservices. Let’s start!

I will use as a reference this very useful blog post.

Step 1. Clone https://github.com/spring-guides/gs-service-registration-and-discovery.git

Step 2. Go to complete/eureka-service folder and execute mvn spring-boot:run. This step will build and execute the discovery and registration service which is the backbone of our eco system. Open https://localhost:8761. This is the Eureka Service Registry application. For more information check https://github.com/Netflix/eureka

Step 3. Clone https://github.com/boykodimitroff/vertx-microservice.git and execute again mvn spring-boot:run. Other approach is to manually create the following classes in a previously generated Spring Boot project.(https://start.spring.io/)

The following class starts a Spring Boot application and automatically tries to register itself to the registration service which is already running at https://localhost:8761. This is accomplished with the @EnableDiscoveryClient annotation.

The method marked with @PostConstruct is related to the Vert.x framework and later will initialize HTTP server which will handle REST requests.

For more information about what verticle is check https://vertx.io/docs/vertx-core/java/#_verticles

/src/main/java/eu/dreamix/VertxApplication.java
package eu.dreamix;

import eu.dreamix.verticle.VertxServerVerticle;
import io.vertx.core.Vertx;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

import javax.annotation.PostConstruct;

@EnableDiscoveryClient
@SpringBootApplication
public class VertxApplication {

  @Autowired
  private VertxServerVerticle vertxServerVerticle;

  public static void main(String[] args) {
     SpringApplication.run(VertxApplication.class);
  }

  @PostConstruct
  public void deployServerVerticle() {
     Vertx.vertx().deployVerticle(vertxServerVerticle);
  }
}

VertxServerVerticle.java provides all the magic. The class initializes HTTP server which will expose REST resource at /info and will give us information about the application.

In order to disable the Spring Boot’s built-in Tomcat instance include spring.main.web-environment=false in the application.properties file.

For more information about the vert.x server and routes check https://vertx.io/docs/vertx-web/java/#_re_cap_on_vert_x_core_http_servers

/src/main/java/eu/dreamix/verticle/VertxServerVerticle.java
package eu.dreamix.verticle;

import eu.dreamix.config.ApplicationConfiguration;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.core.json.Json;
import io.vertx.ext.web.Router;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Component;

/**
* Vert.x verticle which initializes HTTP server and gives application information
* Created by bdimitrov on 2/27/17.
*/
@Component
public class VertxServerVerticle extends AbstractVerticle {

   @Autowired
   private ApplicationConfiguration applicationConfiguration;

   @Autowired
   private DiscoveryClient discoveryClient;

   @Override
   public void start() throws Exception {
       super.start();

       vertx.createHttpServer().requestHandler(router()::accept).listen(applicationConfiguration.httpPort());
   }
   private Router router() {
       Router router = Router.router(vertx);

       router.route("/info").handler(routingContext -> {

           HttpServerResponse response = routingContext.response();

           response.putHeader("Content-Type", "application/json");
           response.end(Json.encodePrettily(discoveryClient.getInstances(applicationConfiguration.applicationName())));
       });

       return router;
   }
}

Simple configuration class which reads property values populated in application.properties.

/src/main/java/config/eu/dreamix/config/ApplicationConfiguration.java
package eu.dreamix.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

/**
* Created by bdimitrov on 3/1/17.
*/
@Configuration
public class ApplicationConfiguration {

   @Autowired
   private Environment environment;

   public String applicationName() {
       return environment.getProperty("spring.application.name");
   }

   public int httpPort() {
       return environment.getProperty("server.port", Integer.class);
   }
}

Again execute mvn spring-boot:run and open the application at the configured httpPort.(In my case it was https://localhost:8080/info).
The last thing you should check is the Eureka dashboard(https://localhost:8761) which now should show the newly registered microservice.

 

 

Java Developer at Dreamix