Possible duplicate of this but answer is not accepted.
I have 2 scenarios
subdomain1.maindomain1.com
and anysubmain.anothermaindomain.com
should be pointed to same webapp folder. And depending on domain, we will select database dynamically but codebase will remain same. Point to note here : Whole codebase remains same . manage.domain.com
crm.domain.com
equote.domain.com
should be pointing to same webapp folder. And depending on domain we will load default servlet file. Point to note here : Part of codebase will remain same for all domains. Ex. core architect files . What solutions other have suggested
Note: I can create 2 AWS EC2 instances for above 2 scenarios. It means that I am not expecting one solution to above 2 problems.
In Apache Tomcat you can configure multiple virtual hosts that each deploy the same .war file (or document base) wile having different context configuration parameters like JDBC connection, ressources, esternal JAR files and others.
To stick with your scenario (1), in server.xml
configure both domains' host elements:
<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>
And create resource and configuration folders for both:
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
Then for each host create a ROOT.xml
each pointing to the same code base (eg .war file) but different data bases configuration . In general this providing a diffent context configuration for each domain.
$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>
Additionally, in order to implement scenario 2, for each domain you can configure different external resource folders .
EG for anysubmain_anothermaindomain_com_dbname
in $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>
This way all domain's web application base on the same docBase but can have different (variants of) jar files or other resource dependencies added.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.