Expandability is an extremely important concept for anyone wanting to really get into production-type architecture and high availability. It allows you to run many more Web sites or applications in your Web server environment with greater availability than ever before. Of course, such advantages always come at a price. A true architectural solution for what your business needs are should be decided by your management and an IT architect. However, I'm always a fan of techies with extra hardware and too much time on their hands, so if you'd like to explore the concepts below on your own servers, I certainly encourage that!
Virtual hosting is simply the concept of allowing one server instance configured on one or more physical server nodes to host two or more applications at the same time. This is an extremely common practice on the Web, mainly because most individuals and small companies cannot afford their own dedicated servers to host their Web applications. With shared space and processes along with ever-improving virtual server software, this is relatively easy for a Web administrator to set up.
Virtual hosting has its obvious limitations, like one application or Web site being codependent on resources with another. What if one application brings a whole server down, which (surprisingly) happens quite often? All other applications are affected. The only true way to remove (or come close to it) such dependencies is to try to eliminate single points of failure in your server environment. That's where clustering comes in.
To use virtual hosts in Tomcat, you just need to set up the Domain Name System (DNS) or hosts data for the host. (For testing, making an IP alias for localhost is sufficient.)
Then, you must add the lines in Listing 19 to the server.xml configuration file.
Listing 19. Set up DNS for the host
<Engine name="Catalina" defaultHost="localhost" debug="0"> <!-- This host is the default host. --> <Host name="localhost" debug="0" appBase="webapps" unpackWARs="true" autoDeploy="true"> <Context path="" docBase="ROOT" debug="0"/> </Context> </Host> <!-- This host is the first virtual host: www.example.com. --> <Host name="www.example.com" appBase="/home/example/webapp"> <Context path="" docBase="."/> </Host> </Engine>
Tomcat's server.xml file, as distributed, contains only one virtual host, but
it's easy to add support for additional virtual hosts. The simplified version
of the server.xml file in Listing 19 shows the overall additional structure
needed to add one virtual host. Each
element must have one or more
elements within it; one of these must be the default context for this host,
which is specified by having its relative path set to the empty string (for
Note: If you've configured Tomcat to support multiple virtual hosts (Web sites), you need to configure a manager for each.
A default Tomcat installation includes the manager. To add an instance of the manager Web application context to a new host, install the manager.xml context configuration file in the $CATALINA_HOME/conf/enginename/hostname folder, as shown in Listing 20.
Listing 20. Example manager context path
<Context path="/manager" debug="0" privileged="true" docBase="/opt/tomcat/localhost/webapps/manager"> </Context>
Clustering is the second important concept of expandability that you should understand in a Web server environment. In contrast to virtual hosting, which allows you to serve multiple applications from a single server instance, clustering allows you to run multiple server instances from different physical servers while providing robust session and load management. In any true production environment, this is a must. Its purposes are three-fold:
- It removes single points of failure in the environment from a software or hardware level. Two servers installed on one physical node provide what's called vertical scaling to failover at a software level. However, this does not suffice if there were a hardware failure. Horizontal scaling provides physical and logical failover, as is the case with true clustering.
- Clustering has the potential to improve performance. Certainly, if your application has its own stand-alone server mansion to exhaust all the resources by its lonesome, then this doesn't hold true. However, most applications share one server with many other applications. When you add more servers and more resources—memory, CPU, and so on—you get increased performance.
- Clustering is transparent to users. Even if a server goes down in the middle of a user's transaction, session persistence handled through a persistent session database will hold the session data and immediately transfer it to the other server.
Considering how extensive a topic clustering is, this tutorial doesn't get into its implementation. If you truly are hosting many applications or plan to from your Tomcat servers, see Resources for more information.