DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Distributed Tracing System (Spring Cloud Sleuth + OpenZipkin)
  • Using Kong Ingress Controller with Spring Boot Services
  • Component Tests for Spring Cloud Microservices
  • Migration of Microservice Applications From WebLogic to Openshift

Trending

  • Build a Simple REST API Using Python Flask and SQLite (With Tests)
  • Monolith: The Good, The Bad and The Ugly
  • How to Create a Successful API Ecosystem
  • Event-Driven Microservices: How Kafka and RabbitMQ Power Scalable Systems
  1. DZone
  2. Software Design and Architecture
  3. Microservices
  4. Getting Started With Vert.x and Spring in Under 15 Minutes

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.

By 
Boyko Dimitrov user avatar
Boyko Dimitrov
·
Mar. 12, 17 · Tutorial
Likes (11)
Comment
Save
Tweet
Share
31.3K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Spring Framework Vert.x microservice Spring Boot

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

Opinions expressed by DZone contributors are their own.

Related

  • Distributed Tracing System (Spring Cloud Sleuth + OpenZipkin)
  • Using Kong Ingress Controller with Spring Boot Services
  • Component Tests for Spring Cloud Microservices
  • Migration of Microservice Applications From WebLogic to Openshift

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!