1 回答

TA貢獻1860條經驗 獲得超8個贊
在 Apache Tomcat 中,您可以配置多個虛擬主機,每個虛擬主機部署相同的 .war 文件(或文檔庫),同時具有不同的上下文配置參數,如 JDBC 連接、資源、外部 JAR 文件等。
要堅持您的方案 (1),請在server.xml配置兩個域的主機元素時:
<Engine name="Catalina" defaultHost="subdomain1.maindomain1.com">
<Host name="subdomain1.maindomain1.com" appBase="subdomain1.maindomain1.com"/>
<Host name="anysubmain.anothermaindomain.com" appBase="anysubmain.anothermaindomain.com"/>
</Engine>
并為兩者創建資源和配置文件夾:
mkdir $CATALINA_HOME/subdomain1.maindomain1.com
mkdir $CATALINA_HOME/anysubmain.anothermaindomain.com
mkdir $CATALINA_HOME/conf/Catalina/subdomain1.maindomain1.com
mkdir $CATALINA_HOME/conf/Catalina/anysubmain.anothermaindomain.com
然后為每個主機創建一個ROOT.xmleach 指向相同的代碼庫(例如 .war 文件)但不同的數據庫配置。通常,這為每個域提供了不同的上下文配置。
$CATALINA_HOME/conf/Catalina/subdomain1.maindomain1.com/ROOT.xml
<Context docBase="/path/to/your/webapp.war" path="">
<Resource name="jdbc/Database" auth="Container" type="javax.sql.DataSource"
username="subdomain1_maindomain1_com" password="anysecurepassword" driverClassName="com.your.jdbc.Driver"
url="jdbc:xyz://localhost:321/subdomain1_maindomain1_com_dbname"/>
...
</Context>
$CATALINA_HOME/conf/Catalina/anysubmain.anothermaindomain.com/ROOT.xml
<Context docBase="/path/to/your/webapp.war" path="">
<Resource name="jdbc/Database" auth="Container" type="javax.sql.DataSource"
username="anysubmain_anothermaindomain_com" password="anysecurepassword" driverClassName="com.your.jdbc.Driver"
url="jdbc:xyz://localhost:321/anysubmain_anothermaindomain_com_dbname"/>
...
</Context>
此外,為了實現方案 2,您可以為每個域配置不同的外部資源文件夾。
EG for anysubmain_anothermaindomain_com_dbnamein$CATALINA_HOME/conf/Catalina/anysubmain.anothermaindomain.com/ROOT.xml
<Context>
...
<Resources>
<PreResources base="/path/to/anysubmain_anothermaindomain_com_dbname/jarfiles/"
className="org.apache.catalina.webresources.DirResourceSet" readOnly="true"
internalPath="/" webAppMount="/WEB-INF/lib" />
</Resources>
...
</Context>
這樣,所有域的 Web 應用程序都基于相同的 docBase,但可以添加不同的(變體)jar 文件或其他資源依賴項。
添加回答
舉報