两台服务器之间通信的最佳方式是什么?

时间:2015-05-04 10:43:03

标签: node.js server communication

我正在构建一个包含两个部分的Web应用程序。在一个部分中,它使用服务器和客户端之间的实时连接,而在另一部分中,它执行一些cpu密集型任务以提供相关数据。

在nodejs和python / java中的cpu密集部分实现实时通信。 nodejs服务器参与与其他服务器的双工通信的最佳方式是什么?

4 个答案:

答案 0 :(得分:2)

您可以使用提供的http.request方法在节点代码中发出curl请求。 http.request方法也用于实现Authentication api。 您可以将回调置于请求成功之后,当您在节点中获得响应数据时,可以将其发送回用户。 在backgrount中,java / python服务器可以利用节点的CPU密集型任务请求。

答案 1 :(得分:2)

For a basic solution you can use Socket.IO if you are already using it and know how it works, it will get the job done since it allows for communication between a client and server where the client can be a different server in a different language.

If you want a more robust solution with additional options and controls or which can handle higher traffic throughput (though this shouldn't be an issue if you are ultimately just sending it through the relatively slow internet) you can look at something like ØMQ (ZeroMQ). It is a messaging queue which gives you more control and lots of different communications methods beyond just request-response.

When you set either up I would recommend using your CPU intensive server as the stable end(server) and your web server(s) as your client. Assuming that you are using a single server for your CPU intensive tasks and you are running several NodeJS server instances to take advantage of multi-cores for your web server. This simplifies your communication since you want to have a single point to connect to.

If you foresee needing multiple CPU servers you will want to setup a routing server that can route between multiple web servers and multiple CPU servers and in this case I would recommend the extra work of learning ØMQ.

答案 2 :(得分:1)

我维护一个node.js应用程序,该应用程序在分布在两台服务器上的34个任务之间进行相互通信。

在您的情况下,对于Web服务器和应用服务器之间的通信,您可以考虑使用mqtt。

我使用mqtt进行这种通信。大多数语言都有mqtt客户端,包括node / javascript,python和java。在我的例子中,我使用mqtt'topics'发布json消息,任何已注册订阅'topic'的任务在发布时都会收到它的数据。如果你谷歌“pub sub”,“mqtt”和“mosquitto”,你会发现很多参考和例子。 Mosquitto(现在是一个Eclipse项目)只是众多mqtt经纪人中的一个。另一个用Java编写的非常好的代理叫做hivemq。

这是一个非常简单,可靠的解决方案,可以很好地扩展。在我的案例中,每天有数百万条消息可靠地通过mqtt。

答案 3 :(得分:0)

您必须正在寻找socketio

  

Socket.IO支持实时双向基于事件的通信。   它适用于所有平台,浏览器或设备,同样关注可靠性和速度。

     传统上,套接字一直是大多数解决方案   实时系统是架构的,提供双向的   客户端和服务器之间的通信通道。