简体   繁体   English

用于客户端/服务器通信的Java标准

[英]Java standard for client/server communication

What is the "official" Java API for client/server or P2P communication? 什么是客户端/服务器或P2P通信的“官方”Java API? Java RMI? Java RMI? Some other networking API?? 其他一些网络API?

Is this official networking API the standard for both SE and EE? 这是官方的网络APISE和EE标准?

I'm sure the answer is very context-specific, so let's take a look at a few instances: 我确定答案是特定于上下文的,所以我们来看看几个实例:

  1. You have 2 swing clients installed on 2 machines and connected to the same network (or the Internet), and you want either of them to send the other a primitive, such as the integer 4, or some POJO, like a "Widget" object 您在2台计算机上安装了2个swing客户端并连接到同一网络(或Internet),并且您希望其中任何一个发送另一个原语,例如整数4或某些POJO,如“Widget”对象
  2. Same as #1 above, but between a Swing client and a fully-compliant Java EE back-end (implementing managed beans, app servers, the whole nine yards) 与上面的#1相同,但是在Swing客户端和完全兼容的Java EE后端之间(实现托管bean,app服务器,整个9码)

I don't have a specific application in mind, I'm just wondering what are the "norms" for client-client and client-server communication in the world of Java. 我没有考虑具体的应用程序,我只是想知道Java世界中客户端 - 客户端和客户端 - 服务器通信的“规范”是什么。

If being bound by Java isn't a problem, RMI is a pretty abstracted solution when it comes to the client and server solution "exchanging" data (especially when the data is Java classes which might be difficult/too much effort to represent as textual data). 如果受Java约束不是问题,那么当客户端和服务器解决方案“交换”数据时,RMI是一个非常抽象的解决方案(特别是当数据是Java类时,可能很难/太多努力表示为文本数据)。 Just make sure your object implements Serializable and almost anything can be transmitted over the wire. 只需确保您的对象实现Serializable,几乎任何东西都可以通过网络传输。

If this doesn't fit your bill and you want to drop down the raw networking stuff, the client-server socket framework Netty is a pretty good choice. 如果这不适合您的账单,并且您想要删除原始网络内容,那么客户端 - 服务器套接字框架Netty是一个不错的选择。

There's no such thing as the most official networking API in J2SE, all J2SE APIs are official in the sense they are supported by Sun (now Oracle). 在J2SE中没有最官方的网络API,所有J2SE API都是官方的,因为Sun(现在是Oracle)支持它们。
That said, you should choose your API based on following criteria: 也就是说,您应该根据以下标准选择API:

  • Do you (or your team) know how to use particular API; 您(或您的团队)是否知道如何使用特定的API;
  • How simple/complex is this API to use; 这个API使用起来有多简单/复杂;
  • What throughput are you aiming for? 您的目标吞吐量是多少? For performance-sensitive applications you may be forced to use binary protocol. 对于性能敏感的应用程序,您可能被迫使用二进制协议。 For the rest of cases, you can use text-based protocol. 对于其他情况,您可以使用基于文本的协议。

For example, between two clients simple text-based protocol will suffice for passing POJOs, for example using Apache MINA or Google protocol buffers. 例如,在两个客户端之间,简单的基于文本的协议就足以传递POJO,例如使用Apache MINA或Google协议缓冲区。
This will work between client and server as well. 这也适用于客户端和服务器。


Response to Zac's questions in comment: 回应Zac的评论问题:

  1. Binary protocols performance gain comes from the fact you don't need to convert everything to text form and back -- you just can pass binary presentation of you application memory with minimal changes, like, in case of BSD Sockets API, converting from host byte-order to network byte-order. 二进制协议性能增益来自于您不需要将所有内容转换为文本形式和返回的事实 - 您只需通过最少的更改就可以传递应用程序内存的二进制表示,例如,在BSD套接字API的情况下,从主机字节转换 - 订购网络字节顺序。 Unfortunately, I don't know details about how RMI/Java serialization processes objects, but I'm sure, it still much faster than passing all data in readable form; 不幸的是,我不知道RMI / Java序列化如何处理对象的细节,但我敢肯定,它仍然比以可读形式传递所有数据快得多;
  2. Yes, MINA and protocol buffers have Java APIs. 是的,MINA和协议缓冲区都有Java API。 They just not part of Java SE bundle, you have to download them separately. 它们只是Java SE捆绑包的一部分,您必须单独下载它们。 By the way, MINA can use both binary and readable serialization, depending on how you use it. 顺便说一句,MINA可以使用二进制和可读序列化,具体取决于您如何使用它。
  3. You should define notion of 'good' somehow, for example, answering to questions I mentioned above. 你应该以某种方式定义“好”的概念,例如,回答我上面提到的问题。 If you want to use objects over network, use RMI. 如果要通过网络使用对象,请使用RMI。 If you don't, Netty or MINA will suffice, whatever you'll find easier to master. 如果你不这样做,Netty或MINA就足够了,无论你发现什么都比较容易掌握。

For P2P, Sun at one point pushed JXTA pretty hard. 对于P2P,Sun一度推动JXTA非常努力。

I wouldn't dare to use RMI for P2P communication. 我不敢用RMI进行P2P通信。

rmi is pretty much the standard java to java protocol. rmi几乎是标准的java to java协议。 it's built in and very simple to use. 它内置并且使用起来非常简单。 most j2ee backends also communicate using rmi, although that's not the only possibility. 大多数j2ee后端也使用rmi进行通信,尽管这不是唯一的可能性。

J2SE the most common is probably RMI or raw sockets. J2SE最常见的可能是RMI或原始套接字。

J2EE uses a messaging bus that everyone (servers and clients) subscribes to which is quite different from rmi style solutions (although at the lowest level an implementation may still rely on RMI). J2EE使用每个人(服务器和客户端)订阅的消息传递总线,这与rmi样式解决方案完全不同(尽管在最低级别,实现可能仍然依赖于RMI)。 It helps automate redundancy and failover. 它有助于自动化冗余和故障转移。 If you need this functionality I believe it can be used in SE as well. 如果您需要此功能,我相信它也可以在SE中使用。

I haven't used J2EE for quite a while now, so this may have changed, but I doubt it. 我现在还没有使用J2EE很长一段时间,所以这可能已经改变了,但我对此表示怀疑。 The messaging system was a core component of J2EE. 消息传递系统是J2EE的核心组件。

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

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