DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
11 Monitoring and Observability Tools for 2023
Learn more
  1. DZone
  2. Software Design and Architecture
  3. Microservices
  4. Asynchronous Communication Between Microservices Using AMQP and Vert.x

Asynchronous Communication Between Microservices Using AMQP and Vert.x

In this article, we'll see how to run an example of asynchronous communication between Vert.X Reactive microservices working with AMQP.

Faisal Masood user avatar by
Faisal Masood
·
Sep. 06, 18 · Tutorial
Like (4)
Save
Tweet
Share
11.86K Views

Join the DZone community and get the full member experience.

Join For Free

Asynchronous Communication Between Microservices Using AMQP and Vert.x

Microservices are the go-to architecture in most new, modern software solutions. They are (mostly) designed to do one thing, and they must talk to each other to accomplish a business use-case. All communication between the microservices is via network calls; this pattern avoids tight coupling between services and provides better separation between them.

There are basically two styles of communication: synchronous and asynchronous. These two styles applied properly are the foundation for request-reply and event-driven patterns. In the case of the request-reply pattern, a client initiates a request and typically waits synchronously for the reply. However, there are cases where the client could decide not to wait and register a callback with the other party, which is an example of the request-reply pattern in an asynchronous fashion.

In this article, I am showcasing the approach of asynchronous request-reply by having two services communicate with each other over Advanced Message Queuing Protocol (AMQP). AMQP is an open standard for passing business messages between applications or organizations. Although this article focuses on the request-reply pattern, the same code can be used to develop additional scenarios like event storming. Communicating using an asynchronous model can be very beneficial for implementing the aggregator pattern.

I will be using Apache QPid Proton (or Red Hat AMQ Interconnect) as the message router and the Vert.x AMQP bridge for communication between the two services.

Solution Components

This demo has three components:

  1. frontend: This is a service written in Java and provides an HTTP endpoint to receive calls from clients. Upon receiving a request, the frontend service sends the call to the QPid router and registers a reply handler. The reply handler will be invoked by the Vert.x AMQP bridge when the response is available. The frontend folder in the codebase hosts this project.
  2. QPid router: The frontend process takes the call and posts a message to the QPid queue. Vert.x automatically takes care of adding a correlationId as the message property to identify a response to the original request.
  3. backend: The backend component listens for the message in the call from the QPid router, process it (e.g. doing a calculation or persisting in a database), and sends the response back to the QPid router. The QPid router will then notify the frontend component with the response. The backend folder in the codebase hosts this project.

Message Flow

The basic flow of the messages across different components is as follows. The full details of this flow along with the relative headers can be found here.

  1. The frontend service will send a message to a QPid server and provides a reply handler. Vert.x automatically populates the required headers needed for request-reply communication.
  2. The receiving application, the backend service, consumes the message and sends a reply back to the QPid server. Vert.x populates the required headers needed for request-reply communication.
  3. The QPid server dispatches the reply message to the frontend service's reply handler. Vert.x bridge handles the invocation of the reply handler automatically.

Clone this GitHub repo to get the example code.

How to Run the Example: Quickstart

You can use the Docker Compose file to run all three components of this example, by issuing the following command:

docker-compose up

How to Run the Example: The Hard Way

This section summarizes how to run each component individually. You need the following software to run them on your laptop.

  1. Docker (for executing the Apache Qpid router)
  2. Open JDK 8 (to compile the frontend and the backend service components)
  3. Maven 3.2 (both services use Maven)
  4. Vegeta as an HTPP client (or you can use your favorite tool for this)

Execution

docker run -it -p 5672:5672 ceposta/qdr


cd frontend 
mvn clean install
java -jar target/frontend-service-full.jar


cd backend 
mvn clean install
java -jar target/backend-service-full.jar

Testing

Vegeta, an open-source tool for HTTP load testing, can be used to post requests to the frontend component.

echo "GET http://localhost:8080/" | ./vegeta attack -duration=60s -rate=50 | tee results.bin | ./vegeta report

Verifying the Number of Messages and Latency

QPid provides an ultra-fast backbone as an asynchronous hub for communication between services. Once you finish testing your application, you can log into the QPid router's Docker container using its IMAGE ID and run qdstat to see various metrics.

docker exec <container-name> qdstat -c
docker exec <container-name> qdstat -l
docker exec <container-name> qdstat -a

Conclusion

Apache QPid provides an ultra-fast backbone for communication between microservices. Since AMQP is a wire-level protocol, services written in other stacks (like .NET) can also use the same communication channel. Java developers can easily adapt to the AMQP-based asynchronous inter-services communication pattern using the Vert.x AMQP bridge.

Vert.x microservice

Published at DZone with permission of Faisal Masood, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • The Power of Zero-Knowledge Proofs: Exploring the New ConsenSys zkEVM
  • Authenticate With OpenID Connect and Apache APISIX
  • A Beginner's Guide to Infrastructure as Code
  • Running Databases on Kubernetes

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: