一个tomcat下部署不同端口多个应用

通过配置tmcat的server.xml来实现多端口多应用:

<?xml version='1.0' encoding='utf-8'?>

<Server port="8005" shutdown="SHUTDOWN">
  <!-- 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" />
  <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->
  <Listener className="org.apache.catalina.core.JasperListener" />
  <!-- 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" />

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

 
  <Service name="Catalina">

    <Connector port="8080" protocol="org.apache.coyote.http11.Http11AprProtocol" URIEncoding="UTF-8" 
               enableLookups="false" 
               acceptCount="500"    
               connectionTimeout="5000" 
               disableUploadTimeout="true" maxThreads="1500" maxSpareThreads="50" minSpareThreads="25"
               redirectPort="8443" />

	<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
    <Engine name="Catalina" defaultHost="localhost">
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>

      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
		<Context docBase="/data/java/appstore-api" path="/" reloadable="false" />
      </Host>
    </Engine>
  </Service>

   <Service name="web">
    <Connector port="8088"   protocol="HTTP/1.1" maxThreads="700" acceptCount="100" connectionTimeout="2000" redirectPort="8445" URIEncoding="UTF-8"/>

    <Engine name="web" defaultHost="localhost">
        <Host name="localhost" appBase="webapp1">
          <Context docBase="/data/java/appstore-web" path="" reloadable="true" />
		</Host>
	</Engine>
  </Service>
  
  <Service name="web2">
    <Connector port="8089"   protocol="HTTP/1.1" maxThreads="700" acceptCount="100" connectionTimeout="2000" redirectPort="8445" URIEncoding="UTF-8"/>
    <Engine name="web2" defaultHost="localhost">
        <Host name="localhost" appBase="webapp2">
          <Context docBase="/data/java/appstore-web2" path="" reloadable="true" />
		</Host>
	</Engine>
  </Service>
</Server>
1、按照上面的配置,启动tomcat,可以发现:

1)启动tomcat后,在tomcat配置目录:/usr/local/tomcat/conf下,除了默认的Catalina文件夹外,还会生成web1和web2两个文件夹。原因是Engine组件中,指定了所用的web1和web2。当然,我们可以将不同service组件的Engine name都指定成Catalina

2)启动tomcat后,在tomcat目录:/usr/local/tomcat下,除了默认的webapps文件夹外,还会生成webapp1和webapp2两个目录。原因是Host组件中,指定了appBase伟webapp1和webapp2.当然,我们可以将不同service组件的Host appBase指定成默认的webapps


2、配置说明:

1)appBase是虚拟主机存放webapp的目录,它可以是相对路径,也可以是绝对路径。如果是相对路径,则相对于$CATALINA_HOME,严格并准确地说是CATALINA_BASE。

2)path是URI的匹配路径,相当于nginx的location后的路径。tomcat要求每个虚拟主机必须配置一个空字符串的path,该条context作为URI无法被明确匹配时的默认context,它相当于nginx中location / {}的作用。

3)docBase则是每个webapp的存放目录(或者是已归档的war文件),它可以是相对路径,也可以是绝对路径,提供相对路径时它相对于appBase。该目录一般在appBase的目录下,但并不规定一定要放在appBase下。对于web服务来说,它相当于nginx的root指令,但对于webapp来说,一个context就相当于一个webapp,而docBase正是webapp的路径。


3、注意:

1)如果多个项目都用了spring框架,那么在配置成一个tomcat多端口多应用时,可能会报错:

13-Apr-2018 12:52:08.716 SEVERE [localhost-startStop-1] org.apache.catalina.core.StandardContext.listenerStart Exception sending context initialized event to listener instance of class org.springframework.web.util.Log4jConfigListener
 java.lang.IllegalStateException: Web app root system property already set to different value: 'webapp.root' = [/data/ttbrain-log-admin/] instead of [/data/Disu/] - Choose unique values for the 'webAppRootKey' context-param in your web.xml files!
	at org.springframework.web.util.WebUtils.setWebAppRootSystemProperty(WebUtils.java:151)
	at org.springframework.web.util.Log4jWebConfigurer.initLogging(Log4jWebConfigurer.java:116)
	at org.springframework.web.util.Log4jConfigListener.contextInitialized(Log4jConfigListener.java:45)
	at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4727)
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5189)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1419)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409)
	at java.util.concurrent.FutureTask.run(FutureTask.java:262)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
	at java.lang.Thread.run(Thread.java:745)
原因是:部署在同一容器中的Web项目,要配置不同的<param-value>,不能重复,否则就会报上面类似的错误。

2)解决方法:在web.xml中加上:

<context-param>
  <param-name>webAppRootKey</param-name>
  <param-value>webapp.root</param-value>
</context-param>

"webapp.root"这个字符串可以随便写任何字符串。如果不配置默认值是"webapp.root"。可以用System.getProperty("webapp.root")来动态获项目的运行路径。

3)加载方式:

Spring通过org.springframework.web.util.WebAppRootListener 这个监听器来运行时的项目路径。但是如果在web.xml中已经配置了 org.springframework.web.util.Log4jConfigListener这个监听器,则不需要配置WebAppRootListener了。因为Log4jConfigListener已经包含了WebAppRootListener的功能
一般配置类型下面的例子:

<!-- 加载Log4J 配置文件  -->  
<context-param>  
    <param-name>log4jConfigLocation</param-name>  
    <param-value>WEB-INF/conf/log4j.properties</param-value>  
</context-param>     
  
<context-param>  
    <param-name>log4jRefreshInterval</param-name>  
      <param-value>3000</param-value>  
 </context-param>  
  
<listener>  
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>  
 </listener>  
在log4j.properties配置文件,就可以按下面的方式使用${webapp.root}:log4j.appender.file.File=${webapp.root}/WEB-INF/logs/sample.log 就可以在运行时动态的找出项目的路径

参考:https://www.cnblogs.com/f-ck-need-u/p/8120008.html


  • 1
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Tomcat 中配置两个应用端口可以用于实现多租户或者多应用程序的部署,具体步骤如下: 1. 打开 Tomcat 的 server.xml 配置文件,该文件位于 Tomcat 的 conf 目录下。 2. 找到 <Service> 标签,该标签包含多个 <Connector> 标签,每个 <Connector> 标签代表一个端口。例如: ```xml <Service name="Catalina"> <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /> <Engine name="Catalina" defaultHost="localhost"> <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> </Host> </Engine> </Service> ``` 以上配置中,<Connector> 标签中的 port 属性分别为 8080 和 8009,代表 Tomcat 的 HTTP 和 AJP 端口。 3. 复制一份 <Connector> 标签,并修改其中的 port 属性,以设置第二个端口。例如: ```xml <Service name="Catalina"> <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /> <Connector port="8081" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> <Engine name="Catalina" defaultHost="localhost"> <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> </Host> </Engine> </Service> ``` 以上配置中,新增的 <Connector> 标签的 port 属性设置为 8081。 4. 在 Tomcat 的 webapps 目录中创建第二个应用程序的目录,并将应用程序复制到该目录中。 5. 重启 Tomcat 服务,以使上述配置生效: ```bash sudo systemctl restart tomcat ``` 至此,Tomcat 的两个应用程序都已经部署,并分别监听两个端口。例如,第一个应用程序可以通过 http://localhost:8080 访问,第二个应用程序可以通过 http://localhost:8081 访问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

赶路人儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值