1Introduction to cluster configuration
Apache Tomcat is arguably the most popular open source Java web server out there. A single instance of Tomcat may not be able to meet the needs of increasing traffic. In this case, consider running Tomcat in a "cluster" environment; in this environment, the workload of the web server is distributed among multiple Tomcat instances.
Load balancing: After a front-end server (often called a "load balancer", "proxy balancer" or "reverse proxy") receives an HTTP request, the front-end server distributes the request to more than one "worker" web server on the back-end, which is handled by They actually handle the request. Load balancing can eliminate a single point of failure in the backend, and can achieve high availability, high scalability, and more reasonable resource optimization for any web service.
Session Replication: Session replication is a mechanism that replicates the entire state of a client session in its entirety to two or more server instances in a cluster for fault tolerance and failover. Typically, a distributed state service replicates the state of client sessions across multiple different server instances in the cluster.
Cluster: A cluster consists of two or more web server instances that work in unison to transparently handle client requests. Clients see a set of server instances as a single entity service. The purpose of a cluster is to provide high-availability services to clients while utilizing all available computing resources as efficiently as possible.
2 Configuration process
2.1 Environmental Requirements
The following are the specific requirements for building a Tomcat cluster. I am assuming 4 Ubuntu servers in this tutorial.
Apache server #1: (106) Apache HTTP web server with mod_jk (acts as a proxy balancer)
Tomcat Server #2: (107-109) Java Runtime 6.x or later, and Apache Tomcat 7.x (acting as a worker web server).
The Apache web server acts as a proxy balancer. The Apache web server is the only one visible to the client, all Tomcat instances are hidden from the client. After the mod_jk plugin is activated, the Apache web server forwards any inbound HTTP requests to the Tomcat worker instances in the cluster.
2.2 apache server installation
Tomcat Connectors allow you to connect Tomcat to other open source web servers. For the Apache web server, Tomcat Connectors come as an Apache module called mod_jk. An Apache web server with mod_jk can turn an Ubuntu server into a proxy balancer.
$ sudo apt-get install apache2 libapache2-mod-jk
2.3 tomcat server installation
1) JDK installation
$ apt-cache search openjdk
After checking the latest version of OpenJDK, install it with apt-get
$ sudo apt-get install openjdk-7-jdk
$ vi ~/.bashrc set environment variables
JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64
PATH=$PATH:$HOME/bin:$JAVA_HOME/bin
2) Tomcat installation
Copy tomcat to server
$ sudo tar xvzf apache-tomcat-7.0.35.tar.gz -C /opt
CATALINA_HOME=/opt/apache-tomcat-7.0.35
Reload .bashrc file as follows.
$ source ~/.bashrc
Now enable Tomcat by running the following startup script.
$ sudo $CATALINA_HOME/bin/startup.sh
2.4 apache server configuration
On Ubuntu, the mod_jk configuration file is located at /etc/apache2/mods-enabled/jk.conf. Updates are as follows:
<IfModule jk_module>
# We need a workers file exactly once
# and in the global server
JkWorkersFile /etc/libapache2-mod-jk/workers.properties
# JK error log
# You can (and should) use rotatelogs here
JkLogFile /var/log/apache2/mod_jk.log
# JK log level (trace,debug,info,warn,error)
JkLogLevel info
JkShmFile /var/log/apache2/jk-runtime-status
JkWatchdogInterval 60
JkMount /* loadbalancer
JkMount /jk-status jkstatus
# Configure access to jk-status and jk-manager
# If you want to make this available in a virtual host,
# either move this block into the virtual host
# or copy it logically there by including "JkMountCopy On"
# in the virtual host.
# Add an appropriate authentication method here!
<Location /jk-status>
# Inside Location we can omit the URL in JkMount
JkMount jk-status
Order deny,allow
Deny from all
Allow from 127.0.0.1
</Location>
<Location /jk-manager>
# Inside Location we can omit the URL in JkMount
JkMount jk-manager
Order deny,allow
Deny from all
Allow from 127.0.0.1
</Location>
</IfModule>
If it is under a virtual machine, you also need to add it to the following /etc/apache2/sites-available/000-default.conf file:
JkMount /* loadbalancer
JkMount /jk-status jkstatus
Create or edit etc/libapache2-mod-jk/workers.properties with the following content:
worker.list=loadbalancer,jkstatus # 为192.168.1.100配置Tomcat实例
worker.tomcat1.type=ajp13
worker.tomcat1.host=192.168.1.100
worker.tomcat1.port=8081
# worker“tomcat1”
worker.tomcat1.connection_pool_size=200
worker.tomcat1.connection_pool_timeout=600
# worker“tomcat1”将要求操作系统发送关于连接的KEEP-ALIVE信号。
worker.tomcat1.socket_keepalive=1
# 为192.168.1.200配置Tomcat实例
worker.tomcat2.type=ajp13
worker.tomcat2.host=192.168.1.200
worker.tomcat2.port=8082
worker.tomcat2.connection_pool_size=200
worker.tomcat2.connection_pool_timeout=600
# worker“tomcat2”将要求操作系统发送关于连接的KEEP-ALIVE信号。
worker.tomcat2.socket_keepalive=1
worker.jkstatus.type=status
worker.loadbalancer.type=lb
worker.loadbalancer.balance_workers=tomcat1,tomcat2
2.5 tomcat server configuration
2.5.1 Setting session store in tomcat multicast mode
sudo vim /opt/apache-tomcat-7.0.30/conf/server.xml
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOptions="8">
<Manager className="org.apache.catalina.ha.session.DeltaManager" expireSessionsOnShutdown="false" notifyListenersOnReplication="true"/>
<Channel className="org.apache.catalina.tribes.group.GroupChannel">
<Membership className="org.apache.catalina.tribes.membership.McastService"
address="228.0.0.4"
port="45564"
frequency="500"
dropTime="3000"/>
<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
address="you ip address"
port="4000"
autoBind="100"
selectorTimeout="5000"
maxThreads="6"/>
<Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
<TransportclassName="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
</Sender>
<InterceptorclassName="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
</Channel>
<Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
filter=""/>
<Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>
<Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"
tempDir="/tmp/war-temp/"
deployDir="/tmp/war-deploy/"
watchDir="/tmp/war-listen/"
watchEnabled="false"/>
<ClusterListenerclassName="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener>
<ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener">
</Cluster>
Implementation of tomcat cluster and load balancing (session synchronization)
Because the session synchronization function of tomcat needs to use multicast, Windows enables the multicast service by default, but Linux does not enable it by default.
Can be opened by command
route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0
If you need to enable multicast when the server starts, you need to add it to the /etc/sysconfig/static-routes file
eht0 net 224.0.0.0 netmask 240.0.0.0
2.5.2 Setting session store in redis sharing mode
Redis already has components that support using redis as the default session storage of tomcat under tomcat7 settings. The following describes the configuration process
1. Download redis from http://redis.io/ and follow the redis server
wget http://download.redis.io/redis-stable.tar.gz
tar xvzf redis-stable.tar.gz
cd redis-stable
make
2. Start redis
cd RedisDirectory/src
./redis-server --port 6379
3. Download tomcat7 from http://tomcat.apache.org/download-70.cgi, follow tomcat7
4. Download jedis as java's redis client from https://github.com/xetorthio/jedis/downloads, and download tomcat's redis session from https://github.com/jcoleman/tomcat-redis-session-manager/downloads manager plugin, download apache's common pool package from http://commons.apache.org/proper/commons-pool/download_pool.cgi, and copy these jar packages to the lib directory of tomcat7
5. Modify the context.xml file under tomcat's conf, add or modify the following configuration
<Valve className="com.radiadesign.catalina.session.RedisSessionHandlerValve" />
<Manager className="com.radiadesign.catalina.session.RedisSessionManager"
host="localhost" <!-- optional: defaults to "localhost" -->
port="6379" <!-- optional: defaults to "6379" -->
database="0" <!-- optional: defaults to "0" -->
maxInactiveInterval="60" <!-- optional: defaults to "60" (in seconds) --> />
6. After restarting tomcat, you can see that the session is stored in redis.