Skip to content

Build a micro service application with Vertx: a complete tutorial (part 5/5)

In this series of posts, I would teach you, how to create a complete reactive micro service application with Vert.x and Java, connect it with your data source, test and deploy on Heroku platform.

Table of contents

You can find a full source code for this tutorial on my github. Feel free to clone it:

git clone https://github.com/mednikovnet/vertx-microservices-tutorial

Before we start

In order to deploy the application on Heroku PaaS, you should have an account and Heroku-CLI installed and up and running. I would not focus on this, as it can vary based on your system, as well it can be “changed without prior notification”. Please refer to Heroku docs on how to create an account and install Heroku CLI, in the case, you don’t have them.

Type following command in your terminal (or its Windows equivalent):

heroku --version

You should receive a similar result (you can have an other version as well an other terminal):

Now you can login to Heroku:

heroku login

Step 1. Put all components together

We built a micro service application throughout this series, and our application consists of two Verticles: BikeService and AppEndpoint. First we need to connect them. Inside AppEndpoint.java in main() method add following code snippet:

public static void main(String[] args){
	Vertx vertx = Vertx.vertx();
	vertx.deployVerticle(new AppEndpoint());
	vertx.deployVerticle(new BikeService(BikeDaoImpl.getInstance()));
}

Now we have all app’s components connected together. You can run an app and assert that everything is OK.

Step 2. Listen a correct port

Heroku assigns a port to your app dynamically, so we need to get a correct port, that was assigned to our app. It is done with environment variables. Check this tutorial, if you are not familiar with them. Heroku provides the PORT variable with port number, in this way we just need to get a value of this variable. Inside AppEndpoint constructor add following code:

public AppEndpoint(){
	String herokuPort = System.getEnv("PORT");
	if (herokuPort !=null){
		this.port = Integer.valueOf(herokuPort);
	} else {
		this.port = 4567;
	}
}

NB!: You can simplify this code with ternary operator.

Step 3. Create a fat jar

The final step here is to create a fat jar. A fat jar (or it is also known as uber-jar) is a self-sufficient archive which contains both classes and dependencies needed to run an application. With Maven we can use Maven Shade plugin to build a fat jar easily. In your Pom.xml add this code inside <project> tag:

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.2.1</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <transformers>
                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                  <mainClass>net.mednikov.BikeShare.app.AppEndpoint</mainClass>
                </transformer>
              </transformers>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

Nevertheless it is a bit complex construction on the first glance, this is very easy. Now navigate to root directory of our application and execute this command in your terminal:

mvn package

At this moment you would have two files inside target folder:

  • bikeshare.jar – this is a fat jar – This is what we should deploy
  • original-bikeshare.jar – only your project classes

Also, ignore dependency-reduced-pom.xml file. It is just for reference. If you have any questions, don’t hesitate to ask them in comments or message me on Twitter. I also advise you to check this tutorial on fatjars.

Step 4. Create a new Heroku application

Create a new application on Heroku – choose an app name and a region of deployment (due to my EU location, I selected Europe):

NB!: Don’t forget to install a Java plugin using CLI:

heroku plugins:install java

Step 5. Deploy

Inside target folder execute the following command:

heroku deploy:jar bikeshare.jar --app bike-share-tutorial

NB!: The value of the app parameter depends on your application name.

As we use an environment variable DATABASE_URL to provide a URL to the DAO, you need to specify it. Put a JDBC URL to Config vars. Navigate to Settings tab and press Reveal config vars button. Than add DATABASE_URL value:

Conclusion

In this series of tutorials we have created a complete micro service application with Vert.x and Java. We used Endpoint-Service-DAO architecture, connected our application to a real MySQL remote database and deployed it on Heroku.

You can get a full source code on Github. In case you have questions, don’t hesitate to ask them either in comments or send me DM in Twitter: @andreevi_ch This tutorial would be updated. To keep your up to date with new improvements, don’t forget to follow my social accounts.

You can find a full source code for this tutorial on my github. Feel free to clone it:

git clone https://github.com/mednikovnet/vertx-microservices-tutorial

Good luck to you with Vert.x!