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 文件或其他资源依赖项。
添加回答
举报
