Tomcat Cluster Configuration Learning-----Distributed Application
Now based on the Javaweb development of the application system has been everywhere, especially e-commerce sites, to develop and grow the site, it must be able to withstand the huge number of web site visits; You know if the server is too large to visit, there will be the situation of service application crashes, this time how to do, Can you just reboot the server? Well, if it's a small public service website, it doesn't matter. But if it is such as a large airline ticketing and other e-commerce sites, every hour a large number of orders business, if these ticketing system once the crash, then restart, these time and customer losses will directly affect the interests of the airline, How can these losses be avoided?
Don't worry, the current IBM WebSphere or the popular Tomcat server provides us with a common solution, that is, multiple servers to share the traffic, so the pressure on one server will be reduced a lot, You can configure as many servers as you need to support your application, and if a service crashes, the other application servers can continue to support application continuation services. A simple flowchart for a multiple application server is roughly as follows:
To implement this principle, we need to solve two problems:
1: How to implement the session sharing between multiple application servers: (one server crashes, another server can continue to support)
2: How to distribute requests to each application server to implement the pressure decomposition: (the solution here is to use Apache as a Web server)
Here's what we actually do to see how we can achieve this.
Environment configuration:
App Application Server apache-tomcat-7.0.52-1
Web server: Apache Apache 2.0.55
Java Environment: jdk1.6
System Environment: WinXP system
1) Web server configuration
First install the Apache Web server:
There are actually three ways to connect Apache servers to tomcat: MOD_JK, Http_proxy, and Ajp_proxy.
One: Software Environment
1. Apache:apache 2.0.55 (Download by http://httpd.apache.org/) (Click to download Apache 2.0.55)
2. tomcat:apache-tomcat-7.0.52-1
3. MOD_JK: Find the Tomcat connectors link under the page http://tomcat.apache.org/Download header (click to download mod_jk-apache-2.0.55.so), it looks like a Unix /linux Dynamic Library, the actual should be a Win32 DLL dynamic library, presumably to maintain the consistency of different platform configuration, only use this extension.
Two: Load Balancing
Use Apache to shunt, the request according to weight and then load tomcat1,tomcat2 ... To deal with
1. Install Apache,tomcat
I've installed Apache in D:/apache group/apache2.
Extract two points tomcat, respectively, in D:/apache Group/apache-tomcat-7.0.52-1,d:/apache group/apache-tomcat-7.0.52-1
2. Modify Apache configuration file http.conf
In the Apache installation directory in the Conf directory to find http.conf, at the end of the file with the following sentence can be added
Include conf/mod_jk.conf
3. http.conf the new mod_jk.conf file under the same directory, the contents are as follows
#加载mod_jk Module
LoadModule jk_module modules/mod_jk-apache-2.0.55.so
#指定 workers.properties file path
Jkworkersfile conf/workers.properties
#指定那些请求交给tomcat处理, "controller" for the load allocation controller
specified in Workers.propertise Jkmount/*.jsp Controller
If you want to specify the *.do also shunt, add another line
Jkmount/*.do Controller
If you want to divert all requests, just write
Jkmount/* Controller
4. Create a new workers.properties file in the same directory as http.conf
Worker.list = controller,tomcat1,tomcat2 #server list
#========tomcat1========
worker.tomcat1.port=8019 #ajp13 port number, under Tomcat server.xml configuration, default 8009
worker.tomcat1.host=localhost #tomcat的主机地址, if not the native, please fill in the IP address
worker.tomcat1.type=ajp13
worker.tomcat1.lbfactor = 1 #server的加权比重, the higher the value, the more requests to be divided
#========tomcat2 ========
worker.tomcat2.port=8029 #ajp13 port number, server.xml configuration under Tomcat, default 8009
worker.tomcat2.host= localhost #tomcat的主机地址, if not for this machine, please fill in the IP address
worker.tomcat2.type=ajp13
worker.tomcat2.lbfactor = 2 # The weighted weight of the server, the higher the value, the more requests
#========controller, the load-balancing controller ========
worker.controller.type=lb
WORKER.CONTROLLER.BALANCED_WORKERS=TOMCAT1,TOMCAT2 #指定分担请求的tomcat
worker.controller.sticky_session=1
5. Modify the Tomcat configuration file Server.xml
If you are a horizontal cluster, that is, the number of installed Tomcat,tomcat on different computers is one, you do not have to modify the Tomcat configuration file. I am here to install two Tomcat on the same computer, which is a vertical cluster, so you have to modify one of the settings, To avoid port conflicts, the reference article is to change the port number beginning with 9 to the 9 start port number, but on my machine if the port number that starts with 9, such as 9080, 9082, will conflict with my WebSphere Application server configuration, So the strategy I'm taking here is to change the third digit of the original port number to 1, as 8080 to 8180.
Open Tomcat2/conf/server.xml File
1 Change the listening port of Tomcat off from 8005 to 8105
namely the
<server port= "8005" shutdown= "Shutdown" >
To
<server port= "8105" shutdown= "Shutdown" >
2) Change the HTTP service port number from 8080 to 8180
Found it
<!--Define a non-ssl http/1.1 Connector on port 8080-->
<connector port= "8080"
Change the 8080 here to 8180.
3) Change the AJP port number from 8009 to 8109
Found it
<!--Define a AJP 1.3 Connector on port 8009-->
<connector port= "8009"
Change the 8009 here to 8109.
4 The HTTP proxy port from 8082 to 8182 (this configuration by default is commented out, you can skip this step)
Found it
<connector port= "8082"
Change the 8082 here to 8182.
5 Write a Test JSP
Create a directory Testcluster, inside a new test.jsp, content for <% System.out.println ("==========================="); %>
Put the testcluster under the WebApps of the TOMCAT1,TOMCAT2.
6) Start APACHE,TOMCAT1,TOMCAT2 and test
Through http://localhost/TestCluster/test.jsp access, refresh several times the page, view the TOMCAT1 and Tomcat2 window, you will be able to see the printed line "========================== = "and statistically speaking, the number of TOMCAT2 printed in the TOMCAT1 is twice times, you can see the request will be tomcat1,tomcat2 according to different weights of the shunt processing, achieve load balance.
For the following cluster configuration, please change the weights of TOMCAT1 and TOMCAT2 to the same in workers.properties, so that the request is distributed more evenly, and it will be easy to see the effect of the experiment.
First configure the Web application server configuration tomcat-a configuration
1: Modify Tomcat's Server.xml file to add the following
Post code to facilitate replication:
<cluster classname= "Org.apache.catalina.ha.tcp.SimpleTcpCluster" channelsendoptions= "6" > <manager classname= "Org.apache.catalina.ha.session.BackupManager" Expiresessionso Nshutdown= "false" notifylistenersonreplication= "true" mapsendoptions= "6"/>
; <manager classname= "Org.apache.catalina.ha.session.DeltaManager" expiresessionsonshutdown= false "notifylistenersonreplication=" true "/> <channel classname=" Org.apache.catali Na.tribes.group.GroupChannel "> <membership classname=" org.apache.catalina.tribes.membership.McastSer
Vice "bind=" 127.0.0.1 "address=" 228.0.0.4 "
port= "45564" "frequency=" droptime= "3000"/> &lT
Receiver classname= "Org.apache.catalina.tribes.transport.nio.NioReceiver" address= "127.0.0.1" Port= "4001" 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.TcpFailureDetect or "/> <interceptor classname=" Org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interce Ptor "/> <interceptor classname=" Org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor " /> </Channel> <valve classname= "Org.apache.catalina.ha.tcp.ReplicationValve
" Filter= ". *\.gif; *\.js;. *\.jpg;. *\.png;. *\.htm;. *\.html;. *\.css;. *\.txt; "
/> <clusterlistener classname= "Org.apache.catalina.ha.session.ClusterSessionListener"/> </Cluster>
Note:
1 App Server Tomcat-b configuration and tomcat-a is basically the same, but note that two servers if you need to pay attention to the port on a computer conflict problem, such as: Tomcat-a port is 8080, then tomcat-b is 8081 or other, Others and so on
My port here is configured as follows:
tomcat-1:serverport:8081 ajp/1.3connector:8019 <server port= "8015" shutdown= "Shutdown" >
tomcat-2:serverport:8082 ajp/1.3connector:8029 <server port= "8025" shutdown= "Shutdown" >
2 and then just a new Web application test, in the new application of the web.xml need to add
<distributable/>
3 Here you can test your configuration, first start tomcat-1, and then start tomcat-2, if you see similar information in two consoles
Prove that the two application servers have been configured to complete.
4 new Web application, web.xml configuration file needs to add the following content to cooperate with the implementation session sharing:
<distrbutable/>
The application is then deployed to two servers and two application servers are started for testing:
Note: Since the JSP comes with the session object, it is best to close the page with the session object after the test and close the Code <%@ page session= "false"%>
Tomcat-a index.jsp Code:
<body>
This is my JSP page.tomcat-a <br>
<%
HttpSession mysession = Request.getsession (false);
if (mysession==null) {
MySession = Request.getsession (true);
Mysession.setattribute ("appname", "value-a");
OUT.PRINTLN ("New Session:" +mysession.getid ());
}else{
Out.println ("Old session:" +mysession.getid ());
}
Out.println ("Appname=" +mysession.getattribute ("appname"));
%>
</body>
Tomcat-b index.jsp Code:
<body>
This is my JSP page.tomcat-b<br>
<%
HttpSession mysession = Request.getsession (false);
if (mysession==null) {
MySession = Request.getsession (true);
Mysession.setattribute ("appname", "value-a");
OUT.PRINTLN ("New Session:" +mysession.getid ());
}else{
Out.println ("Old session:" +mysession.getid ());
}
Out.println ("Appname=" +mysession.getattribute ("appname"));
%>
</body>
5 respectively access to two servers: Http://172.23.1.46:8081/mycluster
and http://172.23.1.46:8082/mycluster separate access to get results as follows to prove that the configuration is OK.
Access to 8081 servers:
The results of the page refresh are as follows:
Access to 8082 servers:
The results of the page refresh are as follows:
Where the Tomcat server is configured, the next step is to configure a unified portal load-balanced Web server, which uses Apache 2.2.19 (which can be downloaded directly from the official web site).