navigation

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

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

by
March 7, 2017

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 http://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.(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 HTTP server which will handle REST requests.

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

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

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 http://vertx.io/docs/vertx-web/java/#_re_cap_on_vert_x_core_http_servers

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

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

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

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.

 

 

Boyko Dimitrov

Java Developer at Dreamix

More Posts - Website

Follow Me:
TwitterLinkedInGoogle Plus

Do you want more great blogs like this?

Subscribe for Dreamix Blog now!

  • Ashish Chande

    Did you integrate Spring Oauth2 with vertx. Suppose I have spring ecosystem and only of the microservice uses vertx and I want this vertx microservice to be a resource server, how do I configure that

    • Boyko Dimitrov

      Hi Ashish,

      Thank you for your comment!

      Do you have strict requirement to use Spring OAauth2? If not, check http://vertx.io/docs/vertx-auth-oauth2/java, since the framework provides it’s own implementation for OAuth2.

      Greetings