java 在 Tomcat 内托管 Netty 服务器是否可行/可取?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3194508/
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-30 00:45:38  来源:igfitidea点击:

Is hosting a Netty Server inside Tomcat feasible/desirable?

javadeploymenttomcatnionetty

提问by ripper234

We building a Netty/NIO based service, and I'm considering the deployment of this service to our production environment. Our standard way of deploying services is as WARs, to be deployed inside Tomcats.

我们构建了一个基于 Netty/NIO 的服务,我正在考虑将这个服务部署到我们的生产环境中。我们部署服务的标准方式是作为 WAR 部署在 Tomcat 中。

When I suggested the same approach here, I got shouts and complaints that "it shouldn't be done", because both Netty and Tomcat are servers, and "it doesn't make sense to host one server in another".

当我在这里提出相同的方法时,我得到了“不应该这样做”的喊叫和抱怨,因为 Netty 和 Tomcat 都是服务器,并且“将一台服务器托管在另一台服务器上没有意义”。

To me it makes perfect sense because it completely solves my deployment issue, as well as saves me from writing some other code. Why is it such a big "no no" ?

对我来说,这是完全合理的,因为它完全解决了我的部署问题,并且让我免于编写其他一些代码。为什么会有这么大的“不不”?

回答by trustin

The dynamic WAR deploying and undeploying that Tomcat provides are designed for web applications. The Netty application you are trying to deploy into Tomcat is not a web application but just a separate server that only shares the VM memory. It means Tomcat has been repurposed into a generic microkernel such as OSGi.

Tomcat 提供的动态 WAR 部署和取消部署是为 Web 应用程序设计的。您尝试部署到 Tomcat 中的 Netty 应用程序不是 Web 应用程序,而是仅共享 VM 内存的单独服务器。这意味着 Tomcat 已被重新用于通用微内核,例如 OSGi。

However, I don't think it's a big problem. Since your company uses WAR as the standard deployment mechanism, it might be a good idea to reuse it. You don't even need to write some management functions like remote shutdown because Tomcat already provides them. All you need to do is to make sure all resources are freed up when undeployed.

不过,我不认为这是一个大问题。由于您的公司使用 WAR 作为标准部署机制,因此重用它可能是个好主意。你甚至不需要编写一些管理功能,比如远程关机,因为Tomcat已经提供了它们。您需要做的就是确保在取消部署时释放所有资源。

Some people might not like this approach though. Ideally, there should be a common infrastructure for deploying and managing whatever application (aka microkernel), where even Tomcat is deployed as a module and the microkernel manages WAR directly instead Tomcat does. But that's a long way to go.

不过,有些人可能不喜欢这种方法。理想情况下,应该有一个通用的基础设施来部署和管理任何应用程序(又名微内核),其中甚至将 Tomcat 部署为一个模块,微内核直接管理 WAR 而不是 Tomcat。但这还有很长的路要走。

回答by Adam Gent

It makes terrific sense. We in fact run a Java email server in Tomcat.

这很有道理。我们实际上在 Tomcat 中运行了一个 Java 电子邮件服务器。

Tomcat has some huge advantages for housing an application:

Tomcat 在容纳应用程序方面有一些巨大的优势:

  1. The daemon scripts are already written for you
  2. Tomcat allows hot deployment (so long as you're not using hibernate :) )
  3. At some point you may need either an Admin UI or Admin API
  4. Lots and lots of tools to monitor Tomcat which means free monitoring of your app.
  1. 守护进程脚本已经为您编写好了
  2. Tomcat 允许热部署(只要您不使用休眠 :))
  3. 在某些时候,您可能需要 Admin UI 或 Admin API
  4. 有很多工具可以监控 Tomcat,这意味着可以免费监控您的应用程序。

Now with Netty, Mina, or any event driven network technology you will not be able to use most MVC frameworks. In fact you will not be able to use most Java enterprise frameworks because many things rely on a thread per request (transactions, security, etc...).

现在使用 Netty、Mina 或任何事件驱动的网络技术,您将无法使用大多数 MVC 框架。事实上,您将无法使用大多数 Java 企业框架,因为很多事情都依赖于每个请求的线程(事务、安全性等)。

回答by stepancheg

Do not start anything in Tomcat, it is very inconvenient, and this has a lot of pain issues. Just embed Tomcat (or Jetty) inside your application and run your application as plain java process.

不要在Tomcat中启动任何东西,很不方便,而且这有很多痛苦的问题。只需将 Tomcat(或 Jetty)嵌入您的应用程序中,然后将您的应用程序作为纯 Java 进程运行。