In many scenarios, session information of a user accessing a website needs to be saved. There are three ways to save session information:
1. Session binding 2. session replication: Copies session information between nodes in multiple clusters to each node. 3. Session server. A separate server is used to save session information. In the experiment, the third method is to save the user's session information by using the session server.
Lab machine:
The two front-end Tomcat clusters use httpd as the reverse proxy. Please forward your requests to the backend Tomcat cluster. At the same time, two memcached servers are used to save your session information, one node serves as the backup session server and the other node serves as the master node. The specific host information used is as follows:
Node1: 172.16.103.1. Install httpd and tomcat.
Node2: 172.16.103.2. install Tomcat and memcached. The memcached installed on the host serves as the slave node.
Node3: 172.16.103.3. Install memcached as the master node of memcached.
Experiment process: Install JDK version jdk-7u9-linux-x64, Tomcat for Apache-Tomcat-7.0.42
1. Configure node1
1. Install httpd
# yum -y install httpd
2. Tomcat depends on JDK. JDK must be installed in advance.
# rpm -ivh jdk-7u9-linux-x64.rpm
Configure JDK
# vim /etc/profile.d/java.shAVA_HOME=/usr/java/latestPATH=$JAVA_HOME/bin:$PATHexport JAVA_HOME PATH# source /etc/profile.d/java.sh
3. install Tomcat
# tar xf apache-tomcat-7.0.42.tar.gz -C /usr/local/# cd /usr/local# ln -sv apache-tomcat-7.0.42/ tomcat
Configure Tomcat
# vim /etc/profile.d/tomcat.sh export CATALINA_HOME=/usr/local/tomcatexport PATH=$CATALINA_HOME/bin:$PATH
4. Provide the sysv script for Tomcat:
# vim /etc/rc.d/init.d/tomcat#!/bin/sh# Tomcat init script for Linux.## chkconfig: 2345 96 14# description: The Apache Tomcat servlet/JSP container.# JAVA_OPTS=‘-Xms64m -Xmx128m‘JAVA_HOME=/usr/java/latestCATALINA_HOME=/usr/local/tomcatexport JAVA_HOME CATALINA_HOMEcase $1 instart) exec $CATALINA_HOME/bin/catalina.sh start ;;stop) exec $CATALINA_HOME/bin/catalina.sh stop;;restart) $CATALINA_HOME/bin/catalina.sh stop sleep 2 exec $CATALINA_HOME/bin/catalina.sh start ;;configtest) exec $CATALINA_HOME/bin/catalina.sh configtest ;;*) exec $CATALINA_HOME/bin/catalina.sh * ;;esac
Start Tomcat and view the port listening result of the Tomcat running instance.
# service tomcat start
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/4B/23/wKiom1QoOJuzXKSxAAP6ehYc0gg833.jpg "Title =" image 2.png "alt =" wkiom1qoojuzxksxaap6ehyc0gg833.jpg "/> where 8080 is the port of the HTTP connector listener for the default tomcat, 8005 is the port for managing Tomcat instance process listening, and 8009 is the port for Tomcat AJP protocol connector listening. In a separately deployed Tomcat server, the connector can also be understood as a Web server for receiving and responding to user requests.
After the Tomcat instance is started, you can use a browser to test whether the Tomcat instance runs normally. For example, enter the address and port listened to by the instance in the browser. The result is as follows:
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/4B/23/wKiom1QoOZfAPkjRAAZK8RRA0Zc501.jpg "Title =" image 1.png" alt = "wkiom1qoozfapkjraazk8rra0zc501.jpg"/> 2. Configure node2
The node to be installed on node2 is similar to node1, including JDK and tomcat. The difference is that memcached must be installed on this node to save the user's session information. memcached of this node is used as the slave node, for how to install and configure JDK and tomcat, see configure node1.
1. Install memcached
# yum -y install memcached# service memcached start
After installing Tomcat on node2, you must test whether the default Tomcat page can be accessed properly for further operations.
3. Configure node3
Only memcached is installed on node3 and serves as the master node for saving user sessions.
# yum -y install memcached # service memcached start
4. Configure the HTTPd service on node1. it works in reverse proxy mode and is used to receive user requests and forward the requests to the backend Tomcat server.
Install httpd to ensure that the following modules are installed. You can run httpd-M or httpd-D dump_modules to check whether the following modules have been installed:
proxy_module (shared) proxy_balancer_module (shared) proxy_http_module (shared) proxy_ajp_module (shared)
5. Create a web app on node1 and node2 for test access. Create a new Web application under the corresponding webapps directory under the tomcat installation path:
# Cd/usr/local/tomcat/webapps/
# Mkdir test/WEB-INF/{classes, lib}
The test page of the Web application is provided. The content provided on the test page is used to test the user's session information. It is a Java Script:
# cd test/# vim index.jsp<%@ page language="java" %>
The process for creating a web app on node2 is the same. The test page content is as follows:
<%@ page language="java" %>
The content of the two pages is different. During the test, you can view the changes of different host names, but the user's session information remains unchanged, so that the user session can be saved. After the two pages are provided, You can restart the Tomcat instance and use the Host Name: port to check whether the two pages can be accessed. The effect should be as follows:
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/4B/24/wKiom1QoPlWw5ZZ0AAG81ZYPXiE598.jpg "Title =" image 5.png" alt = "wkiom1qoplww5zz0aag81zypxie598.jpg"/> 650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/4B/24/wKiom1QoPqGAILruAAG-ZdRNf3E425.jpg "Title =" image 6.png" alt = "wKiom1QoPqGAILruAAG-ZdRNf3E425.jpg"/> after the tomcat configuration of the two nodes is complete, configure the httpd reverse proxy, implement two backend Tomcat instances to form a cluster. The two nodes are defined as lbcluster using the balancer. The weight of each node is 1, and the status output information of the cluster is defined.
<Proxy balancer://lbcluster> BalancerMember ajp://172.16.103.1:8009 loadfactor=1 route=TomcatA BalancerMember ajp://172.16.103.2:8009 loadfactor=1 route=TomcatB ProxySet lbmethod=byrequests</Proxy><Location /status> SetHandler balancer-manager ProxyPass ! Order allow,deny Allow from all</Location>ProxyPass / balancer://lbcluster/ProxyPassReverse / balancer://lbcluster/
Start the HTTPd service and enter the IP address used by the httpd host in the browser to test whether the Tomcat backend host can be accessed.
# service httpd start
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/4B/26/wKioL1QoQNWhBjD6AAYtED2Wnv0089.jpg "Title =" image 7.png" alt = "wkiol1qoqnwhbjd6aayted2wnv0089.jpg"/> in this case, you do not need to enter the address and port in the browser, you only need to enter the IP address of the host running the HTTPd service. To test the Server Load balancer effect, the backend host has a response, which indicates that the configuration is correct.
6. Provide the class library files provided by the memcached Session Manager software for Tomcat on node1 and node2. The class library files are listed as follows. These files must be placed in the lib directory under the tomcat installation path:
Javolution-5.5.1.jar, memcached-session-manager-1.8.2.jar, memcached-session-manager-tc7-1.8.2.jar, msm-javolution-serializer-1.8.2.jar, spymemcached-2.10.2.jar
Configure the tomcat configuration file on node1 and node2, and define the memcached Session Manager Information to modify the tomcat configuration file
Add the following information to the default web application, which defines the host nodes used by memcached. The host with the N1 name is the slave node:
<Context path="/test" docBase="test"> <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager" memcachedNodes="n1:172.16.103.1:11211,n2:172.16.103.2:11211" failoverNodes="n1" requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$" transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"/> </Context>
Provide web for the Web application. XML file, which can be copied from the tomcat configuration file to the directory of the Web application. The purpose is to only save the session information of the cluster, without setting global tomcat.
# Cp/usr/local/tomcat/CONF/Web. xml/usr/local/tomcat/webapps/test/WEB-INF/
# Vim/usr/local/tomcat/webapps/test/WEB-INF/# modify the file to add the following information
<Distributable/>
These configuration operations must be configured on both nodes, and the configurations are identical.
After the configuration is complete, restart Tomcat on the two nodes and perform further tests.
# Servcie Tomcat restart
Enter the IP address used by the cluster in the browser, that is, the IP address used by the reverse proxy for HTTP testing:
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/4B/24/wKiom1QoRVGj_L3QAAG6jzrLzak189.jpg "Title =" image 8.png" alt = "wkiom1qorvgj_l3qaag6jzrlzak189.jpg"/> after refreshing, the host name changes, that is, the corresponding host will change, but the session ID will not change:
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/4B/25/wKiom1QoRaagVpZKAAG5hmIBEN8489.jpg "Title =" image 9.png" alt = "wkiom1qoraagvpzkaag5hmiben8489.jpg"/> you can see that the host name in the two graphs has changed, and the session information has not changed. The session server successfully saves user sessions.
Use memcached Session Manager to implement session servers