加载中

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.

本文中,我会通过基于高级消息队列协议(AMQP)的两个微服务之间的通信来展示异步方式的请求-回复过程。AMQP是应用程序或组织之间传递业务消息的开放标准。虽然本文的重点在于介绍请求-回复模式,但是同样的代码也可用于开发其它情况,比如事件风暴。使用异步模型通信对于实现聚合模式是非常有益的。

我会使用Apache QPid Proton(或Red Hat AMQ Interconnect)作为消息路由器和使用Vert.x AMQP桥接器,用于两种服务之间的通信。

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 frontendservice 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.

解决方案组件

演示有三个部分:

1.前端:这是一个用Java编写的服务,此服务提供了一个HTTP端点来接收来自客户端的调用。收到请求后,前端服务将调用指令发送到QPid路由器并注册应答处理程序。当响应可用时,Vert.x AMQP桥将调用回复处理程序。代码库中的前端文件夹托管此项目。

2.QPid路由器:前端进程执行调用并发布一条消息给QPid队列。Vert.x自动负责添加correlationId作为message属性,以识别对原始请求的响应。

3.后端:后端组件侦听来自QPid路由器的呼叫中的消息,处理它(例如,在数据库中进行计算或持久化),并将响应发送回QPid路由器。然后,QPid路由器将通过响应通知前端组件。代码库中的后端文件夹托管此项目。

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.

消息流

1.跨不同组件的消息的基本流程如下。可在此处找到此流程的完整详细信息以及相关标头。

2.前端服务将向QPid服务器发送消息并提供回复处理程序。Vert.x自动填充请求 - 回复通信所需的标头。

3.接收后端服务应用程序,处理该消息并将回复发送回QPid服务器。Vert.x填充请求 - 回复通信所需的标头。

QPid服务器将回复消息分派给前端服务的回复处理程序。Vert.x网桥自动处理回复处理程序的调用。

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

  • Use the following command to start the local QPid router:

docker run -it -p 5672:5672 ceposta/qdr
  • Compile and execute the frontend service:

cd frontend 
mvn clean install
java -jar target/frontend-service-full.jar
  • Compile and execute the backend service:

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

如何运行示例:快速开始

通过发出以下命令,您可以使用Docker Compose文件来运行本例的所有三个组件:

docker-compose up

如何运行示例: 困难的方式

本节概述如何单独运行每个组件。你需要下面的软件在你的笔记本电脑上运行它们。

  1. Docker (执行Apache Qpid路由器)

  2. Open JDK 8 (要编译前端和后端服务组件)

  3. Maven 3.2 (两个服务都使用Maven)

  4. Vegeta 作为HTPP客户端(或者您可以使用您最喜欢的工具)

执行

  • 使用下面的命令启动本地的QPid路由器:

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

测试

Vegeta是一种用于HTTP负载测试的开源工具,可用于将请求发布到前端组件。

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

验证消息数目及延时

QPid,作为服务之间通信的异步集线器,提供了超快骨干。一旦完成应用程序测试之后,你可以使用其IMAGE ID登录QPid路由的Docker容器,并运行qdstat以查看各种指标

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.

总结

Apache QPid为微服务之间的通信提供了一个超快的主干通讯。由于 AMQP 是一种线级协议,在其他栈(如.net)中编写的服务也可以使用相同的通信通道。Java 开发人员可以使用 Vert.x AMQP birdge 工具轻松实现基于 amqp 的异步服务间通信模式。

返回顶部
顶部
返回顶部
顶部