0% found this document useful (0 votes)
6 views50 pages

COMP 301 Week 10

The document discusses the concept of Project Object Model (POM) in Maven, detailing its structure, inheritance, and aggregation. It also covers microservice communication methods, including synchronous and asynchronous communication, and the use of message brokers like RabbitMQ and Kafka. Key components such as RestTemplate, WebClient, and Open Feign for synchronous communication, as well as command and event messages for asynchronous communication, are explained.

Uploaded by

suayilseven764
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views50 pages

COMP 301 Week 10

The document discusses the concept of Project Object Model (POM) in Maven, detailing its structure, inheritance, and aggregation. It also covers microservice communication methods, including synchronous and asynchronous communication, and the use of message brokers like RabbitMQ and Kafka. Key components such as RestTemplate, WebClient, and Open Feign for synchronous communication, as well as command and event messages for asynchronous communication, are explained.

Uploaded by

suayilseven764
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 50

COMP 301 Week 10

Microservice Communication
Part 1
Understanding POM
● What is POM ?
● Super POM
● Minimal POM
● Project Inheritance
● Project Aggregation
● Project Variables / Properties
What is POM ?

● Building block of maven project


● Its an XML file, that contains information/configuration about the
project that helps maven to build the project

Have All the


information about POM (Project
the project Object Model)
Super POM

It’s a POM that every project inherits.

Maven Project /
POM
Inherite
Super POM Maven Project /
d by
POM

All Maven
Minimal POM

The minimum amount of information / configuration the need to be


provided in the pom to get a functionally project.
● project - root tag
● modelVersion - should be set to 4.0.0
● groupId - the id of the project's group.
● artifactId - the id of the artifact (project) / name of the project
● version - the version of the artifact under the specified group
Minimal POM Structure
Project Inheritance

Suppose we have many projects related to each other, or more specifically


projects having common information / configurations.
These common configurations can be extracted to a different pom(parent
pom) and the projects can inherit “parent pom”.
NOTE : Maven always focus on reusability and modularity. It do not
recommend to duplicate configurations.
Understand Project Inheritance

Project A POM have X, Y, Parent Project POM have X, Y


Z & packaging as “pom”

Inherit Inherit
by POM have by
POM have
POM have X, Y,
Project B Project A Z Project B P
P (X, Y (X,Y
inherited) inherited)
Before Afte
Project Inheritance / Aggregation Impl.
Create 3 projects
● Parent project :: mvn archetype:generate -DgroupId=com.parent.app
-DartifactId=parent -DarchetypeArtifactId=maven-archetype-
quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false
● Child module 1 :: mvn archetype:generate -DgroupId=com.child.app -
DartifactId=child-1 -DarchetypeArtifactId=maven-archetype-quickstart
-DarchetypeVersion=1.4 -DinteractiveMode=false
● Child module 2 :: mvn archetype:generate -DgroupId=com.child.app -
DartifactId=child-2 -DarchetypeArtifactId=maven-archetype-quickstart
-DarchetypeVersion=1.4 -DinteractiveMode=false
Inheritance : Directory Structure Type
1
Minimal Parent POM
Minimal Child POM
Inheritance : Directory Structure Type
2
Minimal Parent POM
Minimal Child POM
Project Aggregation

Similar to Project Inheritance. But in this, in child modules we do not


specify the parent.
In parent, modules are specified.
It is used when,
● When a parent project is logically related to each other
● But they do not have common dependencies.
Understand Project Aggregation

child-1(Module-1)POM have D, E

Parent POM have A, B,


C
child-2(Module-2)POM have F, G

.
Aggregation : Directory Structure
Type 1
Minimal Parent POM
Minimal Child POM
Project Inheritance Vs Project
Aggregation

If we have our projects,


● Logically related
● Common dependency
We can use both in out project. That is we can define parent in child
module and modules in parent project.
Minimal Parent POM
Minimal Child POM
Maven Variables/properties

● Maven encourages us to “don’t repeat yourself”, thus maven allow us


to use variables.
○ Already available variables : Defined in super pom
○ Variable defined by us : Defined by us in out projects pom.xml in
<properties> tag.
Property
Definition

Property
usage
Microservice Communication
• We will look at how microservice can communicate with each other :
1.Synchronous Communication
2.Asynchronous Communication
Synchronous Communication
Synchronized Communication
• In Spring boot microservices can tale k with each other by 3 ways:
Using RestTemplate object(depreciated)
Using WebClient
Spring Cloud Open Feign library
Using RestTemplate
• In order to use RestTemplate object:
Instantiate RestTemplate object using @Bean annotation on the top of
method.
Inject generated RestTemplate object in Spring IOC container into related
Service class( either via constructor or @Autowired annotation)
RestTemplate class
• RestTemplate is Synchronous client to perform HTTP requests,
exposing a simple, template method API over underlying HTTP client
libraries such as the JDK HttpURLConnection, Apache
HttpComponents, and others.
• RestTemplate offers templates for common scenarios by HTTP
method, in addition to the generalized exchange and execute
methods that support less frequent cases.
RestTemplate usage

To Get Object via RestTemplate

To Post Object via RestTemplate


Using WebClient
• WebClient is a non-blocking, reactive client to perform HTTP
requests, exposing a fluent, reactive API over underlying HTTP client
libraries such as Reactor Netty.
• To use WebClient in our Spring boot project, we have to
dd Spring WebFlux dependency to the classpath.

<!-- https://mvnrepository.com/artifact/org.springframework/spring-webflux -->


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
WebClient
• In order to use WebClient, create WebClient bean:

• Let's inject WebClient and use it to make a REST API


call:
Open Feign library
• Feign makes writing web service clients easier with
pluggable annotation support, which includes Feign
annotations and JAX-RS annotations.
• Also, Spring Cloud adds support for Spring MVC
annotations and for using the
same HttpMessageConverters as used in Spring Web.
Using Open Feign
• Open the pom.xml file of the project and add the below dependency:

• Enable Feign Client using @EnableFeignClients


• After that, we need to have a feign API client with the necessary methods,
requests, and responses.

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
Example FeignClient Interface

• We declare a Feign client using the @FeignClient annotation.

• The value argument passed in the @FeignClient annotation is a mandatory,


arbitrary client name, while with the URL argument, we specify the API base URL.

• Furthermore, since this interface is a Feign client, we can use the Spring Web
annotations to declare the APIs that we want to reach out to.

• Finally, You just need to inject Feign Client in your required service.
Asynchronous Communication
Asynchronous Communication

• In ordeer to discuss Asynchronous Communication within


microservices, we need to understand the role of
Message Brokers first.
Message Broker
• Message broker is a component used for applications
to communicate with each other.
• It provides the exchange of information between
applications by transmitting messages received from
the producer to consumer.
Components of Message Broker
Topology
• Producer: It is the component from which the
messages are produced.
• For example; when you create an account on a
website, UserService creates a message indicating
that the user has been created and forwards it to the
message broker. In this example, UserService takes
on the role of the producer
• Consumer: It is our component that tracks the messages coming
to the queue. It receives messages from the queue and executes
actions.
• If we use our previous example, we can imagine that we have a
service that sends “Welcome” mail by reading the user has been
created message through the broker. Our service that carries out
the action, that is, sends the mail, is the consumer in this
example.
Components of Message Broker
Topology
• Message broker: It is the component that
receives the messages from the producer and
transmits them to the consumer.
• It transmits the message to the required queues
according to its rules.
• It also delivers the message to the consumer
through these queues. Components such as
 Kafka,
 RabbitMQ,
 ActiveMQ

• play the role of message broker.


How Message Broker Works?

• The producers transmit their messages to the component called exchange in


the message broker with the specified protocols.
• We can think of exchanges as entry points for message brokers.
• Exchange is responsible for forwarding messages to the required queues
according to the specified rules. I
• In the correct usage of the message broker, messages are not directly sent to the
queues.
How Message Broker Works?

• Consumers are also in communication with the message queues.


• We can think of the queues as the broker’s exit points.
Rabbit MQ vs. Kafka???
• RabbitMQ operates with a structure very similar to this
topology. Each consumer has its own queue:
 Samples of messages sent on exchange are stored in these
queues. When the consumer finishes the transaction, the
message is removed from consumer’s queue.
 Because another consumer does not read a message through
the queue that belongs to someone else.
Rabbit MQ vs. Kafka???
• Kafka presents the above topology with a slightly
different structure:
• The messages are not in separate queues.
• On the other hand, consumers determine where they stay on
message queues with their own pointers.
• After the consumer processes a message, it moves its pointer.
• The messages are not deleted from the queues because
another consumer can also process this message.
• It acts as if there are more than one queue by using pointers.
What are Exchange Types?
Exchange Types
• Direct and fanout are the most used types. The type is
decided according to the number of queues that can
communicate with exchange.
• If the exchange is set up to communicate with only one
queue, this exchange is called direct-exchange.
• If more than one queue is connected to an exchange
and copies of incoming messages are distributed to
these queues, we are using the fanout-exchange
type.
What are the Message Types?
• We can talk about two types of
messages; event and command.
• Event messages are messages that are sent to the messaging
system to announce to all applications that an event has
occurred.
• It is recommended that the message names end with a past
tense suffix, as they are published after the event is completed.
• For example, PackageDelivered
• Event messages are forwarded to fanout-exchange, as they
will be transmitted to the entire system.
• Communication with the message broker is done with
the publish method because the message spreads
throughout the system.
Command Message Type
• Command messages are messages sent to the message broker to
trigger the consumer to fulfill a request.
• It is recommended to include the verb of the process to be executed in
the name of the message.
• The message named CreateAccount can be an example of a command
message.
• Command is processed by a single consumer.
• For this reason, command messages are transmitted to direct-
exchanges.
• Communication with the broker is carried out by the send method.
• Because in this case, it is defined to whom the message will be
delivered. In this type of message, message is sent to a particular
receiver rather than being published to all processes
Command vs. Event Message

You might also like