REDIS+TOMCAT7 Implementing session Sharing

Source: Internet
Author: User
Tags redis tomcat

PS: Until 2015-05-12 is not supported TOMCAT8, details see official website: Https://github.com/jcoleman/tomcat-redis-session-manager

Prerequisite: You have deployed Redis, have not learned, can be moved here: http://blog.csdn.net/caiwenfeng_for_23/article/details/45511007

My case Download: http://download.csdn.net/detail/caiwenfeng_for_23/8689847

Actually very simple, just a few steps:
1. Configure the context.xml file under Tomcat's conf directory:

1> Single Point Reids configuration

<code class= "Hljs xml has-numbering" style= "display:block; padding:0px; Color:inherit; Box-sizing:border-box; font-family: ' Source Code Pro ', monospace;font-size:undefined; White-space:pre; border-radius:0px; Word-wrap:normal; background:transparent; " > <span class= "hljs-comment" style= "Color:rgb (136, 0, 0); Box-sizing:border-box; " ><!--Jedis Save session--></span> <span class= "Hljs-tag" style= "Color:rgb (0, 102, 102); Box-sizing:border-box; " ><<span class= "Hljs-title" style= "Box-sizing:border-box; Color:rgb (0, 0, 136); " >Valve</span> <span class= "Hljs-attribute" style= "Box-sizing:border-box; Color:rgb (102, 0, 102); " >classname</span>=<span class= "Hljs-value" style= "Box-sizing:border-box; Color:rgb (0, 136, 0); " > "Com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" </span>/></span> <spa n class= "Hljs-tag" style= "Color:rgb (0, 102, 102); Box-sizing:bordeR-box; " ><<span class= "Hljs-title" style= "Box-sizing:border-box; Color:rgb (0, 0, 136); " >Manager</span> <span class= "Hljs-attribute" style= "Box-sizing:border-box; Color:rgb (102, 0, 102); " >classname</span>=<span class= "Hljs-value" style= "Box-sizing:border-box; Color:rgb (0, 136, 0); " > "Com.orangefunction.tomcat.redissessions.RedisSessionManager" </span> <span class= "Hljs-attribute" St Yle= "Box-sizing:border-box; Color:rgb (102, 0, 102); " >host</span>=<span class= "Hljs-value" style= "Box-sizing:border-box; Color:rgb (0, 136, 0); " > "localhost" </span> <span class= "Hljs-attribute" style= "Box-sizing:border-box; Color:rgb (102, 0, 102); " >port</span>=<span class= "Hljs-value" style= "Box-sizing:border-box; Color:rgb (0, 136, 0); " > "6379" </span> <span class= "Hljs-attribute" style= "Box-sizing:border-box; Color:rgb (102, 0, 102); " >database</span>=<span class= "Hljs-valUE "Style=" Box-sizing:border-box; Color:rgb (0, 136, 0); " > "0" </span> <span class= "Hljs-attribute" style= "Box-sizing:border-box; Color:rgb (102, 0, 102); " >maxinactiveinterval</span>=<span class= "Hljs-value" style= "Box-sizing:border-box; Color:rgb (0, 136, 0); " > "</span>/></span></code><ul class=" pre-numbering "style=" Box-sizing:border-box; Position:absolute; width:50px; top:0px; left:0px; margin:0px; padding:6px 0px 40px; border-right-width:1px; Border-right-style:solid; Border-right-color:rgb (221, 221, 221); List-style:none; Text-align:right; Background-color:rgb (238, 238, 238); " ><li style= "Box-sizing:border-box; padding:0px 5px; " >1</li><li style= "Box-sizing:border-box; padding:0px 5px; " >2</li><li style= "Box-sizing:border-box; padding:0px 5px; " >3</li><li style= "Box-sizing:border-box; padding:0px 5px; " >4</li><li style= "Box-sizing:border-box; Padding:0px 5px; " >5</li><li style= "Box-sizing:border-box; padding:0px 5px; " >6</li><li style= "Box-sizing:border-box; padding:0px 5px; " >7</li><li style= "Box-sizing:border-box; padding:0px 5px; " >8</li><li style= "Box-sizing:border-box; padding:0px 5px; " >9</li></ul>

2> Sentinel cluster configuration:

<code class= "Hljs xml has-numbering" style= "display:block; padding:0px; Color:inherit; Box-sizing:border-box; font-family: ' Source Code Pro ', monospace;font-size:undefined; White-space:pre; border-radius:0px; Word-wrap:normal; background:transparent; " > <span class= "hljs-comment" style= "Color:rgb (136, 0, 0); Box-sizing:border-box; " ><!--Sentinel configuration--></span> <span class= "Hljs-tag" style= "Color:rgb (0, 102, 102); Box-sizing:border-box; " ><<span class= "Hljs-title" style= "Box-sizing:border-box; Color:rgb (0, 0, 136); " >Valve</span> <span class= "Hljs-attribute" style= "Box-sizing:border-box; Color:rgb (102, 0, 102); " >classname</span>=<span class= "Hljs-value" style= "Box-sizing:border-box; Color:rgb (0, 136, 0); " > "Com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" </span>/></span> <spa n class= "Hljs-tag" style= "Color:rgb (0, 102, 102); Box-sizing:border-box; " ><<span class= "Hljs-title" style= "Box-sizing:border-box; Color:rgb (0, 0, 136); " >Manager</span> <span class= "Hljs-attribute" style= "Box-sizing:border-box; Color:rgb (102, 0, 102); " >classname</span>=<span class= "Hljs-value" style= "Box-sizing:border-box; Color:rgb (0, 136, 0); " > "Com.orangefunction.tomcat.redissessions.RedisSessionManager" </span> <span class= "Hljs-attribute" St Yle= "Box-sizing:border-box; Color:rgb (102, 0, 102); " >maxinactiveinterval</span>=<span class= "Hljs-value" style= "Box-sizing:border-box; Color:rgb (0, 136, 0); " > "<span" </span> class= "Hljs-attribute" style= "Box-sizing:border-box; Color:rgb (102, 0, 102); " >sentinelmaster</span>=<span class= "Hljs-value" style= "Box-sizing:border-box; Color:rgb (0, 136, 0); " > "MyMaster" </span> <span class= "Hljs-attribute" style= "Box-sizing:border-box; Color:rgb (102, 0, 102); " >sentinels</span>=<span class= "Hljs-value" style= "Box-sizing:border-box; Color:rgb (0, 136, 0); " > "127.0.0.1:26379,127.0.0.1:26380,127.0.0.1:26381,127.0.0.1:26382" </span>/></span></code& Gt;<ul class= "pre-numbering" style= "Box-sizing:border-box; Position:absolute; width:50px; top:0px; left:0px; margin:0px; padding:6px 0px 40px; border-right-width:1px; Border-right-style:solid; Border-right-color:rgb (221, 221, 221); List-style:none; Text-align:right; Background-color:rgb (238, 238, 238); " ><li style= "Box-sizing:border-box; padding:0px 5px; " >1</li><li style= "Box-sizing:border-box; padding:0px 5px; " >2</li><li style= "Box-sizing:border-box; padding:0px 5px; " >3</li><li style= "Box-sizing:border-box; padding:0px 5px; " >4</li><li style= "Box-sizing:border-box; padding:0px 5px; " >5</li><li style= "Box-sizing:border-box; padding:0px 5px; " >6</li><li style= "Box-sizing:border-box; padding:0px 5px; " &Gt;7</li></ul> 

2. Add jar

3. Testing

1>
Storage session:

<code class= "HLJS cs has-numbering" style= "display:block; padding:0px; Color:inherit; Box-sizing:border-box; font-family: ' Source Code Pro ', monospace;font-size:undefined; White-space:pre; border-radius:0px; Word-wrap:normal; background:transparent; " > <span class= "Hljs-keyword" style= "Color:rgb (0, 0, 136); Box-sizing:border-box; " >protected</span> <span class= "Hljs-keyword" style= "Color:rgb (0, 0, 136); Box-sizing:border-box; " >void</span> <span class= "Hljs-title" style= "Box-sizing:border-box;" >doPost</span> (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {system.<span class= "Hljs-keyword" style= "Color:rgb (0, 0, 136); box-sizing:border-box;" >out</span>.println (<span class= "hljs-string" style= "Color:rgb (0, 136, 0); box-sizing:border-box;"
           > "Hello" </span>); <span class= "hljs-comment" style= "Color:rgb (136, 0, 0); Box-sizing:border-box; " >//get Session Object </span> HttpSession session=request.getsession (); <span class= "hljs-comment" style= "Color:rgb (136, 0, 0); Box-sizing:border-box; " >//Set session properties </span> <span class= "Hljs-keyword" style= "Color:rgb (0, 0, 136); Box-sizing:border-box; " >for</span> (<span class= "Hljs-keyword" style= "Color:rgb (0, 0, 136); box-sizing:border-box;" >int</span> i=<span class= "Hljs-number" style= "Color:rgb (0, 102, 102); Box-sizing:border-box; " >0</span>;i<<span class= "Hljs-number" style= "Color:rgb (0, 102, 102); Box-sizing:border-box; " >100000</span>;i++) {Session.setattribute (<span class= "hljs-string" style= "Color:rgb (0, 136, 0); Box-sizing:border-box; " > "Name" </span>+i, <span class= "hljs-string" style= "Color:rgb (0, 136, 0); Box-sizing:border-box; " 
        > "Magci_" </span>+i); }}</code><ul class= "pre-numbering" style= "Box-sizing:border-box; PositioN:absolute; width:50px; top:0px; left:0px; margin:0px; padding:6px 0px 40px; border-right-width:1px; Border-right-style:solid; Border-right-color:rgb (221, 221, 221); List-style:none; Text-align:right; Background-color:rgb (238, 238, 238); " ><li style= "Box-sizing:border-box; padding:0px 5px; " >1</li><li style= "Box-sizing:border-box; padding:0px 5px; " >2</li><li style= "Box-sizing:border-box; padding:0px 5px; " >3</li><li style= "Box-sizing:border-box; padding:0px 5px; " >4</li><li style= "Box-sizing:border-box; padding:0px 5px; " >5</li><li style= "Box-sizing:border-box; padding:0px 5px; " >6</li><li style= "Box-sizing:border-box; padding:0px 5px; " >7</li><li style= "Box-sizing:border-box; padding:0px 5px; " >8</li><li style= "Box-sizing:border-box; padding:0px 5px; " >9</li></ul>

2> reboot Tomcat: If the session is saved under Tomcat, the session does not exist after the reboot, and if it is saved under Redis, the Tomcat restart has no effect on the session.

3> Remove Session:

<code class= "HLJS cs has-numbering" style= "display:block; padding:0px; Color:inherit; Box-sizing:border-box; font-family: ' Source Code Pro ', monospace;font-size:undefined; White-space:pre; border-radius:0px; Word-wrap:normal; background:transparent; " > <span class= "Hljs-keyword" style= "Color:rgb (0, 0, 136); Box-sizing:border-box; " >protected</span> <span class= "Hljs-keyword" style= "Color:rgb (0, 0, 136); Box-sizing:border-box; " >void</span> <span class= "Hljs-title" style= "Box-sizing:border-box;" >doPost</span> (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {system.<span class= "Hljs-keyword" style= "Color:rgb (0, 0, 136); box-sizing:border-box;" >out</span>.println (<span class= "hljs-string" style= "Color:rgb (0, 136, 0); box-sizing:border-box;"
           > "Hello" </span>); <span class= "hljs-comment" style= "Color:rgb (136, 0, 0); Box-sizing:border-box; " >//get Session Object </span> HttpSession session=request.getsession (); <span class= "hljs-comment" style= "Color:rgb (136, 0, 0); Box-sizing:border-box; " >//Remove Session Properties </span> <span class= "Hljs-keyword" style= "Color:rgb (0, 0, 136); Box-sizing:border-box; " >for</span> (<span class= "Hljs-keyword" style= "Color:rgb (0, 0, 136); box-sizing:border-box;" >int</span> i=<span class= "Hljs-number" style= "Color:rgb (0, 102, 102); Box-sizing:border-box; " >0</span>;i<<span class= "Hljs-number" style= "Color:rgb (0, 102, 102); Box-sizing:border-box; " >100000</span>;i++) {System.<span class= "Hljs-keyword" style= "Color:rgb (0, 0, 136); box-sizing:b Order-box; " >out</span>.println (Session.getattribute (<span class= "hljs-string" style= "Color:rgb" (0, 136, 0); Box-sizing:border-box; "
        > "name" </span>+i)); }}</code><ul class= "pre-numbering" style= "Box-sizing:border-box; Position:absolute; width:50px; top:0px; left:0px; margin:0px; padding:6px 0px 40px; border-right-width:1px; Border-right-style:solid; Border-right-color:rgb (221, 221, 221); List-style:none; Text-align:right; Background-color:rgb (238, 238, 238); " ><li style= "Box-sizing:border-box; padding:0px 5px; " >1</li><li style= "Box-sizing:border-box; padding:0px 5px; " >2</li><li style= "Box-sizing:border-box; padding:0px 5px; " >3</li><li style= "Box-sizing:border-box; padding:0px 5px; " >4</li><li style= "Box-sizing:border-box; padding:0px 5px; " >5</li><li style= "Box-sizing:border-box; padding:0px 5px; " >6</li><li style= "Box-sizing:border-box; padding:0px 5px; " >7</li><li style= "Box-sizing:border-box; padding:0px 5px; " >8</li><li style= "Box-sizing:border-box; padding:0px 5px; " >9</li></ul>

Note: The session persistence setting is turned on by default from Tomcat6, and the local session persistence can be turned off in the test, but it is also very simple, in the context.xml file under Tomcat's Conf directory, uncomment the following configuration:

<code class= "Hljs xml has-numbering" style= "display:block; padding:0px; Color:inherit; Box-sizing:border-box; font-family: ' Source Code Pro ', monospace;font-size:undefined; White-space:pre; border-radius:0px; Word-wrap:normal; background:transparent; " > <span class= "hljs-comment" style= "Color:rgb (136, 0, 0); Box-sizing:border-box; " ><!--Uncomment this to disable session persistence across Tomcat restarts--></span> <span class= "h Ljs-comment "style=" Color:rgb (136, 0, 0); Box-sizing:border-box; " ><!--<manager pathname= ""/>--></span></code><ul class= "pre-numbering" style= "box- Sizing:border-box; Position:absolute; width:50px; top:0px; left:0px; margin:0px; padding:6px 0px 40px; border-right-width:1px; Border-right-style:solid; Border-right-color:rgb (221, 221, 221); List-style:none; Text-align:right; Background-color:rgb (238, 238, 238); " ><li style= "Box-sizing:border-box; padding:0px 5px;" >1</li><li style= "Box-sizing:border-box; padding:0px 5px; " >2</li><li style= "Box-sizing:border-box; padding:0px 5px; " >3</li><li style= "Box-sizing:border-box; padding:0px 5px; " >4</li></ul>

See this blog: example analysis of Session persistence

You can try running the demo case above.

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.