How to Create Microservices Using http4k
Learn how to create a simple microservice-based application using http4k, Kotlin, and JDBI.
Join the DZone community and get the full member experience.Join For Free
In this article, we will build a simple microservice based on Kotlin, the http4k toolkit , and JDBI.
Our Use Case
Let's consider a simple functionality: registering companies and generating a ranking system. Each company has a score based on their foundation date. Companies are classified by their score, so we have a simple solution, as described below:
The above diagram shows a simple service with it own database. We are assuming that the Ranking Web (UI) Application will interact with Company Service to show the company list. Let's take a look at some design points:
- Language/Toolkit: We are using the http4k toolkit. This toolkit provides a simple and uniform way to serve, consume, and test HTTP services. It was entirely written in Kotlin and brings with it the concept of "function as service." http4k provides many server runtimes, such as Netty, Apache, Ktor, etc.
- Persistence: all data will be managed via JDBI. JBDI is a library that provides an idiomatic access to relational data in the Java world. All data will be stored in a PostgreSQL database.
- Testing: All services need to be tested and require a lot of testing. To create our unit tests, we are using Kotest and JUnit. Kotest has an amazing DSL for testing, making our tests more fluent.
We will start from the repository, ranking-service. All boilerplate code is provided in this initial repository. Let's build the registration operation. First, when we create a company record, we need to set the company's score in our ranking system. The score calculation is based on its foundation date, so we have some rules for that:
- Companies less than 2-years-old cannot be included in our rankings.
- Companies between 2- and 3-years-old have a score of "C".
- Companies between 4- and 5-years-old have a score of "B".
- Companies that are 6-years-old or more have a score of "A".
Once the company is registered, we can perform an evaluation using the following:
Those values must be cumulative, and only allowed to go up, not down, based on votes. The company table should look like:
Our entity's code looks like the following:
Our service must provide three operations, as shown in the table below:
Register a New Company
Now we need to implement our company registration functionality. Let's implement our HTTP handler that represents the first operation shown in the table above. First, let's see the code below:
Inside the Router.kt file, we're defining a function,
registerHandler, that receives a service as an argument. http4k defines a Handler like a function: (Request) → Response. Now, let's see the
register function from the
The function above implements the business logic to register a new company. Basically, we validate the foundation date and check if another company with the same site exists. Our service depends on
CompanyRepository, which implements a persistence contract with the database. Let's check the code for the repository's implementation:
The implementation uses a helper class,
Database, which abstracts the boilerplate code used by JDBI under the hood. The function
getSql() loads external files which contain SQL statements. Our functionality for company registration has been completed with two persistence functions.
Once our registration operation is complete, we can build the company listing functionality to display our Rankings page.
As you can see in code below, our handler defines a body lens that will returns a list of based on
RankingResponse. The lens concept is a functional paradigm used by http4k that can read or get a value from an object passed by an argument through the lens. The code for the service is really simple, as we can see below:
The service basically transforms an entity into a Model object, which will be serialized in response. Now, let's see the repository function to fetch companies.
Let's finish our simple service by implementing a Voting operation. Our vote handler must look like:
Now let's see the
vote function from the
CompanyService class. It has a simple logic that identifies which type of vote we are performing and updates the counter.
The repository implementation is very straightforward, as we can see below:
I hope this little tutorial has shown you the power and simplicity that http4k brings to us. Creating services is really simple and enjoyable using http4k and in the future we will explore more features. If you want a deep dive into more of http4k, take a look at the many examples and documentation from the http4k website. The complete code of this tutorial can be found at https://github.com/keuller/tutorial-ranking-service. See you next time!
Opinions expressed by DZone contributors are their own.