个人随笔
目录
一个tomcat下部署不同端口多个应用
2019-09-26 23:17:19

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

  1. <?xml version='1.0' encoding='utf-8'?>
  2. <Server port="8005" shutdown="SHUTDOWN">
  3. <!-- Security listener. Documentation at /docs/config/listeners.html
  4. <Listener className="org.apache.catalina.security.SecurityListener" />
  5. -->
  6. <!--APR library loader. Documentation at /docs/apr.html -->
  7. <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  8. <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->
  9. <Listener className="org.apache.catalina.core.JasperListener" />
  10. <!-- Prevent memory leaks due to use of particular java/javax APIs-->
  11. <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  12. <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  13. <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
  14. <GlobalNamingResources>
  15. <Resource name="UserDatabase" auth="Container"
  16. type="org.apache.catalina.UserDatabase"
  17. description="User database that can be updated and saved"
  18. factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
  19. pathname="conf/tomcat-users.xml" />
  20. </GlobalNamingResources>
  21. <Service name="Catalina">
  22. <Connector port="8080" protocol="org.apache.coyote.http11.Http11AprProtocol" URIEncoding="UTF-8"
  23. enableLookups="false"
  24. acceptCount="500"
  25. connectionTimeout="5000"
  26. disableUploadTimeout="true" maxThreads="1500" maxSpareThreads="50" minSpareThreads="25"
  27. redirectPort="8443" />
  28. <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
  29. <Engine name="Catalina" defaultHost="localhost">
  30. <Realm className="org.apache.catalina.realm.LockOutRealm">
  31. <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
  32. resourceName="UserDatabase"/>
  33. </Realm>
  34. <Host name="localhost" appBase="webapps"
  35. unpackWARs="true" autoDeploy="true">
  36. <Context docBase="/data/java/appstore-api" path="/" reloadable="false" />
  37. </Host>
  38. </Engine>
  39. </Service>
  40. <Service name="web">
  41. <Connector port="8088" protocol="HTTP/1.1" maxThreads="700" acceptCount="100" connectionTimeout="2000" redirectPort="8445" URIEncoding="UTF-8"/>
  42. <Engine name="web" defaultHost="localhost">
  43. <Host name="localhost" appBase="webapp1">
  44. <Context docBase="/data/java/appstore-web" path="" reloadable="true" />
  45. </Host>
  46. </Engine>
  47. </Service>
  48. <Service name="web2">
  49. <Connector port="8089" protocol="HTTP/1.1" maxThreads="700" acceptCount="100" connectionTimeout="2000" redirectPort="8445" URIEncoding="UTF-8"/>
  50. <Engine name="web2" defaultHost="localhost">
  51. <Host name="localhost" appBase="webapp2">
  52. <Context docBase="/data/java/appstore-web2" path="" reloadable="true" />
  53. </Host>
  54. </Engine>
  55. </Service>
  56. </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的路径。

以下是指定相同的appBase的一个配置:

  1. <Service name="Catalina">
  2. <Connector port="8081" protocol="HTTP/1.1" maxThreads="700" acceptCount="100" connectionTimeout="2000" redirectPort="8445" URIEncoding="UTF-8"/>
  3. <Engine name="Catalina" defaultHost="localhost">
  4. <Host name="localhost" appBase="webapps">
  5. <Context docBase="testjsp" path="" reloadable="true" />
  6. </Host>
  7. </Engine>
  8. </Service>
  9. <Service name="Catalina">
  10. <Connector port="8082" protocol="HTTP/1.1" maxThreads="700" acceptCount="100" connectionTimeout="2000" redirectPort="8445" URIEncoding="UTF-8"/>
  11. <Engine name="Catalina" defaultHost="localhost">
  12. <Host name="localhost" appBase="webapps">
  13. <Context docBase="testjsp" path="" reloadable="true" />
  14. </Host>
  15. </Engine>
  16. </Service>

这里的name跟默认的Catalina一样,appBase也跟默认的webapps一样,当然建议docBase指定到别的目录,启动然后访问如下链接测试:

  1. http://localhost:8081/jspIndex
  2. http://localhost:8082/jspIndex
 142

啊!这个可能是世界上最丑的留言输入框功能~


当然,也是最丑的留言列表

有疑问发邮件到 : suibibk@qq.com 侵权立删
Copyright : 个人随笔   备案号 : 粤ICP备18099399号-2