Difficulty: beginner
Estimated Time: 15 minutes

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:

  1. Cloning the source code
  2. Initialize the OpenShift environment
  3. Build and deploy the quote-generator scenario
  4. 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:

Architecture

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:

  1. how the service is called
  2. where the service is living (location)

Architecture

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.

Summary

In this scenario, you covered

  • how we can use the event bus to implement asynchronous RPC (RPC done right)

  • how to implement a service proxy using callbacks

  • how to publish messages on the event bus

  • how to use the Vert.x Web Client

Don’t stop now! The next scenario will only take about 10 minutes to complete.

Eclipse Vert.x on RHOAR

Step 1 of 4

Step 1

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.

Open the io.vertx.workshop.portfolio.PortfolioService class

portfolio-service/src/main/java/io/vertx/workshop/portfolio/PortfolioService.java

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. The other methods follows the same pattern.

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.

Terminal
Local Web Browser
OpenShift Console