To view the preview page please CLICK HERE.
This Project represents a small Microservice App using Eclipse Vert.x and Hazelcast In-Memory Data Grid.
Eclipse Vert.x is a tool-kit for building reactive asynchronous applications on the JVM. Reactive applications are both scalable as workloads grow, and resilient when failures arise.
The project is a Maven project, written by Java language and includes 2 modules that will be communicate with Vert.x Event Bus, the two modules are:
- RestVerticle module
- OrderVerticle module
The communication between the two verticles will be by using Vert.x Event Bus.
The Application will be running in cluster mode by using Hazelcast In-Memory Data Grid (IMDG)Open Source.
The maven package will be generating a docker-compose YAML that will contain two containers for the 2 verticles.
Language & Framework: Java, Maven
Asynchronous Tools: Eclipse Vert.x
In-Memory Data Grid (IMDG): Hazelcast
Architecture & Design Patterns: Microservice Application, Reactive Application
Client-Side UI: HTML, CSS, Bootstrap 5
🛠 This java module will be a Vert.x verticle and will contain 2 classes:
main(String[] args)
– To run the application in a cluster mode, the main class will use Vert.x implementation of Hazelcast as a cluster manager
getAddress()
– This method will use the NetworkInterface to locate and filter the IP addresses.
The relevant IP address will be sent back to the main method.
🛠 First, we will start by creating HTTP Server and Router:
start(Promise<Void> startPromise)
– This method starts an HTTP server.
The method create a Vert.x HTTP server and then handle requests using the router generated from the createRouter() method (listening to port 8080).
createRouter()
– This method creates a Vert.x Web Router (the object used to route HTTP requests to specific request handlers). The method will return Vert.x Web Router.
🛠 Then, this class will expose a REST API (using Vert.x) with 5 REST methods:
GET: greetingHandler(RoutingContext context)
– This method greeting the user at the main endpoint.
POST: loginHandler(RoutingContext context)
– This method will use the RoutingContext interface to get the username and password from the user, and check if the user can be logged in (username and password will be saved in local JSON file). In the background the module should open a session for each user that logged in.
POST: logoutHandler(RoutingContext context)
– This method will be used to log out from the user session, his session will be destroyed.
POST: addOrderHandler(RoutingContext context)
– This method will add an order to the user.
By using Vert.x Event Bus, the order will be sent to the OrderVerticle module.
GET: getOrdersHandler(RoutingContext context)
– This method will return all user orders.
The request to get the data will be sent by Vert.x Event Bus to the OrderVerticle module.
🛠 Extra methods used in this class to support those REST methods:
sessionAuth(RoutingContext context)
– Helper method to check if users session is permitted.
contextResponse(RoutingContext context, String errorValue, String loginValue, Integer httpStatus)
– Helper method to print error values in case one of the endpoints collapse, or get runtime error.
This method used in other methods exist in this java class, I added it for clean code. 😊
🛠 This java module will be a vert.x verticle and will contain 2 classes:
main(String[] args)
– To run the application in a cluster mode, the main class will use Vert.x implementation of Hazelcast as a cluster manager. This method will generate the hazelcast configuration and set the destination address.
getAddress()
– This method will use the NetworkInterface to locate and filter the IP addresses.
The relevant IP address will be sent back to the main method.
🛠 First, by using Vert.x Event Bus, we will manage requests that received:
start(Promise<Void> promise)
– This method use Verte.x Event Bus to manage requests received from the RestVertical module. The Event Bus will direct each request to the relevant method.
addOrder(Message<Object> message, String orderId, String orderName, String orderDate)
– This method add new orders to the user existing orders. All the data will be saved in a local JSON file and include: orderID, orderName and orderDate. The response will be sent to the OrderVerticle module.
getOrders(Message<Object> message)
– This method will return all user orders.
The response will be sent to the OrderVerticle module.
🛠 Extra method used in this class to support other methods:
messageResponse(Message<Object> message, String errorValue, String insertValue)
– Helper method to print error values in case one of the endpoints collapse, or get runtime error.
This method used in other methods exist in this java class, I added it for clean code. 😊
👉 To run this Microservice properly, you should first run RestVerticle Module, and then the OrderVerticle Module.
http://localhost:8080
Parameter | Type | Description |
---|---|---|
context |
RoutingContext |
Required. The user context |
http://localhost:8080/login
Parameter | Type | Description |
---|---|---|
context |
RoutingContext |
Required. The user context |
http://localhost:8080/logout
Parameter | Type | Description |
---|---|---|
context |
RoutingContext |
Required. The user context |
http://localhost:8080/add-order
Parameter | Type | Description |
---|---|---|
context |
RoutingContext |
Required. The user context |
http://localhost:8080/get-orders
Parameter | Type | Description |
---|---|---|
context |
RoutingContext |
Required. The user context |
Thanks for reading,
Chelly 👩🏻💻