| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
luoyin889
7年前发布

Spring Session + Redis实现分布式Session共享

   <p>通常情况下,Tomcat、Jetty等Servlet容器,会默认将Session保存在内存中。如果是单个服务器实例的应用,将Session保存在服务器内存中是一个非常好的方案。但是这种方案有一个缺点,就是不利于扩展。</p>    <p>目前越来越多的应用采用分布式部署,用于实现高可用性和负载均衡等。那么问题来了,如果将同一个应用部署在多个服务器上通过负载均衡对外提供访问,如何实现Session共享?</p>    <p>实际上实现Session共享的方案很多,其中一种常用的就是使用Tomcat、Jetty等服务器提供的Session共享功能,将Session的内容统一存储在一个数据库(如MySQL)或缓存(如Redis)中。我在以前的一篇博客中有介绍如何配置Jetty的Session存储在MySQL或MongoDB中。</p>    <p>本文主要介绍另一种实现Session共享的方案,不依赖于Servlet容器,而是Web应用代码层面的实现,直接在已有项目基础上加入Spring Session框架来实现Session统一存储在Redis中。如果你的Web应用是基于Spring框架开发的,只需要对现有项目进行少量配置,即可将一个单机版的Web应用改为一个分布式应用,由于不基于Servlet容器,所以可以随意将项目移植到其他容器。</p>    <h2><strong>Maven依赖</strong></h2>    <p>在项目中加入Spring Session的相关依赖包,包括Spring Data Redis、Jedis、Apache Commons Pool:</p>    <pre>  <!-- Jedis -->  <dependency>      <groupId>redis.clients</groupId>      <artifactId>jedis</artifactId>      <version>2.9.0</version>  </dependency>  <!-- Spring Data Redis -->  <dependency>      <groupId>org.springframework.data</groupId>      <artifactId>spring-data-redis</artifactId>      <version>1.7.3.RELEASE</version>  </dependency>  <!-- Spring Session -->  <dependency>      <groupId>org.springframework.session</groupId>      <artifactId>spring-session</artifactId>      <version>1.2.2.RELEASE</version>  </dependency>  <!-- Apache Commons Pool -->  <dependency>      <groupId>org.apache.commons</groupId>      <artifactId>commons-pool2</artifactId>      <version>2.4.2</version>  </dependency>  </pre>    <h2><strong>配置Filter</strong></h2>    <p>在web.xml中加入以下过滤器,注意如果web.xml中有其他过滤器,一般情况下Spring Session的过滤器要放在第一位。</p>    <pre>  <filter>      <filter-name>springSessionRepositoryFilter</filter-name>      <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>  </filter>  <filter-mapping>      <filter-name>springSessionRepositoryFilter</filter-name>      <url-pattern>/*</url-pattern>    <dispatcher>REQUEST</dispatcher>    <dispatcher>ERROR</dispatcher>  </filter-mapping>  </pre>    <h2><strong>Spring配置文件</strong></h2>    <pre>  <bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"/>  <bean class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">   <property name="hostName" value="localhost" />   <property name="password" value="your-password" />   <property name="port" value="6379" />   <property name="database" value="10" />  </bean>  </pre>    <p>只需要以上简单的配置,至此为止即已经完成Web应用Session统一存储在Redis中,可以说是及其简单。</p>    <h2><strong>解决Redis云服务Unable to configure Redis to keyspace notifications异常</strong></h2>    <p>如果是自建服务器搭建Redis服务,以上已经完成了Spring Session配置,这一节就不用看了。不过很多公司为了稳定性、减少运维成本,会选择使用Redis云服务,例如阿里云数据库Redis版、腾讯云存储Redis等。使用过程中会出现异常:</p>    <pre>  Context initialization failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'enableRedisKeyspaceNotificationsInitializer' defined in class path resource [org/springframework/session/data/redis/config/annotation/web/http/RedisHttpSessionConfiguration.class]:  Invocation of init method failed; nested exception is java.lang.IllegalStateException: Unable to configure Redis to keyspace notifications.  See http://docs.spring.io/spring-session/docs/current/reference/html5/#api-redisoperationssessionrepository-sessiondestroyedevent  Caused by: redis.clients.jedis.exceptions.JedisDataException: ERR unknown command config  </pre>    <p>实际上这种异常发生的原因是,很多Redis云服务提供商考虑到安全因素,会禁用掉Redis的config命令:</p>    <p><img src="https://simg.open-open.com/show/4a75863c8035108f903d0c90a93b6c1e.jpg"></p>    <p>在错误提示链接的文档中,可以看到Redis需要以下的配置:</p>    <pre>  redis-cli config set notify-keyspace-events Egx  </pre>    <p>首先要想办法给云服务Redis加上这个配置。</p>    <p>部分Redis云服务提供商可以在对应的管理后台配置:</p>    <p style="text-align: center;"><img src="https://simg.open-open.com/show/30512a3d2d4ae65265cd4317557917cc.jpg"></p>    <p>如果不能在后台配置,可以通过工单联系售后工程师帮忙配置,例如阿里云:</p>    <p style="text-align: center;"><img src="https://simg.open-open.com/show/d64543330a84a1da4ee7538e7cf77de4.jpg"></p>    <p>完成之后,还需要在Spring配置文件中加上一个配置,让Spring Session不再执行config命令:</p>    <p>However, in a secured Redis enviornment the config command is disabled. This means that Spring Session cannot configure Redis Keyspace events for you. To disable the automatic configuration add ConfigureRedisAction.NO_OP as a bean.</p>    <p>配置:</p>    <pre>  <?xml version="1.0" encoding="UTF-8"?>  <beans xmlns="http://www.springframework.org/schema/beans"   xmlns:util="http://www.springframework.org/schema/util"   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans.xsd   http://www.springframework.org/schema/util   http://www.springframework.org/schema/util/spring-util.xsd">     <bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"/>   <bean class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">    <property name="hostName" value="localhost" />    <property name="password" value="your-password" />    <property name="port" value="6379" />    <property name="database" value="10" />   </bean>     <!-- 让Spring Session不再执行config命令 -->   <util:constant static-field="org.springframework.session.data.redis.config.ConfigureRedisAction.NO_OP"/>    </beans>  </pre>    <p> </p>    <p>来自:http://xxgblog.com/2016/09/29/spring-session-redis/</p>    <p> </p>    
 本文由用户 luoyin889 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
 转载本站原创文章,请注明出处,并保留原始链接、图片水印。
 本站是一个以用户分享为主的开源技术平台,欢迎各类分享!
 本文地址:https://www.open-open.com/lib/view/open1475207062407.html
Redis Spring 分布式系统 分布式/云计算/大数据