Tomcat session cluster and session Server

Source: Internet
Author: User
Tags dedicated server

Tomcat session cluster and session Server

Overview

Role of session

Httpd is a stateless protocol. Multiple http requests are not associated, and the server cannot identify which requests come from the same client. To solve this problem, when the client accesses the session for the first time, the server creates a session to save the session information, and then sets the session ID (the unique identifier of the session) placed in the header of the Response Message and sent to the client, the client will carry the sessionID (usually in the cookie of the request) to indicate which session this belongs.

Maintain session

After Tomcat implements load balancing, client requests are scheduled to different servers, and session information is often stored in the memory of one of the servers, if subsequent requests are scheduled to other servers, the client cannot access the previous session information. There are three ways to solve this problem:

1) session binding: During scheduling, requests from the same client are always scheduled to the same server;

2) session cluster, which synchronizes session information on multiple servers in real time;

3) The session server stores the session information on all servers to the session server.

The first method can be used to configure the corresponding parameter implementation on the proxy server at the front end, but this method will affect the effect of Server Load balancer, and one of the servers is faulty, the session information on it will also be lost. The second method is implemented by synchronizing sessions, that is, the session information of each session exists on all servers, which is not applicable to large cluster environments. The third method is to store session information on a dedicated server. For example, on a memcache server, the server first synchronizes session information from memcache to the local server when processing requests, and then proceed. This article introduces the second and third implementation methods.

Session cluster implementation

Experiment environment: Two tomcat nodes, using httpd for load balancing.

192.168.1.116 # http Server

192.168.1.106 # tomcat node1

192.168.1.127 # tomcat node2

1) configure the http server to achieve Load Balancing (on 192.168.1.116 ).

[Root @ www ~] # Vim/etc/httpd/extra/httpd-vhosts.conf

####### Global configuration ################

<Proxy balancer: // lbcluster>

BalancerMember http: // 192.168.1.106: 8080 loadfactor = 1 route =

BalancerMember http: // 192.168.1.127: 8080 loadfactor = 1 route = B

# ProxySet stickysession = JSESSIONID # No session binding

ProxySet lbmethod = bytraffic

</Proxy>

######## Virtual host configuration ##############

<VirtualHost *: 80>

ServerName www.bkjia.com

ProxyVia Off

ProxyRequests Off

ProxyPreserveHost On

ProxyPass/balancer: // lbcluster/

ProxyPassReverse/balancer: // lbcluster/

<Location/status>

SetHandler balancer-manager

Proxypass!

Require all granted

</Location>

<Proxy *>

Require all granted

</Proxy>

<Location/>

Require all granted

</Location>

</VirtualHost>

Contains the corresponding file:

[Root @ www ~] # Vim/etc/httpd. conf

........

Include/etc/httpd/extra/httpd-vhosts.conf

2) Configure the server on each tomcat node. xml file. Add the following section to the host to implement the session cluster. If the following content is defined in the Engine container, the cluster function is enabled for all hosts.

[Root @ node1 ~] # Vim/usr/local/tomcat/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.3.4"

Port = "45564"

ExpireSessionsOnShutdown = "false"

NotifyListenersOnReplication = "true"/>

<Channel className = "org. apache. catalina. tribes. group. GroupChannel">

<Membership className = "org. apache. catalina. tribes. membership. McastService"

Address = "228.0.3.4" # The multicast address of the listener (it is best not to use the default value)

Port = "45564" # listening port

Frequency = "500" # interval of each Heartbeat message (in milliseconds)

DropTime = "3000"/> # timeout duration (if the heartbeat information is not received after this time, the member is removed from Membership)

<Cycler className = "org. apache. catalina. tribes. transport. nio. NioReceiver"

Address = "192.168.1.106" # interface through which data is received. If multiple IP addresses exist, you must specify

Port = "4000"

AutoBind = "100"

SelectorTimeout = "5000"

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 = ""/>

<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"/>

<ClusterListener className = "org. apache. catalina. ha. session. JvmRouteSessionIDBinderListener"/>

<ClusterListener className = "org. apache. catalina. ha. session. ClusterSessionListener"/>

</Cluster>

.....

If the address in the Receiver er segment must be specified in the virtual machine configuration, it is the address of an interface on the machine. Set this parameter to the corresponding IP address (192.168.1.127) When configuring Node 2 ).

3) add <distributable/>

[Root @ node1 web] # vim WEB-INF/web. xml

.....

<Web-app .....

.....

<Distributable/>

</Web-app>

4) Add a test page on two tomcat nodes

Node1 (192.168.1.106 ):

[Root @ node1 ~] # Vim/tomcat/webapps/web/index. jsp

<% @ Page language = "java" %>

<Html>

<Head> <title> Tomcat node1 </title>

<Body>

<H1> <font color = "blue"> Tomcat node1 </font>

<% Session. setAttribute ("bkjia.com", "xiaoxiao.com"); %>

<H4> Session_ID: <% = session. getId () %>

<H4> Creat_time: <% = session. getCreationTime () %>

</Body>

</Html>

On node2 (192.168.1.127 ):

[Root @ node2 ~] # Vim/tomcat/webapps/web/index. jsp

<% @ Page language = "java" %>

<Html>

<Head> <title> Tomcat node2 </title>

<Body>

<H1> <font color = "red"> Tomcat node2 </font>

<% Session. setAttribute ("bkjia.com", "xiaoxiao.com"); %>

<H4> Session_ID: <% = session. getId () %>

<H4> Creat_time: <% = session. getCreationTime () %>

</Body>

</Html>

Restart the tomcat service and perform the test:

When the session information is not sent to different nodes, the test is completed.

Session Server Based on memcached + MSM

Lab Topology

Lab environment:

192.168.1.116 # http Server

192.168.1.106 # tomcat node1

192.168.1.127 # tomcat node2

192.168.1.121, 192.168.1.122 # memcached Server

Working principle:

During the first access, the server creates session information and stores the information in the process and synchronizes it to memcache in real time. If the next scheduling is performed on another node, the node first checks its session region, if the user does not have a session, go to memcache to find the session information and read the session information from memcache to its web container, if the session is updated, it will be synchronized to memcache immediately. After a request from the same client is scheduled to a previously accessed node, the node will first check whether the local session information is synchronized with the memcache in memcache, if you do not synchronize the read data from memcache to overwrite your original data.

Introduction to memcached-session-manager

Memcached-session-manager (msm) is a tool provided by Google to save tomcat session information to memcache and read the corresponding information into web container as needed. It can work in two modes: 1) Session stickiness; 2) Session stickiness; that is, whether the front-end proxy server has enabled stickysession (the same client requests proxy to the same server ). The above principle works in a non-sticky session mode. If a user accesses a session in a sticky scenario, you do not need to check whether the session information is up-to-date in memcache every time. After the session is updated, you do not need to synchronize the session information to memcache in a timely manner, which can save some steps and bandwidth.

For memcached-session-manager, http://code.google.com/p/memcached-session-manager/. this project is based on Java Development and uses the following jarfile (version ):

Memcached-session-manager-version.jar

Memcached-session-manager-tc (6 | 7 | 8)-version. jar

Spymemcached-version.jar

Msm-javolution-serializer-version.jar

Javolution-version.jar

Among them, msm-javolution-serializer is a streaming tool that can convert session information into streaming data and store it on memcache, and restore it when reading data. There are many similar tools, in the implementation process, you can select different streaming tools as needed, and the related configurations are also different. Please refer to the official documentation.

Implementation Process

1) install memcached on 192.168.1.121 and 192.168.1.122.

[Root @ node1 ~] # Tar xf libevent-2.0.21-stable.tar.gz

[Root @ node1 ~] # Cd libevent-2.0.21

[Root @ node1 ~] #./Configure -- prefix =/usr/local/libevent

[Root @ node1 ~] # Make & make install

[Root @ node1 ~] # Echo "/usr/local/libevent/lib">/etc/ld. so. conf. d/libevent. conf

[Root @ node1 ~] # Ldconfig

######################################## #############

[Root @ node1 ~] # Tar xf memcached-1.4.15.tar.gz

[Root @ node1 ~] # Cd memcached-1.4.15

[Root @ node1 ~] #./Configure -- prefix =/usr/local/memcached -- with-libevent =/usr/local/libevent

[Root @ node1 ~] # Make & make install

Provide service scripts for memcached and configure them as system services (the scripts can be copied)

[Root @ node1 ~] # Chmod + x/etc/init. d/memcached

[Root @ node1 ~] # Chkconfig -- add memcached

[Root @ node1 ~] # Service memcached start

2) Add a VM to tomcat node1 and node2 nodes (in session-free mode ):

[Root @ node1 ~] # Vim/usr/local/tomcat/conf/server. xml

........

<Host name = "www.bkjia.com" appBase = "/tomcat/webapps"

UnpackWARs = "true" autoDeploy = "true">

<Context path = "/web" docBase = "web" reloadable = "true">

<Manager className = "de. javakaffee. web. msm. MemcachedBackupSessionManager"

MemcachedNodes = "n1: 192.168.1.121: 11211, n2: 192.168.1.122: 11211"

Sticky = "false"

SessionBackupAsync = "false"

LockingMode = "uriPattern:/path1 |/path2"

RequestUriIgnorePattern = ". * \. (ico | png | gif | jpg | css | js) $"

TranscoderFactoryClass = "de. javakaffee. web. msm. serializer. javolution. JavolutionTranscoderFactory"

/>

</Context>

</Host>

In a sticky session mode:

[Root @ node1 ~] # Vim/usr/local/tomcat/conf/server. xml

........

<Host name = "www.bkjia.com" appBase = "/tomcat/webapps"

UnpackWARs = "true" autoDeploy = "true">

<Context path = "/web" docBase = "web" reloadable = "true">

<Manager className = "de. javakaffee. web. msm. MemcachedBackupSessionManager"

MemcachedNodes = "n1: 192.168.1.121: 11211, n2: 192.168.1.122: 11211"

FailoverNodes = "n1"

RequestUriIgnorePattern = ". * \. (ico | png | gif | jpg | css | js) $"

TranscoderFactoryClass = "de. javakaffee. web. msm. serializer. javolution. JavolutionTranscoderFactory"

/>

</Context>

</Host>

MemcachedNodes specifies the address and port of the backend memcache server. Two memcache servers are used here to achieve high availability. When saving session information, you must write it to two memcache servers at the same time.

3) copy the required jar file (Class Library) to the lib directory of tomcat:

[Root @ node1 msm] # ls

Javolution-5.5.1.jar memcached-session-manager-tc7-1.8.2.jar spymemcached-2.10.2.jar

Memcached-session-manager-1.8.2.jar msm-javolution-serializer-1.8.2.jar

[Root @ node1 msm] # cp./*/usr/local/tomcat/lib/

Copy to another node:

[Root @ node1 msm] # scp./* 192.168.1.127:/usr/local/tomcat/lib/

Restart the tomcat service on the two nodes:

[Root @ node1 ~] # Service tomcat restart

Use the Server Load balancer configured in the previous case and the added test page for testing. The test results are consistent with those described above .................. Pai_^

For more Tomcat tutorials, see the following:

Install and configure the Tomcat environment in CentOS 6.6

Install JDK + Tomcat in RedHat Linux 5.5 and deploy Java Projects

Tomcat authoritative guide (second edition) (Chinese/English hd pdf + bookmarks)

Tomcat Security Configuration and Performance Optimization

How to Use Xshell to view Tomcat real-time logs with Chinese garbled characters in Linux

Install JDK and Tomcat in CentOS 64-bit and set the Tomcat Startup Procedure

Install Tomcat in CentOS 6.5

Tomcat details: click here
Tomcat: click here

This article permanently updates the link address:

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.