Java 如何在tomcat中部署多个将在不同端口上运行的Web应用程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4366843/
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
How to deploy multiple web application in tomcat which will run on different ports?
提问by LetsSyncUp
How to deploy multiple java web application in tomcat which will run on different ports ? - How to do settings so that different web application will run on different ports - What all needs to be done for achieving this?
如何在将在不同端口上运行的 tomcat 中部署多个 java web 应用程序?- 如何进行设置,以便不同的 Web 应用程序将在不同的端口上运行 - 需要做什么才能实现这一目标?
回答by Bozho
You'd better have multiple tomcat installations. It would be easier.
你最好有多个 tomcat 安装。会更容易。
I guess you can register multiple <Connector>
s in server.xml
, and then filter out the contexts, but that's tedious and sounds wrong.
我猜你可以在 中注册多个<Connector>
s server.xml
,然后过滤掉上下文,但这很乏味而且听起来不对。
回答by Sean
You will need to setup another service in your server.xml file (tomcat_home/conf). If you havent changed your server file, you should already have one named Catalina (I am using Tomcat 5.5, you may have something slightly different depending on version)
您需要在 server.xml 文件 (tomcat_home/conf) 中设置另一个服务。如果您还没有更改您的服务器文件,您应该已经有一个名为 Catalina 的文件(我使用的是 Tomcat 5.5,根据版本的不同,您可能会有一些略有不同的内容)
<Service name="Dev2">
<Connector port="8090" maxHttpHeaderSize="8192"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true" />
<Connector port="8092"
enableLookups="false" redirectPort="9443" protocol="AJP/1.3" />
<Engine name="Dev2" defaultHost="MyDev">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
<Host name="MyDev" appBase="webapps"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
</Host>
</Engine>
</Service>
Notice that the names have changed from Catalina to Dev2, and localhost to MyDev. Change these to whatever you seem fit for your application. The ports and connectors have also changed. Once the new Service is setup, you then need to deploy applications to the proper Service/Port. You accomplish this by using XML files under (See Virtual Hosting)
请注意,名称已从 Catalina 更改为 Dev2,将 localhost 更改为 MyDev。将这些更改为您认为适合您的应用程序的任何内容。端口和连接器也发生了变化。设置新服务后,您需要将应用程序部署到正确的服务/端口。您可以通过使用下的 XML 文件来完成此操作(请参阅虚拟主机)
Tomcat_Home/conf/Catalina/localhost/
and
和
Tomcat_Home/conf/Dev2/MyDev/
for the respective ports which you are setting up
对于您正在设置的相应端口
At this point all you have to do is add a few more files to point the Service to your application.
As an Example, under Tomcat_Home/conf/Dev2/MyDev/
I have a file called Another.xml This file contains the following
此时,您所要做的就是添加更多文件以将 Service 指向您的应用程序。作为一个例子,在Tomcat_Home/conf/Dev2/MyDev/
我下面有一个名为 Another.xml 的文件,这个文件包含以下内容
<Context path="/" docBase="C:/to_delete" debug="10" crossContext="false">
</Context>
Now I can access the new application using the web address http://127.0.0.1:8090/Another
If I try and access this using my default port of 8080, I get an error as the application was not deployed for that given port.
现在我可以使用 Web 地址访问新应用程序http://127.0.0.1:8090/Another
如果我尝试使用我的默认端口 8080 访问它,我会收到一个错误,因为该应用程序没有为该给定端口部署。
Few things to note about this setup. If you use VirtualVM to look at the application, you will notice that they share the same process ID. Therefore you have to be extra careful of your resources. They will be using the same Heap space, and all the threads will be showing in the same list. If you have logging in your applications (i.e Log4j) ensure you have an option to show which thread was doing the work, as it may be tough to tell otherwise which port/application this would be coming from.
关于此设置需要注意的几件事。如果您使用 VirtualVM 查看应用程序,您会注意到它们共享相同的进程 ID。因此,您必须格外小心您的资源。它们将使用相同的堆空间,并且所有线程将显示在同一个列表中。如果您已登录您的应用程序(即 Log4j),请确保您可以选择显示哪个线程正在执行这项工作,因为否则可能很难判断这将来自哪个端口/应用程序。
As Bozho has already pointed out, It may be easier to simply have two instances of Tomcat running instead of one server listening on multiple ports.
正如 Bozho 已经指出的那样,简单地运行两个 Tomcat 实例而不是一个服务器侦听多个端口可能会更容易。
回答by Dennis
Sorry about making this an answer. I don't see any commenting ability for me on this question. Mabye the question is too old or my reputation is not high enough.
很抱歉让这个答案。在这个问题上,我没有看到任何评论能力。Mabye 问题太老了或者我的声誉不够高。
However, I have been researching the same question myself. You will have to know a lot more about how Tomcat, http servers, and the Java system environment to use the same instance. I have read where it is also VERY slow also.
但是,我自己一直在研究同样的问题。您将不得不更多地了解 Tomcat、http 服务器和 Java 系统环境如何使用相同的实例。我读过它也很慢的地方。
The best bet is separate instances. There are two fairly easy ways to do that: A/ For Ubuntu, you can use SVN to get this script: http://ubuntuforums.org/showthread.php?t=1211517http://code.google.com/p/tomcat-linux/
最好的选择是单独的实例。有两种相当简单的方法可以做到这一点: A/ 对于 Ubuntu,您可以使用 SVN 来获取此脚本:http: //ubuntuforums.org/showthread.php? t= 1211517 http://code.google.com/p /tomcat-linux/
B/ Your own, per user instances. http://brian.pontarelli.com/2007/09/17/multiple-tomcat-instances-on-ubuntu/
B/ 您自己的,每个用户实例。 http://brian.pontarelli.com/2007/09/17/multiple-tomcat-instances-on-ubuntu/
The last one was written for tomcat 5.5, but is probably adaptable to Tomcat 6
最后一个是为 tomcat 5.5 编写的,但可能适用于 Tomcat 6
However, the best directions for multiple JVM instances for the latest Tomcat on Linux is here: http://www.puschitz.com/InstallingTomcat.html
但是,Linux 上最新 Tomcat 的多个 JVM 实例的最佳方向在这里:http: //www.puschitz.com/InstallingTomcat.html