Creating the source code

I have installed and configured everything necessary to create the microservice to manage the registry of players. Now it's time to write the code needed to expose our microservice APIs.

I use the Vert.x project generator utility, http://start.vertx.io/ , in order to get the project skeleton to work on. The microservice, as described earlier, will have to display APIs that allow us to perform CRUD operations.

To implement the microservice, I will use the following components:

  •  Core:  As the name suggested it contains the core  functionalities as the support for HTTP
  •  Config: Provides an extensible way to configure Vert.x applications
  •  Web: Toolkit that contains utilities required to build web applications and HTTP microservices
  •  JDBC client: This component enables developers to communicate with any JDBC-compliant database but with a different approach, the asynchronous API

We will use com.packtpub.vertx as the Maven Group name of the project, and footballplayermicroservice as the Artifact. Set these values in the project form generator as depicted in the following screenshot:

Click Generate Project to create and download a ZIP file with the project skeleton.

Unzip the file into a directory of your choice and open the Maven project with your favorite IDE (Eclipse, NetBeans, IntelliJ, and so on).

The core element of the project is the Maven pom.xml file, which contains all the dependencies needed to implement our microservice.

The project uses vertx-stack-depchain to correctly manage dependencyManagement:

<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-stack-depchain</artifactId>
<version>${vertx.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

We also need to add the PostgreSQL JDBC driver dependency in the Maven pom.xml file in order to connect to the database used to store the football player information:

<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4.1212</version>
</dependency>

Now it's time to launch the first build with this command:

$ mvn clean package

After that, you can test that your application is up and running with the following command:

$ java -jar $PROJECT_HOME/footballplayermicroservice/target/footballplayermicroservice-1.0.0-SNAPSHOT-fat.jar

Here, the $PROJECT_HOME variable is the path where you have to unzip the project generated by the Vert.x project generator utility. You will visualize an output like this:

HTTP server started on http://localhost:8080
Oct 11, 2018 10:41:06 AM io.vertx.core.impl.launcher.commands.VertxIsolatedDeployer
INFO: Succeeded in deploying verticle

Put the http://localhost:8080 URL into your browser, and you will see this message: Hello from Vert.x!

Now let's stop our application using the Ctrl + C command, and start to update our project.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset