Tomcat under Redis implements session sharing

Source: Internet
Author: User

1. Implement session sharing in a distributed deployment scenario. Here is my test environment and configuration.

Version 2.Redis 3.0.6 Tomcat version: 7.0 JDK version: 1.7

3. The required jar package and version

Commons-pool-1.3.jar

Jedis-2.0.0.jar

Tomcat-redis-session-manager-1.2-tomcat-7-java-7.jar

4.Reids installation Configuration

Redis uses a master and standby configuration (installed on a single machine, so you need to configure a different port number). The installation directory is as follows:

Redis-3.0.6-master Redis-3.0.6-slave

Where Master is the default configuration. Port good for the default 6379,slave needs to be modified accordingly.

Slave configuration file redis.conf port and SLAVOF

Where port is modified to 6380

Modify the salveof to correspond to the master IP and port.

Slaveof 192.168.2.64 6379

5. Test procedures

The test project is myweb, the login page only shows SessionID, the main code of the page is as follows:

<body>
<%
HttpSession s = request.getsession ();
S.setattribute ("SessionId", S.getid ());
%>
The value to get the session is <%=s.getattribute ("SessionId")%>
</body>

Deploy MyWeb on TOMCAT1 and TOMCAT2, respectively. Where TOMCAT1 port number is 8080,TOMCAT2 port number is 8081

Configuration of 6.Tomcat

A. Modify the port numbers for TOMCAT1 and TOMCAT2, respectively, 8080,8081 for testing.

B. At the same time, you need to copy the required jar package to the Lib under Tomcat. Commons-pool-1.3.jar, Jedis-2.0.0.jar, Tomcat-redis-session-manager-1.2-tomcat-7-java-7.jar

C. Modify the Context.xml file for TOMCAT1 and TOMCAT2. Add support for Redis. In the context, add the following configuration:

<valve classname= "Com.radiadesign.catalina.session.RedisSessionHandlerValve"/>
<manager classname= "Com.radiadesign.catalina.session.RedisSessionManager"
Host= "192.168.2.64"
Port= "6379"
database= "0"
Maxinactiveinterval= "/>"

7. Start the master and slave of Redis, and the TOMCAT1 and TOMCAT2 of Tomcat, respectively. Access TOMCAT1 Displays the SessionID as:

The value to get the session is CCA7E2B7A254323F63658828F9DE2B10

Access TOMCAT2 Displays the SessionID as:

The value to get the session is CCA7E2B7A254323F63658828F9DE2B10

Also look at the SessionID in Redis as:

127.0.0.1:6379> Get CCA7E2B7A254323F63658828F9DE2B10
"\xac\xed\x00\x05w\b\x00\x00\x01t{\xaa\x7fnsr\x00\x0

8. Through the above configuration, to achieve the different Tomcat under the session of the sharing. The front desk can be done by Nginx Proxy, to achieve different tomcat under the switch.

Tomcat under Redis implements session sharing

Related Article

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.