Apache TOMCAT integrated Cluster session sharing

Source: Internet
Author: User
Tags apache php apache tomcat

Apache TOMCAT Integrated Cluster

Required Software

CentOS6.5 64apache2.2.15tomcat7.0.62jdk 1.8.0_45tomcat-connectors 1.2.40mysql< optional >PHP< optional >

An installation
A Server installation IP 192.168.150.7

Apache PHP mysqlyum install httpd httpd-devel php php-devel php-gd php-mysql php-mbstring mysql mysql-devel mysql-server  Mysql-libsservice httpd Start//start Httpdservice mysqld start//start mysql/usr/bin/mysqladmin-u root password ' new-password ' Modify MySQL Password
Java & Jdkyum list Java*yum install java-1.8.0-openjdk.x86_64 java-1.8.0-openjdk-devel.x86_64 Java-1.8.0-openjdk-headless.x86_64

Test that the Java Javac command is available and view the version after loading

[[email protected] ~]# java-versionopenjdk version "1.8.0_45" OpenJDK Runtime Environment (build 1.8.0_45-b13) OpenJDK 64- Bit Server VM (build 25.45-b02, Mixed mode) [[email protected] ~]# Tomcat Tomcat-connectorscd/usr/localwget Http://mirror S.cnnic.cn/apache/tomcat/tomcat-7/v7.0.62/bin/apache-tomcat-7.0.62.tar.gzwget http://apache.dataguru.cn/tomcat/ Tomcat-connectors/jk/tomcat-connectors-1.2.40-src.tar.gztar Xvzf APACHE-TOMCAT-7.0.62.TAR.GZMV apache-tomcat-7.0.62 Tomcattar xvzf tomcat-connectors-1.2.40-src.tar.gzcd TOMCAT-CONNECTORS-1.2.40-SRCCD native./ Configure--WITH-APXS=/USR/SBIN/APXSMAKECD APACHE-2.0/CP Mod_jk.so/etc/httpd/modules

b Server Installation IP 192.168.150.8

Yum list Java*yum install java-1.8.0-openjdk.x86_64 java-1.8.0-openjdk-devel.x86_64 java-1.8.0-openjdk-headless.x86_ 64cd/usr/localwget Http://mirrors.cnnic.cn/apache/tomcat/tomcat-7/v7.0.62/bin/apache-tomcat-7.0.62.tar.gztar Xvzf APACHE-TOMCAT-7.0.62.TAR.GZMV apache-tomcat-7.0.62 Tomcat

Two configurations
A server IP 192.168.150.7
Configure the HTTP server first

cd /etc/httpd/conftouch mod_jk.conf //New mod_jk.conf file    Note To create a new VI in the/ETC/HTTPD/CONF directory  mod_jk.conf//Edit mod_jk.conf file   Add the following   save Exit loadmodule jk_module /etc/httpd/modules/ Mod_jk.sojkoptions +forwardkeysize +forwarduricompat -forwarddirectoriesjkrequestlogformat   "%w %v %t" jkworkersfile /etc/httpd/conf/workers.propertiesjkmount /*.jsp  controllerjkmount /*.do controllerjkmount /*.jar controllerjkmount /*.class  controllerjkmount /application/* controllerjkmount /*/servlet/* controllertouch  workers.properties//New Workers.properties file   Note   to create a new vi workers.properties//in the/etc/httpd/conf directory Edit Workers.properties file   Add the following   save Exit workers.tomcat_home=/usr/local/tomcat   // Tomcat installation directory worker.list=controller,tomcat1,tomcat2 //cluster Tomcat server   later said #========tomcat1========    //First Tomcat settings WORKER.TOMCAT1.PORT=8009&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;//AJP Port   Default 8009 No modification required   Because our experiment is not in the server WORKER.TOMCAT1.HOST=LOCALHOST&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;//IP address   This machine does not need to be modified worker.tomcat1.type=ajp13worker.tomcat1.lbfactor=1                //rotation times    The bigger the more #========tomcat2========worker.tomcat2.port=8009                 // Second Tomcat set worker.tomcat2.host=192.168.150.8  //second tomcatip   because our Tomcat is on another server   To write IP address Worker.tomcat2.type=ajp13worker.tomcat2.lbfactor=1worker.controller.type=lbworker.controller.balance_ workers=tomcat1,tomcat2  //Rotation's Tomcat list Worker.controller.sticky_session=0worker.controller.sticky_ Session_force=0worker.jkstatus.type=status

Then modify the httpd.conf file
Modify the ServerName
Join at the end

Include conf/mod_jk.conf

Tomcat session Configuration 192.168.150.7 192.168.150.8 do the same configuration

Cd/usr/local/tomcat/conf

Be careful to back up the source files before modifying

CP Server.xml SERVER.XML.BAKVI Server.xml

Will

<!--Engine name= "Catalina" defaulthost= "localhost"--

Revision changed to

<engine name= "Catalina" defaulthost= "localhost" jvmroute= "TOMCAT1" >

Add the following to the <Engine> node:

<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= "224.0.0.0"                  port= "45564"                  frequency= "     "           droptime= "/>    " <receiver classname= "Org.apache.catalina.tribes.transport.nio.NioReceiver"                address= "192.168.150.7"                port= "4000"                autobind= "        "       selectortimeout= "        "       maxthreads= "6"/>     <sender classname = "Org.apache.catalina.tribes.transport.ReplicationTransmitter" >      < Transport classname= "Org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>     </Sender>    <interceptor classname= " 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= ". *\.gif;. *\.js;. *\.jpg;. *\.png;. *\.htm;. *\.html;. *\.css;. *\.txt; " />  <valve classname= "Org.apache.catalina.ha.session.JvmRouteBinderValve"/>    <clusterlistener classname= "Org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener "/>  <clusterlistener classname=" Org.apache.catalina.ha.session.ClusterSessionListener "/ ></Cluster>

Add the following to the host node

<deployer classname= "Org.apache.catalina.ha.deploy.FarmWarDeployer" tempdir= "/tmp/war-temp/" Deploydi R= "/tmp/war-deploy/" watchdir= "/tmp/war-listen/" watchenabled= "false"/>

Note that on the B server, which is 192.168.150.8 plus above

<engine name= "Catalina" defaulthost= "localhost" jvmroute= "TOMCAT1" >

This line TOMCAT1 modified to TOMCAT2

<receiver classname= "Org.apache.catalina.tribes.transport.nio.NioReceiver" address= "192.168.150.7"//Modify here              For the IP address of the B machine 192.168.150.8 port= "4000"//here the first server without modification 150.8 modified for 4001 autobind= "100" Selectortimeout= "maxthreads=" 6 "/>

At this point the cluster and session congratulations basic configuration complete below we do the test

Create a new file on both servers if you first

cd /usr/local/tomcat/webappsmkdir testcd testtouch  index.jsp //Create a new index.jsp file   Add the following 
 <b> Current sessionid:</b>   <br />   <%            string sessionid = session.getid ();            out.println (SessionID);            system.err.println ("sessionid = "  + sessionID);              //  If there is a new  Session  Property Settings            String dataName =  Request.getparameter ("Dataname");           if  ( Dataname != null && !dataname.isempty ())  {                    string datavalue  = request.getparameter ("DAtavalue ");                    session.setattribute (Dataname, datavalue);            }   %>   <br />   <br  />   <b>session Property List:</b>     <br />    <%           enumeration<string > e =  (enumeration<string>)  session                             .getattributenames ();           while  (E.hasmoreelements ())  {                   &nBsp String name = e.nextelement ();                    String value =  (String)   Session.getattribute (name);                    out.println (name +  " = "  + value +   "<br>");                    system.err.println (name +  " = "  + value);            }   %>   <form  Method= "POST" >   <ul style= "list-style-type: none;" >           <li><label for= "DataName" > Key: </label><input size= " id=" Dataname "&NBsp;                 name = "Dataname" ></li>           <li>< Label for= "DataValue" > Value: </label><input size= "      "             id= "DataValue"  name= "DataValue" ></li>           <li><input type = "Submit"  value= "Submission"  /></li>   </ul>   </form>
mkdir Web-inf//New Web-infi folder CD Web-inf Touch Web. XML//new Web. xml file and add the following
<web-app xmlns= "Http://java.sun.com/xml/ns/j2ee" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation= "Http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" > "version=" 2.4 "> <distributable/> <display-name>TomcatDemo</display-name></web-app>

Note <distributable/> This sentence must be added otherwise the session cannot be shared

and notice.

<membership classname= "Org.apache.catalina.tribes.membership.McastService" address= "224.0.0.0" Port= "45564" frequency= "Droptime="/>

The address= "224.0.0.0" in this configuration in the Server.xml file above

For Windows systems, broadcast addresses (which are used here in 224.0.0.0 and 240.0.0.0) are open by default,
For Linux, you need to open the address by command.

Route add-net 224.0.0.0 netmask 240.0.0.0 Dev eth0

Then view the ROUTE-V command

[[Email protected] web-inf]# route -vkernel ip routing tabledestination      Gateway         Genmask          Flags Metric Ref    Use  iface192.168.150.0   *                255.255.255.0   U     0       0        0 eth0link-local       *                255.255.0.0     u     1002   0         0 eth0224.0.0.0       *                240.0.0.0       U      0      0        0 eth0    //already has a default         192.168.150.1  .  0.0.0.0         UG    0       0        0 eth0

This sentence should be added to the/etc/rc.d/rc.local file to start the boot

After two server settings are complete, we go back to 192.168.150.7 a server.
Copy the/usr/local/tomcat/webapps/test directory to the root directory of Apache.

Cp-r/usr/local/tomcat/webapps/test/var/www/html//Do not copy also line

After the replication is complete, we start the server
A server

Service httpd restartcd/usr/local/tomcat/bin./startup.sh

b Server

cd/usr/local/tomcat/bin./startup.sh

Look at the listening port when the boot is complete
A server should listen to at least the following ports

80,8080,8009,4000
b server should have a port

8080 8009 4001

After we visit
http://192.168.150.7/test/index.jsp
Multiple refreshes to see if the cluster switches before TOMCAT1 and TOMCAT2
Session should be constant on both servers

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6D/BC/wKiom1VqmK7TlWtMAAD2HPlYhgI974.jpg "style=" float: none; "title=" 1.JPG "alt=" Wkiom1vqmk7tlwtmaad2hplyhgi974.jpg "/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6D/B8/wKioL1VqmkahoVQiAADnZP32fPA237.jpg "style=" float: none; "title=" 2.JPG "alt=" Wkiol1vqmkahovqiaadnzp32fpa237.jpg "/>



If the session changes, check the configuration file and view the Tomcat log

You can now close one of the Tomcat tests if the session is lost or requested


The above is through the MOD_JK way cluster also has a proxy agent module
Modified Apache httpd.conf file will be canceled by * * *

LoadModule proxy_module modules/mod_proxy.soloadmodule proxy_balancer_module modules/mod_proxy_ Balancer.soloadmodule proxy_ftp_module modules/mod_proxy_ftp.soloadmodule proxy_http_module modules/mod_proxy_ Http.soloadmodule Proxy_ajp_module modules/mod_proxy_ajp.so

and add content at the end

Proxyrequests off Proxypass/test balancer://tomcat/proxypassreverse/test balancer://tomcat/<Proxy Balancer ://tomcat> balancermember ajp://localhost:8009/test loadfactor=1 route=tomcat1 BalancerMember AJP ://192.168.150.8:8009/test loadfactor=1 route=tomcat2 </Proxy>

and will before
Include conf/mod_jk.conf
This sentence is commented out


Then restart the Apache test cluster effect is the same session is also correct

You can also view the cluster status by monitoring

The following content

<Location/balancer-manager> SetHandler Balancer-manager Order deny,allow Deny from all Allow from localhost 192.168.150.1</location>

192.168.150.1 change to the IP address of your current browsing machine
Join the httpd.conf
Then restart Apache.

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6D/BC/wKiom1VqmOOx_lCBAAJBizniL74896.jpg "title=" 3.JPG " alt= "Wkiom1vqmoox_lcbaajbiznil74896.jpg"/>


Apache TOMCAT integrated Cluster session sharing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.