本文介紹了如何在tomcat中部署一個(gè)web app。?
1. 在tomcat中部署一個(gè)web app有兩類做法。第一,靜態(tài)部署。此類部署就是將web app的文件拷貝到相應(yīng)的路徑,作一些配置,然后重啟tomcat,在tomcat啟動的過程中,會將這個(gè)web應(yīng)用部署到tomcat中。第二,動態(tài)部署。動態(tài)部署tomcat支持兩種方式,第一就是使用tomcat manager,這是tomcat 5 core包中就自帶的一個(gè)web app,用這個(gè)tomcat manager就可以動態(tài)的部署一個(gè)web應(yīng)用,無需重啟tomcat;第二,使用tomcat deployer,這個(gè)包可以在tomcat 5的網(wǎng)站上下載,相比tomcat manager,tomcat deployer(也就是TCD-Tomcat Client Deployer)提供了更多的功能。TCD部署應(yīng)用就是通過一個(gè)命令行的界面和tomcat manager做交互,最終達(dá)到部署的目的,不過相比manager,TCD提供了編譯,驗(yàn)證一個(gè)web應(yīng)用,將一個(gè)web應(yīng)用打包成war文件的額外功能。TCD的安裝需要預(yù)先安裝JRE和ANT,而且對于EasyCluster來說,我們不太需要這個(gè)東西,所以這里就不對TCD做描述了,有興趣的話可以直接看這篇tomcat的文檔。?
2. 下面描述在tomcat中部署一個(gè)應(yīng)用的邏輯和配置。在Tomcat 4.x中,我們都知道,要部署一個(gè)web應(yīng)用,要修改server.xml,在其中加入<context....這樣的配置,對于tomcat來說,一個(gè)context就是一個(gè)web應(yīng)用。在Tomcat 5中,情況有些變化,context的概念沒變,但是Tomcat 5不鼓勵將context的配置直接寫在server.xml中,這也是一個(gè)清晰邏輯的好做法,那么,在Tomcat 5中,tomcat 5是如何查找一個(gè)web應(yīng)用的context配置的呢?邏輯如下:?
(1). $CATALINA_HOME/conf/[enginename]/[hostname]/context.xml?
(2). $CATALINA_HOME/webapps/[webappname]/META-INF/context.xml?
Tomcat 5就是到上面的兩個(gè)地方去查找web應(yīng)用的context配置的。這里enginename可以在server.xml中進(jìn)行配置,默認(rèn)就是Catalina(也就是Tomcat 5的包解開后在server.xml中的配置)。如果我們沒有配置一個(gè)web應(yīng)用的context,而我們已經(jīng)將web應(yīng)用的文件拷貝到了相應(yīng)的位置,那么Tomcat 5會自動為我們在(1)的這個(gè)路徑下創(chuàng)建一個(gè)名為[webappname].xml的context配置。不過注意,如果我們手動配置了context,那么,文件名不一定要是[webappname].xml,因?yàn)門omcat在判定一個(gè)web app的名字的時(shí)候,是根據(jù)context中的配置,而不是文件名。 ?
3. 下面開始介紹如何用靜態(tài)部署的方法在tomcat中部署一個(gè)web app。這很簡單,在server.xml中,有一個(gè)配置叫Host,在Host的配置中,可以配置一個(gè)叫appBase的attribute(可以在server.xml中搜索該關(guān)鍵字),這就是tomcat查找web app的根目錄。默認(rèn)情況下,這個(gè)appBase就是webapps,也就是說,我們只需要把web app的文件拷貝到這個(gè)appBase指定的目錄下,然后重啟tomcat,tomcat就會為我們部署這個(gè)應(yīng)用。(也可以將war文件拷貝到appBase指定的目錄下,效果是一樣的)。?
具體來說,tomcat在啟動的時(shí)候,是這樣部署應(yīng)用的:?
(1) 首先查找所有的Context Descriptor,也就是2點(diǎn)中提到的那兩個(gè)查找路徑,tomcat在這兩個(gè)路徑下查找所有的context的配置,優(yōu)先部署這些應(yīng)用。?
(2) tomcat在appBase指定的目錄下查找web應(yīng)用并部署。如果發(fā)現(xiàn)一個(gè)web應(yīng)用既有文件目錄的形式,也有一個(gè)對應(yīng)的war文件,那么查看war文件的時(shí)間戳和對應(yīng)文件,目錄的時(shí)間戳,如果war文件的時(shí)間戳比較新,那么解開war文件,再部署該應(yīng)用。?
(3) 部署剩余的war文件。?
注:如果在server.xml中,定義了deployOnStartup=false,那么tomcat將不會執(zhí)行上述的靜態(tài)部署邏輯。此外,正如2中所說,如果tomcat在部署一個(gè)web app的時(shí)候,沒有找到對應(yīng)的context配置(Context Descriptor),也就是說,我們沒有手動配置context,那么,tomcat會在$CATALINA_HOME/conf/[enginename]/[hostname]目錄下自動生成一個(gè)。
1. 在tomcat中部署一個(gè)web app有兩類做法。第一,靜態(tài)部署。此類部署就是將web app的文件拷貝到相應(yīng)的路徑,作一些配置,然后重啟tomcat,在tomcat啟動的過程中,會將這個(gè)web應(yīng)用部署到tomcat中。第二,動態(tài)部署。動態(tài)部署tomcat支持兩種方式,第一就是使用tomcat manager,這是tomcat 5 core包中就自帶的一個(gè)web app,用這個(gè)tomcat manager就可以動態(tài)的部署一個(gè)web應(yīng)用,無需重啟tomcat;第二,使用tomcat deployer,這個(gè)包可以在tomcat 5的網(wǎng)站上下載,相比tomcat manager,tomcat deployer(也就是TCD-Tomcat Client Deployer)提供了更多的功能。TCD部署應(yīng)用就是通過一個(gè)命令行的界面和tomcat manager做交互,最終達(dá)到部署的目的,不過相比manager,TCD提供了編譯,驗(yàn)證一個(gè)web應(yīng)用,將一個(gè)web應(yīng)用打包成war文件的額外功能。TCD的安裝需要預(yù)先安裝JRE和ANT,而且對于EasyCluster來說,我們不太需要這個(gè)東西,所以這里就不對TCD做描述了,有興趣的話可以直接看這篇tomcat的文檔。?
2. 下面描述在tomcat中部署一個(gè)應(yīng)用的邏輯和配置。在Tomcat 4.x中,我們都知道,要部署一個(gè)web應(yīng)用,要修改server.xml,在其中加入<context....這樣的配置,對于tomcat來說,一個(gè)context就是一個(gè)web應(yīng)用。在Tomcat 5中,情況有些變化,context的概念沒變,但是Tomcat 5不鼓勵將context的配置直接寫在server.xml中,這也是一個(gè)清晰邏輯的好做法,那么,在Tomcat 5中,tomcat 5是如何查找一個(gè)web應(yīng)用的context配置的呢?邏輯如下:?
(1). $CATALINA_HOME/conf/[enginename]/[hostname]/context.xml?
(2). $CATALINA_HOME/webapps/[webappname]/META-INF/context.xml?
Tomcat 5就是到上面的兩個(gè)地方去查找web應(yīng)用的context配置的。這里enginename可以在server.xml中進(jìn)行配置,默認(rèn)就是Catalina(也就是Tomcat 5的包解開后在server.xml中的配置)。如果我們沒有配置一個(gè)web應(yīng)用的context,而我們已經(jīng)將web應(yīng)用的文件拷貝到了相應(yīng)的位置,那么Tomcat 5會自動為我們在(1)的這個(gè)路徑下創(chuàng)建一個(gè)名為[webappname].xml的context配置。不過注意,如果我們手動配置了context,那么,文件名不一定要是[webappname].xml,因?yàn)門omcat在判定一個(gè)web app的名字的時(shí)候,是根據(jù)context中的配置,而不是文件名。 ?
3. 下面開始介紹如何用靜態(tài)部署的方法在tomcat中部署一個(gè)web app。這很簡單,在server.xml中,有一個(gè)配置叫Host,在Host的配置中,可以配置一個(gè)叫appBase的attribute(可以在server.xml中搜索該關(guān)鍵字),這就是tomcat查找web app的根目錄。默認(rèn)情況下,這個(gè)appBase就是webapps,也就是說,我們只需要把web app的文件拷貝到這個(gè)appBase指定的目錄下,然后重啟tomcat,tomcat就會為我們部署這個(gè)應(yīng)用。(也可以將war文件拷貝到appBase指定的目錄下,效果是一樣的)。?
具體來說,tomcat在啟動的時(shí)候,是這樣部署應(yīng)用的:?
(1) 首先查找所有的Context Descriptor,也就是2點(diǎn)中提到的那兩個(gè)查找路徑,tomcat在這兩個(gè)路徑下查找所有的context的配置,優(yōu)先部署這些應(yīng)用。?
(2) tomcat在appBase指定的目錄下查找web應(yīng)用并部署。如果發(fā)現(xiàn)一個(gè)web應(yīng)用既有文件目錄的形式,也有一個(gè)對應(yīng)的war文件,那么查看war文件的時(shí)間戳和對應(yīng)文件,目錄的時(shí)間戳,如果war文件的時(shí)間戳比較新,那么解開war文件,再部署該應(yīng)用。?
(3) 部署剩余的war文件。?
注:如果在server.xml中,定義了deployOnStartup=false,那么tomcat將不會執(zhí)行上述的靜態(tài)部署邏輯。此外,正如2中所說,如果tomcat在部署一個(gè)web app的時(shí)候,沒有找到對應(yīng)的context配置(Context Descriptor),也就是說,我們沒有手動配置context,那么,tomcat會在$CATALINA_HOME/conf/[enginename]/[hostname]目錄下自動生成一個(gè)。
?
4. 在tomcat中動態(tài)部署一個(gè)web應(yīng)用(不使用Tomcat manager和TCD)。在這種情況下,要想部署一個(gè)應(yīng)用,只有一種做法,那就是利用Tomcat的autoDeploy。在server.xml中,有一個(gè)配置項(xiàng)叫Host,前面已經(jīng)提到了,在Host中可以配置appBase,一樣的,autoDeploy也可以配置在這個(gè)Host配置項(xiàng)中,如果autoDeploy被設(shè)成了true,那么,當(dāng)以下情況發(fā)生時(shí),Tomcat將部署/重部署一個(gè)web應(yīng)用(注:以下文字中的exploded web application指的那些沒有被打成war包的web應(yīng)用,也就是一對文件和目錄的那種web應(yīng)用):?
(1) Deployment of .WAR files copied into the Host appBase.?
(2) Deployment of exploded web applications which are copied into the Host appBase.?
(3) Re-deployment of a web application which has already been deployed from a .WAR when the new .WAR is provided. In this case the exploded web application is removed, and the .WAR is expanded again. Note that the explosion will not occur if the Host is configured so that .WARs are not exploded with a unpackWARs attribute set to "false", in which case the web application will be simply redeployed as a compressed archive.?
(4) Re-deployment of a web application if the /WEB-INF/web.xml file (or any other resource defined as a WatchedResource) is updated.?
(5) Re-deployment of a web application if the Context Descriptor file from which the web application has been deployed is updated.?
(6) Re-deployment of a web application if a Context Descriptor file (with a filename corresponding to the Context path of the previously deployed web application) is added to the $CATALINA_HOME/conf/[enginename]/[hostname]/ directory.?
(7) Undeployment of a web application if its document base (docBase) is deleted. Note that on Windows, this assumes that anti-locking features (see Context configuration) are enabled, otherwise it is not possible to delete the resources of a running web application.?
很清楚了吧?一旦war文件更新,web.xml更新,Context Descriptor更新,web應(yīng)用的docBase被刪除,等等這類事件都會導(dǎo)致Tomcat部署/重部署一個(gè)web應(yīng)用。不過我想這個(gè)autoDeploy設(shè)成true之后,會給Tomcat帶來一些性能的損失,所以如果在產(chǎn)品應(yīng)用場合,應(yīng)該把這個(gè)autoDeploy關(guān)掉。?
注:也可以在server.xml中的loader配置項(xiàng)中配置autoDeploy,此時(shí)loader(負(fù)責(zé)load class)會監(jiān)控文件的更新情況從而完成部署/重部署。?
(1) Deployment of .WAR files copied into the Host appBase.?
(2) Deployment of exploded web applications which are copied into the Host appBase.?
(3) Re-deployment of a web application which has already been deployed from a .WAR when the new .WAR is provided. In this case the exploded web application is removed, and the .WAR is expanded again. Note that the explosion will not occur if the Host is configured so that .WARs are not exploded with a unpackWARs attribute set to "false", in which case the web application will be simply redeployed as a compressed archive.?
(4) Re-deployment of a web application if the /WEB-INF/web.xml file (or any other resource defined as a WatchedResource) is updated.?
(5) Re-deployment of a web application if the Context Descriptor file from which the web application has been deployed is updated.?
(6) Re-deployment of a web application if a Context Descriptor file (with a filename corresponding to the Context path of the previously deployed web application) is added to the $CATALINA_HOME/conf/[enginename]/[hostname]/ directory.?
(7) Undeployment of a web application if its document base (docBase) is deleted. Note that on Windows, this assumes that anti-locking features (see Context configuration) are enabled, otherwise it is not possible to delete the resources of a running web application.?
很清楚了吧?一旦war文件更新,web.xml更新,Context Descriptor更新,web應(yīng)用的docBase被刪除,等等這類事件都會導(dǎo)致Tomcat部署/重部署一個(gè)web應(yīng)用。不過我想這個(gè)autoDeploy設(shè)成true之后,會給Tomcat帶來一些性能的損失,所以如果在產(chǎn)品應(yīng)用場合,應(yīng)該把這個(gè)autoDeploy關(guān)掉。?
注:也可以在server.xml中的loader配置項(xiàng)中配置autoDeploy,此時(shí)loader(負(fù)責(zé)load class)會監(jiān)控文件的更新情況從而完成部署/重部署。?
有關(guān)Tomcat manager部署應(yīng)用,有一篇單獨(dú)的文檔介紹。而有關(guān)TCD的東西,這里就不介紹了。 ?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
