1. 云栖社区>
  2. 技术文集>
  3. 列表>
  4. 正文

redis+tomcat7实现session共享

作者:用户 来源:互联网 时间:2018-06-24 19:16:05

技术

redis+tomcat7实现session共享 - 摘要: 本文讲的是redis+tomcat7实现session共享, PS:截止到2015-05-12前是不支持Tomcat8的,详情见官网:https://github.com/jcoleman/tomcat-redis-session-manager 前提:你已经部署了Re

PS:截止到2015-05-12前是不支持Tomcat8的,详情见官网:https://github.com/jcoleman/tomcat-redis-session-manager

前提:你已经部署了Redis,尚未学会的,可以移步这里:http://blog.csdn.net/caiwenfeng_for_23/article/details/45511007

我的案例下载:http://download.csdn.net/detail/caiwenfeng_for_23/8689847

其实很简单,就几个步骤: 
1.配置Tomcat的conf目录下的context.xml文件:

1> 单点Reids配置

<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>        
    <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);">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" style="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);">"60"</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集群配置:

<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 配置 --></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>        
    <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);">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" 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);">"60"</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><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></ul>

2.添加jar

redis+tomcat7实现session共享

3.测试

1> 
存储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;">//取得Session对象</span>
        HttpSession session=request.getSession(); 
        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//设置Session属性</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>重启Tomcat:假如Session保存在tomcat下,重启后Session不存在;如果保存在Redis下,Tomcat重启对Session无影响

3>取出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;">//取得Session对象</span>
        HttpSession session=request.getSession(); 
        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//取出Session属性</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: border-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>

注意事项:从Tomcat6开始默认开启了Session持久化设置,测试时可以关闭本地Session持久化,其实也很简单,在Tomcat的conf目录下的context.xml文件中,取消注释下面那段配置即可:

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

详见这篇博客:Session持久化的实例分析

可以尝试运行上面的demo案例。

以上是云栖社区小编为您精心准备的的内容,在云栖社区的博客、问答、公众号、人物、课程等栏目也有 的相关内容,欢迎继续使用右上角搜索按钮进行搜索技术 ,以便于您获取更多的相关知识。

tomcat7用redis存储session报错

问题描述 tomcat7用redis存储session报错 com.orangefunction.tomcat.redissessions.RedisSessionManager sessionFromSerializedData SEVERE: Unable to deserialize into session java.lang.ClassNotFoundException: 解决...

CentOS6.5下Tomcat7 Nginx Redis配置步骤教程详解_Linux

...:由于Redis配置对jar包和tomcat版本比较严格,请务必使用tomcat7和本文中提供的jar包。 下载地址: http://pan.baidu.com/s/1bO67Ky tomcat: tomcat1 localhost:8080 tomcat2 localhost:9080 nginx: localhost:1210 redis: localhost:6379 1. tomcat的安装和配置 1. 在server.xm...

使用redis、memchache实现PHP sessions共享

默认情况下,PHP将session存放到当前服务器/tmp目录下,一个session一个文件,全部以sess_前缀开始。随着session文件的增加出现性能问题,并且不利于项目的扩展:session不共享,导致session丢失。 为了解决这个问题,我们可以将ses...

session会话-nginx-mysql-tomcat

...到本机,这样实现session共享和高可用。 二、本配置基于tomcat7环境,通过tomcat集群自带的session复制,session信息将会被自动复制到各个节点。 2、在tomcat-1和tomcat-2节点上安装JDK 在安装tomcat之前必须先安装JDK,JDK的全称是java develop...

在nodejs使用Redis缓存和查询数据及Session持久化(Express)

...xpressJS(NodeJS)中 设置二级域名跨域共享Cookie  中提及将Session存放到Mongodb中,其中有很多讲解的不是很详细。 我们为什么要把Session存放到数据中,以及又为什么要在子域名间跨域共享Cookie呢。 Session与Cookie的关系客户端与服...

前三篇
后三篇