简体   繁体   English

如何在两个Java服务器之间建立网络通信

[英]How to establish network communication between two Java servers

at the moment I have two Java Servers(Game and Database servers) in the future there will be more.目前我有两个 Java 服务器(游戏和数据库服务器),将来会有更多。 I need establish network communication between its, since they can be run on different machines.我需要在它之间建立网络通信,因为它们可以在不同的机器上运行。

Initially, I thought to serialize objects and send them over the network, let's say you need to save the user to the database, I serialize the object and send it to save, but how can we be with the deletion, modification, etc... So I think this approach is not very preferable.最初,我想将对象序列化并通过网络发送它们,假设您需要将用户保存到数据库中,我将对象序列化并发送保存,但是我们如何进行删除、修改等操作。 . 所以我觉得这种方法不是很可取。

In this regard, the question may be there are any ready-made tools or technologies for this purpose, or it may be worth to implement the Protocol itself(but then how better to do it)?在这方面,问题可能是为此目的有任何现成的工具或技术,或者可能值得实施协议本身(但如何更好地实施)?

Start with a protocol.从协议开始。

Define what can be sent and what data a server would need.定义可以发送的内容以及服务器需要的数据。 You may use serialization if needed, but along with that you should provide additional information on what is being sent and, if needed, what to to with it.如果需要,您可以使用序列化,但除此之外,您还应该提供有关正在发送的内容以及如何处理的附加信息。

And, of course, there is no ready implementation for your business logic.而且,当然,您的业务逻辑没有现成的实现。

You decide what to send, when and how.决定发送什么、何时以及如何发送。

Define your own protocol using JSON as follows: Design an object containing the data you wish to pass back and forth, or a separate entity for each direction.使用 JSON 定义您自己的协议,如下所示:设计一个包含您希望来回传递的数据的对象,或者为每个方向设计一个单独的实体。 There are several ready made solutions to convert an object to a JSON string and back.有几种现成的解决方案可以将对象转换为 JSON 字符串并返回。 Then send the string over TCP - see for example Sending a JSON object over TCP with Java .然后通过 TCP 发送字符串 - 参见示例使用 Java 通过 TCP 发送 JSON 对象

So there are various mechanism through which two servers (API's) can communicate, starting from RMI, RPC to REST, JMS.因此,两个服务器(API)可以通过各种机制进行通信,从 RMI、RPC 到 REST、JMS。 You'll have to decide what mechanism you want to implement.您必须决定要实施的机制。 REST is one of the good example, whre depending upon what action you want to perform, you choose appropriate Http Method, so while insert/update use POST/PUT and pass the data in body. REST 是一个很好的例子,根据您要执行的操作,您选择合适的 Http 方法,因此在插入/更新时使用 POST/PUT 并在正文中传递数据。 Whereas while delete(DELETE) just call the api(method), at worst some flag can be used.虽然 delete(DELETE) 只是调用 api(method),但最坏的情况是可以使用一些标志。

JMS is another mechanism which could be handy, where you send the messages and perform the action. JMS 是另一种可能很方便的机制,您可以在其中发送消息并执行操作。 Similar to REST you define the message format and message header could be used to define the action you want to perform.与 REST 类似,您可以定义消息格式和消息标头,可用于定义要执行的操作。

One of the old fashioned ways of doing such a thing, like you said communicate with Database server, would be define a DB User Stored Procedures(USP) or a function & call USP/function by passing appropriate parameters.做这种事情的老式方法之一,就像你说的与数据库服务器通信,是定义一个数据库用户存储过程(USP)或一个函数,并通过传递适当的参数来调用 USP/函数。 Off-course you'll have to write client application for perform this passing/calling task.当然,您必须编写客户端应用程序来执行此传递/调用任务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM