redis集群怎么共享session
redis集群共享session的示例:
1.建立maven结构的web项目。
2.在对应的文件中添加代码。
pom.xml文件
<!--spring-sessionbegin--><dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.7.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session</artifactId>
<version>1.3.0.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.4.2</version>
<scope>compile</scope>
</dependency>
<!--spring-sessionend-->
配置filter,在web.xml中,添加以下代码,必须位于filter链的最前面。
<!--spring-session--><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>
在applicationContext.xml(spring容器配置文件的名字)中注册需要的bean
<!--redis--><beanid="jedisPoolConfig"class="redis.clients.jedis.JedisPoolConfig">
</bean>
<beanid="jedisConnectionFactory"class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<propertyname="hostName"value="localhost"/>
<propertyname="port"value="6379"/>
<propertyname="password"value="****"/>
<propertyname="usePool"value="true"/>
<propertyname="poolConfig"ref="jedisPoolConfig"/>
</bean>
<beanid="redisTemplate"class="org.springframework.data.redis.core.RedisTemplate">
<propertyname="connectionFactory"ref="jedisConnectionFactory"/>
</bean>
<!--将session放入redis-->
<beanid="redisHttpSessionConfiguration"class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
<propertyname="maxInactiveIntervalInSeconds"value="1800"/>
</bean>
版权声明
本文仅代表作者观点,不代表米安网络立场。
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。