Use Memcached for Tomcat cluster Session sharing

Source: Internet
Author: User
Tags nginx server

Use Memcached for Tomcat cluster Session sharing

Environment CentOS6.7. download and install the necessary software: yum-y install epel-release (tomcat7 is currently the mainstream version of tomcat7)

Yum-y install tomcat download tomcat 7 directly, yum-y install java-1.7.0-openjdk-devel (tomcat needs to run on Java Virtual Machine)

Vim/etc/tomcat/server. xml modify the configuration file


<Host name = "web.com" appBase = "/data/webapps /"
UnpackWARs = "true" autoDeploy = "true">
<Context path = "" docBase = "/data/webapps" reloadable = "true">
<Manager className = "de. javakaffee. web. msm. MemcachedBackupSessionManager"
MemcachedNodes = "n1: 192.168.238.250: 11211, n2: 192.168.238.133: 11211"
FailoverNodes = "n2"
RequestUriIgnorePattern = ". * \. (ico | png | gif | jpg | css | js) $"
TranscoderFactoryClass = "de. javakaffee. web. msm. serializer. javolution. JavolutionTranscoderFactory"/>
</Context>
</Host>

I can't see all the words above. I suggest you paste a picture.

Custom Virtual Host: <Host name = "web.com" appBase = "/data/webapps/" unpackWARs = "true" autoDeploy = "true">

Context: <Context path = "" docBase = "/data/webapps" reloadable = "true">

The key is the manager section:

Obtain a globally unique name for the manager: <Manager className = "de. javakaffee. web. msm. MemcachedBackupSessionManager"

Defines the memcached server nodes: memcachedNodes = "n1: 192.168.238.250: 11211, n2: 192.168.238.133: 11211"

Define slave node: failoverNodes = "n2"

Ignore those request URLs: requestUriIgnorePattern = ". * \. (ico | png | gif | jpg | css | js) $"

Fixed format: transcoderFactoryClass = "de. javakaffee. web. msm. serializer. javolution. JavolutionTranscoderFactory"/>

Define the default virtual host as web.com:

JvmRoute = "TomcatA" is to add JvmRoute = "TomcatA" to the session id of the web.com request.

Create page resources: mkdir-p/data/webapps/{WEB-INF, classes, lib} cd/data/webapps/vim index. jsp

The jsp page resources are as follows:

<% @ Page language = "java" %>
<Html>
<Head> <title> TomcatA </title> <Body>
<H1> <font color = "red"> TomcatA. test </font> <Table align = "center" border = "1">
<Tr>
<Td> Session ID </td>
<% Session. setAttribute ("test", "test"); %>
<Td> <% = session. getId () %> </td>
</Tr>
<Tr>
<Td> Created on </td>
<Td> <% = session. getCreationTime () %> </td>
</Tr>
</Table>
</Body>
</Html>

Stickers:

Download several packages: javolution-5.5.1 memcached-session-manager-1.8.2 memcached-session-manager-tc7-1.8.2

Msm-javolution-serializer-1.8.2 spymemcached-2.10.2 the role of these packages: java through memcached session management

Above these packages: https://github.com/magro/memcached-session-manager/wiki/SetupAndConfiguration

Put the above packages in the/usr/share/tomcat/lib/directory (note that the above packages should not be wrong, which is a potential pitfall)

The versions of these packages must be correct, and I step on a day's pitfall here, if your is tomcat8, corresponding to the memcached-session-manager-tc8-1.8.2

The two memcached servers are deployed on two tomcat servers respectively. You can use yum-y install memcached to download them.

Perform the above steps on tomcatA again on tomcatB. Change TomcatA In the jsp page resource on tomcatB to TomcatB, and change red to blue.

Set up another Nginx server at the front end to achieve load balancing, start memcached, start tomcat, start Nginx, and go to the browser for verification.

 

The verification is successful, and the Session ID remains the same.

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.