Event bus services - Porfolio service
In this scenario, we are going to implement an event bus service. A
Portfolio stores the owned shares and the available cash.
1. Initialize katacoda environment
All the scenarios in this lab are sequential and build on each other. The portfolio microservice being developed in this scenario is dependant on the quote generator microservice that should have been built and running on the OpenShift Container Platform already. That is what the script running in the terminal is doing. It is:
- Cloning the source code
- Initialize the OpenShift environment
- Build and deploy the quote-generator scenario
- Build and deploy the micro-trader-dashboard
2. Introduction - RPC and Async RPC
Microservices are not only about REST. They can be exposed using any types of interactions, and Remote Procedure Calls is one of them. With RPC, a component can effectively send a request to another component by doing a local procedure call, which results in the request being packaged in a message and sent to the callee. Likewise, the result is sent back and returned to the caller component as the result of the procedure call:
Such interactions has the advantages to introduce typing, and so is less error-prone than unstructured messages. However, it also introduces a tighter coupling between the caller and the callee. The caller knows how to call the callee:
- how the service is called
- where the service is living (location)
The AsyncResult notifies the Handler whether the invocation succeeded or failed. Upon success, the handler can retrieve the result.
Such async-RPC has several advantages:
- the caller is not blocked
- it deals with failures
- it avoids you to send messages on the event bus and manages object marshalling and unmarshalling for you.
MicroTrader Part 2: Portfolio Service
Async service interface
To create an async RPC service, or event bus service, or service proxies, you first need a Java interface declaring the async methods.
io.vertx.workshop.portfolio.PortfolioService class by clicking the link:
The class is annotated with:
ProxyGen- enables the event bus service proxy and server generation
VertxGen- enables the creation of the proxy in the different language supported by Vert.x
Let’s have a look at the first method:
void getPortfolio(Handler<AsyncResult<Portfolio>> resultHandler);
This method lets you retrieve a Portfolio object. This method is asynchronous and so has a Handler parameter receiving an AsyncResult
NOTE You may have also noticed that the package has a package-info.java file. This file is required to enable the service proxy generation.