Professional Documents
Culture Documents
YouTube - Eclipse Vert.x-Building Reactive Microservices in Java-Chapter 04
YouTube - Eclipse Vert.x-Building Reactive Microservices in Java-Chapter 04
x
Chapter 04
Message Based Microservices
Firmansyah.profess@gmail.com
2018
00. HTTP Main Issues
a. Reactive Microservices must be:
• Autonomous
• Asynchronous
• Resilient
• Elastic
b. HTTP based microservices as demonstrated on chapter 03 is not
Reactive Microservices, because it does not provide the resilience and
elasticity.
c. It does not provide the resilience, because if the first microservice
(HttpMicro01 class) fails, we won’t be able to recover by calling
another one.
d. It does not provide the elasticity, if we are under load, creating a new
instance of the HttpMicro01 won’t help us, because HttpMicro02 is
configured to target the HttpMicro01 microservice explicitly.
e. HTTP based microservices can be Reactive Microservices if we
provide some infrastructure to route virtual URLs to a set of service
and a load-balancing strategy to provide elasticity and
health-check support to improve resilience.
00. Message Based MSA
a. The Vert.x Event Bus is messaging backbone for creating Message
Based Microservices Architecture (MSA) that allowing the different
components of an application to interact using messages.
b. Messages are sent to addresses and have a set of headers and a body.
An address is an opaque string representing a destination. Message
consumers register themselves to addresses to receive the messages.
c. The Vert.x Event Bus is also clustered, meaning it can dispatch
messages over the network between distributed senders and
consumers. nodes are connected to enable shared data structure, hard-
stop failure detection, and load-balancing group communication.
d. The Vert.x Event Bus provides three types of delivery semantics:
1. The send method: allows a component to send a message to an
address. If more than one consumer is registered on this address,
Vert.x applies a round-robin strategy to select a consumer.
2. The publish method to deliver the message to all consumers
registered on the address.
3. The send method with a reply handler. This request/
response mechanism allows implementing message-based
asynchronous.
00. Sequence Diagram
We will create two verticle classes with following
sequence diagram to demonstrate Message
based Microservices
MsgMicro02 MsgMicro01
HTTP Request
Address: “AdrMsg”, Message:”Firmansyah”
HTTP Response
01. Create MsgMicro01 Class
a. Create a directory called “chapter04”
b. Generate the project structure using maven
inside chapter04 folder:
mvn io.fabric8:vertx-maven-plugin:1.0.5:setup \
-DprojectGroupId=io.vertx.chapter04 \
-DprojectArtifactId=msg-micro01-vertx-app \
-Dverticle=io.vertx.chapter04.MsgMicro01 \
-Ddependencies=infinispan
This command generates:
1. The Maven project structure,
2. Configures the vertx-maven-plugin, and
3. Creates a verticle class (io.vertx.chapter04.MsgMicro01),
4. Adds the Infinispan dependency, an in-memory data grid that will
be used to manage the cluster. Once generated, we may
need to configure Infinispan to build the cluster. The
default configuration uses multicast to discover the nodes.
01. Create MsgMicro01 Class
c. Modify io.vertx.chapter04.MsgMicro01 Class
1. Update start() method :
@Override
public void start() {
// Receive message from the address 'AdrMsg'
vertx.eventBus().<String>consumer("AdrMsg", message -> {
JsonObject json = new JsonObject()
.put("served-by", this.toString());
// Check whether we have received a payload in the
// incoming message
if (message.body().isEmpty()) {
message.reply(json.put("message", "hello"));
} else {
message.reply(json.put("message",
"hello " + message.body()));
}
});
}
01. Create MsgMicro01 Class
Notes:
• This code retrieves the eventBus from the vertx object and
registers a consumer on the address 'AdrMsg'. When a
message is received, it replies to it. Depending on whether or not
the incoming message has an empty body, we compute a
different response. As in the example in the previous chapter, we
send a JSON object back.
• We added the served-by entry in the JSON for knowing from
which instance of MsgMicro01 class will reply the message if we
clustered MsgMicro01 class.
Credits
PPT: ALLPPT.com
Music: https://www.bensound.com