此文源自一次多年前面試的面試題,民工哥將它總結出來分享給你們,但願對你們有所幫助,或者從此的面試中說不定會用的上。 html
首先,咱們瞭解一下常見的Java Web服務器。 java
Tomcat就是實際環境中最多見的,不少時候,特別是像在平時的測試環境,常常會遇到多個項目同時測試的狀況,因此,今天民工哥與你們來聊一聊如何在一個Tomcat服務下,同時部署多個應用項目。web
一、不修改端口面試
你們都知道,應用項目是直接放在Tomcat webapps目錄下面apache
[root@CentOS7-1 tomcat]# cd webapps/ [root@CentOS7-1 webapps]# ll total 4 drwxr-x--- 16 root root 4096 Jun 4 03:07 docs drwxr-x--- 6 root root 83 Jun 4 03:07 examples drwxr-x--- 5 root root 87 Jun 4 03:07 host-manager drwxr-x--- 5 root root 103 Jun 4 03:07 manager drwxr-x--- 3 root root 283 Jun 4 03:07 ROOT
因此,咱們在不修改端口的狀況下,能夠直接在此目錄下新增多個項目目錄,也能夠直接將war包放在此目錄下,因爲測試環境,咱們直接模擬war解壓後的目錄,用添加目錄來替代。bootstrap
[root@CentOS7-1 webapps]# mkdir test java [root@CentOS7-1 webapps]# ls docs examples host-manager java manager ROOT test
準備測試的首頁文件tomcat
[root@CentOS7-1 webapps]# echo "this is a test" >test/test.html [root@CentOS7-1 webapps]# echo "this is a java" >java/java.html [root@CentOS7-1 webapps]# cat test/test.html this is a test [root@CentOS7-1 webapps]# cat java/java.html this is a java
修改配置文件服務器
<!-- test --> <Context path="test/" docBase="test" reloadable="true" /> <Context path="java/" docBase="java" reloadable="true" /> #增長上兩行配置便可 </Host> </Engine> </Service> </Server>" ../conf/server.xml" 173L, 7744C
docBase屬性: 指定Web應用的文件路徑,能夠是絕對路徑,也能夠給定相對路徑path屬性: 指定訪問該Web應用的URL入口。app
reloadable屬性: 若這個屬性爲true,tomcat服務器在運行狀態下會監視WEB-INF/classes和WEB-INF/lib目錄下class文件的改動,若是監測到class文件被更新,服務器會自動從新加載Web應用。curl
重啓Tomcat服務,測試訪問,結果以下:
部署成功。
注:配置文件中增長的配置步驟能夠不作,直接跳過,不是必需要作的步驟。
二、修改端口
第二種修改端口的方法,實際上是基於第一種方法作出的改良,在tomcat目錄下建立多個webapps目錄。
刪除webapps目錄下的java項目,並刪除webapps1目錄下test項目便可。
修改配置文件
server.xml已有第一個項目的配置信息,如今須要新增第二個項目的配置,在Server節點下,新增一個Service節點,第2個Service節點直接複製第1個Service內容修改便可。
<Service name="Catalina1"> <!--The connectors can use a shared executor, you can define one or more named thread pools--> <Connector port="8081" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> <!-- A "Connector" using the shared thread pool--> <Engine name="Catalina1" defaultHost="localhost"> <!-- Use the LockOutRealm to prevent attempts to guess user passwords via a brute-force attack --> <Realm className="org.apache.catalina.realm.LockOutRealm"> <!-- This Realm uses the UserDatabase configured in the global JNDI resources under the key "UserDatabase". Any edits that are performed against this UserDatabase are immediately available for use by the Realm. --> <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/> </Realm> <Host name="localhost" appBase="webapps1" 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>
只須要注意修改幾個配置便可,不太熟悉的讀者,能夠利用代碼工具比較一下,就會發現兩段配置的不一樣,這裏就很少解釋了。
重啓服務並測試訪問
[root@CentOS7-1 conf]# ../bin/startup.sh Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar Tomcat started. [root@CentOS7-1 conf]# lsof -i :8080 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME java 2486 root 52u IPv6 25075 0t0 TCP *:webcache (LISTEN) [root@CentOS7-1 conf]# lsof -i :8081 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME java 2486 root 57u IPv6 25079 0t0 TCP *:tproxy (LISTEN) [root@CentOS7-1 conf]# curl http://127.0.0.1:8080/test/test.html this is a test [root@CentOS7-1 conf]# curl http://127.0.0.1:8081/java/java.html this is a java