Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
330 views
in Technique[技术] by (71.8m points)

java - How to deploy multiple web application in tomcat which will run on different ports?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

<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 )

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

<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.

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...