欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Tomcat多端口启动web应用

程序员文章站 2024-03-25 14:27:16
...

0、参考与调料包

参考:无

调料包:CentOS7.4, docker, 示例代码

1、简介

最近在梳理一个没了源码的项目,目的是整出它的源码。源码丢失的原因是没有全量发版,每次都是直接将class文件放入tomcat内的项目进行替换也就完事了,经过多人交接后,该项目无论是源码,还是部署方式(多个模块)都百花齐放,令人叹为观止。

在整理代码前,为了以后部署方便,也为了防止重蹈覆辙,就需要先把项目的部署方式梳理一遍,也就有了下面这个Tomcat多端口启动web应用的demo。

2、具体步骤

 

1、准备个demo

这个其实没啥特别的,编写个Spring Web Application打war包就 可以了。

2、修改Tomcat的配置文件server.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<!-- Note:  A "Server" is not itself a "Container", so you may not
     define subcomponents such as "Valves" at this level.
     Documentation at /docs/config/server.html
 -->
<Server port="-1" shutdown="SHUTDOWN">
    <Listener className="org.apache.catalina.startup.VersionLoggerListener"/>
    <!-- Security listener. Documentation at /docs/config/listeners.html
    <Listener className="org.apache.catalina.security.SecurityListener" />
    -->
    <!--APR library loader. Documentation at /docs/apr.html -->
    <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on"/>
    <!-- Prevent memory leaks due to use of particular java/javax APIs-->
    <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener"/>
    <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>
    <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener"/>

    <!-- Global JNDI resources
         Documentation at /docs/jndi-resources-howto.html
    -->
    <GlobalNamingResources>
        <!-- Editable user database that can also be used by
             UserDatabaseRealm to authenticate users
        -->
        <Resource name="UserDatabase" auth="Container"
                  type="org.apache.catalina.UserDatabase"
                  description="User database that can be updated and saved"
                  factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
                  pathname="conf/tomcat-users.xml"/>
    </GlobalNamingResources>

    <!-- A "Service" is a collection of one or more "Connectors" that share
         a single "Container" Note:  A "Service" is not itself a "Container",
         so you may not define subcomponents such as "Valves" at this level.
         Documentation at /docs/config/service.html
     -->
    <Service name="Catalina">
        <Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>
        <Connector port="8009" protocol="AJP/1.3" redirectPort="8443"/>
        <Engine defaultHost="localhost" name="Catalina">
            <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/>
            <Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true" xmlNamespaceAware="false"
                  xmlValidation="false">
            </Host>
        </Engine>
    </Service>

    <Service name="Catalina1">
        <Connector connectionTimeout="20000" port="8099" protocol="HTTP/1.1" redirectPort="8443"/>
        <Connector port="8009" protocol="AJP/1.3" redirectPort="8443"/>
        <Engine defaultHost="localhost" name="Catalina1">
            <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/>
            <Host appBase="webapps1" autoDeploy="true" name="localhost" unpackWARs="true" xmlNamespaceAware="false"
                  xmlValidation="false">
                <Context docBase="tomcat_v2-0.0.1-SNAPSHOT" path="" reloadable="true" crossContext="true"/>
            </Host>
        </Engine>
    </Service>

    <Service name="Catalina2">
        <Connector connectionTimeout="20000" port="8098" protocol="HTTP/1.1" redirectPort="8443"/>
        <Connector port="8009" protocol="AJP/1.3" redirectPort="8443"/>
        <Engine defaultHost="localhost" name="Catalina2">
            <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/>
            <Host appBase="webapps2" autoDeploy="true" name="localhost" unpackWARs="true" xmlNamespaceAware="false"
                  xmlValidation="false">
                <Context docBase="tomcat_v2-0.0.2-SNAPSHOT" path="" reloadable="true" crossContext="true"/>
            </Host>
        </Engine>
    </Service>
</Server>

3、Dockerfile编写

# Use an image
FROM tomcat:8-jre8

# Set the working directory to /usr/local
WORKDIR /usr/local/tomcat

# Copy the current directory contents into the container at /usr/local/tomcat
COPY ./webapps1 /usr/local/tomcat/webapps1
COPY ./webapps2 /usr/local/tomcat/webapps2
COPY ./server.xml /usr/local/tomcat/conf/server.xml

# Make port 8080, 8099, 8098, 8443 available to the world outside this container
EXPOSE 8080 8099 8098 8443

# Define environment variable

# Run Command

4、构建镜像与容器运行

# 根据当前工作目录的Dockerfile构建镜像
docker build webserver_v6 .
# 启动容器
docker run -p 8080:8080 -p 8099:8099 -p 8098:8098 webserver_v6

5、验证

访问占用8098的web application

Tomcat多端口启动web应用

访问占用8099的web application

Tomcat多端口启动web应用

3、附注

博客写得仓促,不足之处,欢迎大家指点、讨论。

 

相关标签: tomcat docker