我可以使用ZeroMQ套接字更改使用REST的两个微服务之间的通信机制吗? [英] Can I use ZeroMQ sockets to change communication mechanism between two microservices that use REST?

查看:102
本文介绍了我可以使用ZeroMQ套接字更改使用REST的两个微服务之间的通信机制吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将基于HTTP API->的通信干净转换为使用ZMQ库的消息通信?

How can we transform cleanly a communication based on HTTP API --> to a message communication using ZMQ library ?

推荐答案

如果确实要这样做,可以使用ZeroMQ工具设计一种介体.

In case you indeed want to do so, one may design a kind of Mediator, using ZeroMQ tools.

ZeroMQ具有一组多层次的抽象,其中AccessPoint通常具有在它们彼此之间执行的某些行为"(分布式行为).

ZeroMQ has a set of multi-level abstractions, where AccessPoints, typically have a certain "behaviour" ( a distributed behaviour ) they perform among themselves.

您指定的目标旨在不使用这种行为,而是对数据流进行某种透明的(几乎)有线级别的处理.

Your indicated target aims at using no such behaviour, but to have some sort of transparent, (almost) wire-level handling of data-flows.

为此,让我首先将您的注意力吸引到以下概念:
- 不到5秒的ZeroMQ层次结构
在可行的工具旁边,该工具可以切实帮助您完成给定的任务:
- ZMQ_STREAM 可扩展的正式通信原型(用于AccessPoint)

For this very purpose let me direct your kind attention first to the concept:
- ZeroMQ Hierarchy in Less than Five Seconds
and next to a possible tool, feasible to help in the given task:
-ZMQ_STREAM Scalable Formal Communication Archetype ( for an AccessPoint )

使用 tcp:// 传输时,类型为ZMQ_STREAM的套接字用于从非ØMQ对等方发送和接收TCP数据. ZMQ_STREAM套接字可以充当客户端和/或服务器,以异步方式发送和/或接收TCP数据.

当接收TCP数据时,ZMQ_STREAM套接字在将消息传递给应用程序之前,必须在消息部分之前包含消息的始发对等方的标识.从所有连接的对等方之间公平地排队接收到的消息.

发送TCP数据时,ZMQ_STREAM套接字应删除消息的第一部分,并使用它来确定消息应路由到的对等方的身份,并且不可路由的消息将导致EHOSTUNREACHEAGAIN错误.

要打开与服务器的连接,请使用zmq_connect调用,然后使用ZMQ_IDENTITY zmq_getsockopt调用获取套接字标识.

要关闭特定连接,请发送标识帧,然后发送零长度消息(请参见示例部分).

建立连接后,应用程序将接收零长度的消息.同样,当对等方断开连接(或连接断开)时,应用程序将收到零长度的消息.

您必须先发送一个标识帧,然后发送一个数据帧. ZMQ_SNDMORE标志对于身份帧是必需的,但在数据帧上将被忽略.

A socket of type ZMQ_STREAM is used to send and receive TCP data from a non-ØMQ peer, when using the tcp:// transport. A ZMQ_STREAM socket can act as client and/or server, sending and/or receiving TCP data asynchronously.

When receiving TCP data, a ZMQ_STREAM socket shall prepend a message part containing the identity of the originating peer to the message before passing it to the application. Messages received are fair-queued from among all connected peers.

When sending TCP data, a ZMQ_STREAM socket shall remove the first part of the message and use it to determine the identity of the peer the message shall be routed to, and unroutable messages shall cause an EHOSTUNREACH or EAGAIN error.

To open a connection to a server, use the zmq_connect call, and then fetch the socket identity using the ZMQ_IDENTITY zmq_getsockopt call.

To close a specific connection, send the identity frame followed by a zero-length message (see EXAMPLE section).

When a connection is made, a zero-length message will be received by the application. Similarly, when the peer disconnects (or the connection is lost), a zero-length message will be received by the application.

You must send one identity frame followed by one data frame. The ZMQ_SNDMORE flag is required for identity frames but is ignored on data frames.

示例:

                                             /* Create Context-Engine */
void *ctx = zmq_ctx_new ();                     assert (ctx);

                                             /* Create ZMQ_STREAM socket */
void *socket = zmq_socket (ctx, ZMQ_STREAM);    assert (socket);

int rc = zmq_bind (socket, "tcp://*:8080");     assert (rc == 0);

                                             /* Data structure to hold the ZMQ_STREAM ID */
uint8_t id [256];
size_t id_size = 256;

                                             /* Data structure to hold the ZMQ_STREAM received data */
uint8_t raw [256];
size_t raw_size = 256;

while (1) {
                                             /* Get HTTP request; ID frame and then request */
 id_size = zmq_recv (socket, id, 256, 0);       assert (id_size > 0);

 do {
 raw_size = zmq_recv (socket, raw, 256, 0);     assert (raw_size >= 0);
 } while (raw_size == 256);

                                             /* Prepares the response */
 char http_response [] =
                          "HTTP/1.0 200 OK\r\n"
                          "Content-Type: text/plain\r\n"
                          "\r\n"
                          "Hello, World!";

                                             /* Sends the ID frame followed by the response */
 zmq_send (socket, id, id_size, ZMQ_SNDMORE);
 zmq_send (socket, http_response, strlen (http_response), 0);

                                            /* Closes the connection by sending the ID frame followed by a zero response */
 zmq_send (socket, id, id_size, ZMQ_SNDMORE);
 zmq_send (socket, 0, 0, 0);
}
zmq_close (socket); zmq_ctx_destroy (ctx);  /* Clean Close Sockets / Terminate Context */

这篇关于我可以使用ZeroMQ套接字更改使用REST的两个微服务之间的通信机制吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆