Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Getting Started With Vert.x and Spring in Under 15 Minutes

DZone's Guide to

Getting Started With Vert.x and Spring in Under 15 Minutes

With tools like Spring Boot and Vert.x, creating a microservices ecosystem is a piece of cake. See how to do it in less than 15 minutes.

· Java Zone
Free Resource

Learn how to troubleshoot and diagnose some of the most common performance issues in Java today. Brought to you in partnership with AppDynamics.

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 set up a microservices ecosystem with technologies used in my current project.

The core advantage of being a software engineer and working for a startup company in Silicon Valley is the ability to use cutting edge technologies. Recently, we adopted Vert.x – an ideal framework for creating lightweight, high-performance microservices. My idea was to use it for creating simple autonomous software units that give me information about themselves.

The other tool I already use and feel quite comfortable working with is Spring Boot. It will help me to quickly set up my 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 the 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 ecosystem. Open http://localhost:8761. This is the Eureka Service Registry application. For more information, check out https://github.com/Netflix/eureka.

Step 3: Clone https://github.com/boykodimitroff/vertx-microservice.git and, again, execute mvn spring-boot:run. Another approach is to manually create the following classes in a previously generated Spring Boot project. (http://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 http://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 an HTTP server that will handle REST requests.

For more information about what a verticle is, check here: http://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 the HTTP server, which will expose REST resources 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 out: http://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;

@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;
   }
}


That's a simple configuration class that 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;

@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 http://localhost:8080/info).

The last thing you should check is the Eureka dashboard (http://localhost:8761), which now should show the newly registered microservice.

Understand the needs and benefits around implementing the right monitoring solution for a growing containerized market. Brought to you in partnership with AppDynamics.

Topics:
microservices ,vert.x ,spring boot ,tutorial ,java

Published at DZone with permission of Boyko Dimitrov, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}