Fast NIO,Java 异步 HTTP 服务器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1137748/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 15:19:17  来源:igfitidea点击:

Fast NIO, asynchronous HTTP Server for Java

javahttpasynchronous

提问by Daff

Following on this questionI am actually in the phase of finding the right HTTP container for one of my projects, too. I looked at several containers but I'm still not sure which one will be best suited for high load AJAX only requests. Apache Minalooks quite promising, but relatively complex as well. The asynchronous web server implementation called AsyncWeb seems to have been merged with Mina but I couldn't find any production relase of it, yet. In the other question I recommended the Simple HTTPserver which I really like because it is... well simple, clear and clean but I still don't know if it would fit the purpose.

这个问题之后,我实际上也处于为我的一个项目寻找合适的 HTTP 容器的阶段。我查看了几个容器,但我仍然不确定哪一个最适合高负载 AJAX 请求。Apache Mina看起来很有前途,但也相对复杂。名为 AsyncWeb 的异步 Web 服务器实现似乎已与 Mina 合并,但我还找不到它的任何生产版本。在另一个问题中,我推荐了我非常喜欢的Simple HTTP服务器,因为它......非常简单、清晰和干净,但我仍然不知道它是否符合目的。

Additionally I'm not sure on which request handling concept to choose:

此外,我不确定选择哪种请求处理概念:

  1. Create a dispatcher Thread for every incoming connection (optimizations might of course include a thread pool and a dispatching queue), that is doing all the work. The advantage is probably, that I don't have to deal with that many synchronisation issues but it will probably lower the throughput on high loads drastically.

  2. Because it is going to be a highly modular application a "pipelining" (there might be a more fitting term I guess ;) approach might work as well: Create a fixed number of Threads, each for a certain task. E.g. one for request handling -> one for header deserialization (if I get input in different formats like subtmitted HTML Forms, XML-RPCs, JSON etc.) -> one for "Controller Dispatching" (doing whatever I want to do with these data) -> and one for serializing the output in the desired format (JSON, XML, HTML etc.) and move every request through these levels until it is completed. Probably more difficult to realize but I have a fixed number of Threads (the number can dependend on the hardware as well) and a clean separation of concerns.

  1. 为每个传入连接创建一个调度程序线程(优化当然可能包括一个线程池和一个调度队列),这将完成所有工作。优点可能是,我不必处理那么多同步问题,但它可能会大大降低高负载下的吞吐量。

  2. 因为它将是一个高度模块化的应用程序,所以“流水线”(我猜可能有一个更合适的术语;)方法也可以工作:创建固定数量的线程,每个线程用于特定任务。例如,一种用于请求处理 -> 一种用于标头反序列化(如果我以不同的格式输入,例如提交的 HTML 表单、XML-RPC、JSON 等)-> 一种用于“控制器调度”(对这些数据做任何我想做的事情) ) -> 和一个用于以所需格式(JSON、XML、HTML 等)序列化输出并将每个请求通过这些级别移动直到完成。可能更难实现,但我有固定数量的线程(数量也可能取决于硬件)和清晰的关注点分离。

Any experiences with any Framework that might fit and the two different handling approaches?

任何可能适合的框架和两种不同的处理方法的经验?

采纳答案by ng.

There is an example on the Simple HTTPsubversion repository.

Simple HTTPsubversion 存储库中有一个示例。

http://simpleweb.svn.sourceforge.net/viewvc/simpleweb/trunk/src/demo/java/org/simpleframework/example/javafx/

http://simpleweb.svn.sourceforge.net/viewvc/simpleweb/trunk/src/demo/java/org/simpleframework/example/javafx/

It demonstrates realtime market data using comet and only one thread dispatching updates to any number of clients. Simple has a transparent transport layer that provides blocking I/O semantics with the advantage of asynchronous dispatch with NIO based around fixed ByteBuffer queues for each connected client. So you get a smooth transparent NIO output with predictable memory consumption. Also performance measurements against servers like Jetty show about a x2 performance improvement.

它使用 Comet 演示实时市场数据,并且只有一个线程向任意数量的客户端发送更新。Simple 有一个透明的传输层,它提供阻塞 I/O 语义,并具有基于每个连接客户端的固定 ByteBuffer 队列的 NIO 异步调度的优势。因此,您可以获得具有可预测内存消耗的平滑透明 NIO 输出。此外,针对 Jetty 等服务器的性能测量也显示了 x2 的性能改进。

http://simpleweb.svn.sourceforge.net/viewvc/simpleweb/trunk/application/Plotter/ApacheBench/ScalabilityApacheBench.png?revision=1448

http://simpleweb.svn.sourceforge.net/viewvc/simpleweb/trunk/application/Plotter/ApacheBench/ScalabilityApacheBench.png?revision=1448

回答by Brian Agnew

You may want to look at Jetty, and in particular, Hightide

你可能想看看Jetty,特别是Hightide

Hightide is pre-configured with state-of-the-art Ajax communication libraries such as DWR, ActiveMQ-Web (JMS to the browser), and the Bayeux protocol (also known as cometd). Deploying your applications on Hightide means that it will scale smoothly thanks to a combination of Jetty's smart IO layer and continuations mechanism

Hightide 预先配置了最先进的 Ajax 通信库,例如 DWR、ActiveMQ-Web(JMS 到浏览器)和 Bayeux 协议(也称为 Cometd)。在 Hightide 上部署您的应用程序意味着它将平滑扩展,这要归功于 Jetty 的智能 IO 层和延续机制的结合

Specifically, a lot of work has been put in to optimise Jetty for AJAX. See this blog entryfor more details

具体来说,已经投入了大量工作来为 AJAX 优化 Jetty。有关更多详细信息,请参阅此博客条目