Java 基于 Netty 的非阻塞 REST 框架
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19758215/
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
Netty based non-blocking REST framework
提问by Vaibhav Raj
I am working on a RESTfull application which requires high scalability. I am considering Netty based frameworks for RESTfull applications. I went through some of the available options and tried to get what they can offer as a non-blocking implementation. Here are my findings:
我正在开发一个需要高可扩展性的 RESTfull 应用程序。我正在考虑为 RESTfull 应用程序使用基于 Netty 的框架。我浏览了一些可用的选项,并试图获得它们可以作为非阻塞实现提供的功能。以下是我的发现:
- rest.li --> Still under experimental phase for Netty based NIO implementaions. So, not production ready.
- RESTEasy --> Standard JBoss project which supports Netty 4.x. But,instead of full stack Netty based NIO implementation, RESTEasy is a Buffer exchange between Netty and RESTEasy. It's not taking the advantages of Netty. Therefore scalability is not as high as expected from a Netty based framework.
- Netty-http component --> Another option is Apache Camel integration while using Netty-http component as an endpoint for routing requests to services exposed in from of beans. I think it's same as RESTEasy, only Netty-http component uses Netty based NIO capabilities and the rest of the system would use the old IO. I don't think I would help much in gaining scalabiltiy.
- RESTExpress --> It claims to be Netty based framework for RESTFull application. But, neither it has a decent community nor it can be trusted (Because it's very new) for enterprise application which requires high degree of security.
- rest.li --> 基于 Netty 的 NIO 实现仍处于试验阶段。所以,还没有做好生产准备。
- RESTEasy --> 支持 Netty 4.x 的标准 JBoss 项目。但是,RESTEasy 不是基于全栈 Netty 的 NIO 实现,而是 Netty 和 RESTEasy 之间的缓冲区交换。它没有利用 Netty 的优势。因此,可扩展性没有基于 Netty 的框架预期的那么高。
- Netty-http 组件 --> 另一个选项是 Apache Camel 集成,同时使用 Netty-http 组件作为端点,将请求路由到从 bean 中公开的服务。我认为它与 RESTEasy 相同,只有 Netty-http 组件使用基于 Netty 的 NIO 功能,系统的其余部分将使用旧的 IO。我认为我对获得可扩展性没有多大帮助。
- RESTExpress --> 它声称是基于 Netty 的 RESTFull 应用程序框架。但是,对于需要高度安全性的企业应用程序,它既没有像样的社区,也没有可信度(因为它很新)。
Before having the above findings, I wanted to use some ready to use framework and get the work done faster.
在获得上述发现之前,我想使用一些现成的框架并更快地完成工作。
I know it's an opinion based question. But, still I seriously need help for choosing right framework for my application. If in case, there is no Netty based REST framework: would it be wise to go for plumbing low level Netty based NIO code in my application? Any help appreciated. Thanks in advance.
我知道这是一个基于意见的问题。但是,我仍然非常需要帮助来为我的应用程序选择正确的框架。如果没有基于 Netty 的 REST 框架:在我的应用程序中使用基于低级别 Netty 的 NIO 代码是否明智?任何帮助表示赞赏。提前致谢。
采纳答案by Adam Gent
If you really want non-blocking you need to do non-blocking from the ground up and have proper REST clients. Otherwise as stated in my commentthe performance difference will be negligible and in many cases worse for NIO (Netty with thread sharing).
如果您真的想要非阻塞,则需要从头开始进行非阻塞并拥有适当的 REST 客户端。否则,如我的评论中所述,性能差异将可以忽略不计,并且在许多情况下对于 NIO(具有线程共享的 Netty)更糟。
There only two libraries that I know do non-blocking from the ground up Vert.xand somewhat Finagle(its missing other things like non-blocking data access).
我知道只有两个库从头开始做非阻塞的Vert.x和一些Finagle(它缺少其他东西,如非阻塞数据访问)。
You should also know Tomcat and various other servlet containers that can work with JAX-RS support NIO. The issue is that even though NIO is supported it will still be a single thread per request. Only Play, Finagle, Vert.x and pure Netty (regardless of NIO) support a different shared threading model and thus have different mechanisms for doing concurrency.
您还应该了解 Tomcat 和其他各种可以与 JAX-RS 一起使用的 servlet 容器支持 NIO。问题是即使支持 NIO,它仍然是每个请求的单个线程。只有 Play、Finagle、Vert.x 和纯 Netty(不考虑 NIO)支持不同的共享线程模型,因此有不同的并发机制。
回答by user2954240
Have you taken a look at Play?
你看过Play吗?
It appears that you are inclined to use Netty but if you are willing to look around a very simple Grizzly + Jerseysetup will probably perform well enough. Heck, a simple Glassfish 4.0 JAX-RS app may work well too.
看起来您倾向于使用 Netty,但如果您愿意环顾四周,一个非常简单的Grizzly + Jersey设置可能会表现得足够好。哎呀,一个简单的 Glassfish 4.0 JAX-RS 应用程序也可以很好地工作。
回答by trustin
Here's the list of microframeworks I'm aware of for REST applications:
以下是我所知道的 REST 应用程序的微框架列表:
- Scalatra - http://www.scalatra.org/(Scala + Netty)
- RestExpress - https://github.com/RestExpress/RestExpress(Java + Netty)
- Finatra - https://github.com/capotej/finatra(Scala + Finagle + Netty)
- Scalatra - http://www.scalatra.org/(Scala+ Netty)
- RestExpress - https://github.com/RestExpress/RestExpress(Java + Netty)
- Finatra - https://github.com/capotej/finatra(Scala+ Finagle + Netty)
Please feel free to comment to the answer - I'll update the answer to add more.
请随时对答案发表评论 - 我会更新答案以添加更多内容。
回答by Jingguo Yao
You can take a look at AsyncRestTemplatein Spring Framework 4.2.5.RELEASE. It can be used on top of Netty.
您可以查看Spring Framework 4.2.5.RELEASE中的AsyncRestTemplate。它可以在 Netty 之上使用。
Note: by default AsyncRestTemplate relies on standard JDK facilities to establish HTTP connections. You can switch to use a different HTTP library such as Apache HttpComponents, Netty, and OkHttp by using a constructor accepting an AsyncClientHttpRequestFactory.
注意:默认情况下,AsyncRestTemplate 依赖于标准的 JDK 工具来建立 HTTP 连接。通过使用接受 AsyncClientHttpRequestFactory 的构造函数,您可以切换到使用不同的 HTTP 库,例如 Apache HttpComponents、Netty 和 OkHttp。
回答by Kishore Karunakaran
回答by Martin Tarjányi
Spring 5 comes with a reactive web framework called WebFlux. You can choose from multiple servers like Netty or Undertow. A reactive non-blocking WebClient has also been added to Spring and it also has support for reactive Mongo, Redis and Cassandra (I guess more is coming soon).
Spring 5 附带了一个名为 WebFlux 的响应式 Web 框架。您可以从多个服务器中进行选择,例如 Netty 或 Undertow。Spring 中还添加了一个反应式非阻塞 WebClient,它还支持反应式 Mongo、Redis 和 Cassandra(我想很快就会有更多)。
Spring Boot, the 'opinionated view of Spring', will also get a new version (2.0) based on Spring 5. At the time of writing it is expectedto be released in February.
Spring Boot,即“Spring 的观点”,也将获得基于 Spring 5 的新版本 (2.0)。在撰写本文时,预计将于2 月发布。
More information on the reactive stack of Spring: https://docs.spring.io/spring/docs/current/spring-framework-reference/web-reactive.html
有关 Spring 的反应式堆栈的更多信息:https: //docs.spring.io/spring/docs/current/spring-framework-reference/web-reactive.html
回答by user1079877
Undertow 3.0will be on the top of Netty (instead of XNIO). A perfect, super light, simple API to build an application like REST API server. I use it for almost any Java Microservices I have.
Undertow 3.0将位于Netty(而不是 XNIO)之上。一个完美的、超轻的、简单的 API 来构建像 REST API 服务器这样的应用程序。我几乎将它用于我拥有的任何 Java 微服务。