Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

war - deploying multiple applications to Tomcat

I want to deploy two applications foo.war and bar.war to the same Tomcat instance. Is it possible for them to listen for connections on different ports, e.g. foo listens on port 81 and bar listens on port 82? If so, how can I configure this? I realise that it is not necessary for the applications to listen on different ports, but that is what I want to achieve.

Also, am I right in saying that if I rename foo.war to ROOT.war such that it runs in the root context, then all requests to this Tomcat instance will be handled by the foo app and therefore bar would have to be deployed to a separate Tomcat instance?

question from:https://stackoverflow.com/questions/23569327/deploying-multiple-applications-to-tomcat

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

1 Answer

0 votes
by (71.8m points)

If you want Tomcat to listen to multiple ports, you need to setup a connector for each port. To get each port mapped to a different application, you need need to wrap each connector in a service and create a host with it's own appBase.

Example of service definition in server.xml:

<Service name="foo">
    <Connector port="80" protocol="org.apache.coyote.http11.Http11NioProtocol" />
    <Engine name="Catalina80" defaultHost="localhost">
        <Host name="localhost" appBase="foo" unpackWARs="true" autoDeploy="true" />
    </Engine>
</Service>

<Service name="bar">
    <Connector port="81" protocol="org.apache.coyote.http11.Http11NioProtocol" />
    <Engine name="Catalina81" defaultHost="localhost">
        <Host name="localhost" appBase="bar" unpackWARs="true" autoDeploy="true" />
    </Engine>
</Service>

Instead of dropping the war files in the webapps directory, you need to create the directory foo for port 80 and bar for port 81. Name both war files ROOT.war and drop them in their own base directory. You can of course have multiple apps in each directory if you need.

The directory defined in appBase is relative to the tomcat directory. By using an absolute path, it could be anywhere on your system. From the documentation:

appBase

The Application Base directory for this virtual host. This is the pathname of a directory that may contain web applications to be deployed on this virtual host. You may specify an absolute pathname, or a pathname that is relative to the $CATALINA_BASE directory. [...] If not specified, the default of webapps will be used.

Another option is to keep the default tomcat configuration and use another http server (apache, nginx, lighttpd,...) to map a port to the internal path of a tomcat application.

The root application won't receive requests that match other applications, e.g. /foo/example will go to foo.war, /example/example will go to ROOT.war.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...