Tomcat7+Redis存储Session

来源:转载


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"> <span class="hljs-comment"><!-- Jedis save session --></span> <span class="hljs-tag"><<span class="hljs-title">Valve</span> <span class="hljs-attribute">className</span>=<span class="hljs-value">"com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve"</span> /></span> <span class="hljs-tag"><<span class="hljs-title">Manager</span> <span class="hljs-attribute">className</span>=<span class="hljs-value">"com.orangefunction.tomcat.redissessions.RedisSessionManager"</span> <span class="hljs-attribute">host</span>=<span class="hljs-value">"localhost"</span> <span class="hljs-attribute">port</span>=<span class="hljs-value">"6379"</span> <span class="hljs-attribute">database</span>=<span class="hljs-value">"0"</span> <span class="hljs-attribute">maxInactiveInterval</span>=<span class="hljs-value">"60"</span>/></span></code><ul style="display: block;" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li></ul>

2> Sentinel集群配置:

<code class="hljs xml has-numbering"> <span class="hljs-comment"><!-- Sentinel 配置 --></span> <span class="hljs-tag"><<span class="hljs-title">Valve</span> <span class="hljs-attribute">className</span>=<span class="hljs-value">"com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve"</span> /></span> <span class="hljs-tag"><<span class="hljs-title">Manager</span> <span class="hljs-attribute">className</span>=<span class="hljs-value">"com.orangefunction.tomcat.redissessions.RedisSessionManager"</span> <span class="hljs-attribute">maxInactiveInterval</span>=<span class="hljs-value">"60"</span> <span class="hljs-attribute">sentinelMaster</span>=<span class="hljs-value">"mymaster"</span> <span class="hljs-attribute">sentinels</span>=<span class="hljs-value">"127.0.0.1:26379,127.0.0.1:26380,127.0.0.1:26381,127.0.0.1:26382"</span> /></span></code><ul style="display: block;" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li></ul>

2.添加jar

3.测试

1>
存储Session:

<code class="hljs cs has-numbering"> <span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title">doPost</span>(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.<span class="hljs-keyword">out</span>.println(<span class="hljs-string">"hello"</span>); <span class="hljs-comment">//取得Session对象</span> HttpSession session=request.getSession(); <span class="hljs-comment">//设置Session属性</span> <span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> i=<span class="hljs-number">0</span>;i<<span class="hljs-number">100000</span>;i++){ session.setAttribute(<span class="hljs-string">"name"</span>+i, <span class="hljs-string">"Magci_"</span>+i); } }</code><ul style="display: block;" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li></ul>

2>重启Tomcat:假如Session保存在tomcat下,重启后Session不存在;如果保存在Redis下,Tomcat重启对Session无影响

3>取出Session:

<code class="hljs cs has-numbering"> <span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title">doPost</span>(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.<span class="hljs-keyword">out</span>.println(<span class="hljs-string">"hello"</span>); <span class="hljs-comment">//取得Session对象</span> HttpSession session=request.getSession(); <span class="hljs-comment">//取出Session属性</span> <span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> i=<span class="hljs-number">0</span>;i<<span class="hljs-number">100000</span>;i++){ System.<span class="hljs-keyword">out</span>.println(session.getAttribute(<span class="hljs-string">"name"</span>+i)); } }</code><ul style="display: block;" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li></ul>

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

<code class="hljs xml has-numbering"> <span class="hljs-comment"><!-- Uncomment this to disable session persistence across Tomcat restarts --></span> <span class="hljs-comment"><!-- <Manager pathname="" /> --></span></code><ul style="display: block;" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li></ul>

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

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



分享给朋友:
您可能感兴趣的文章:
随机阅读: