Spring-session+redis Cluster for Session sharing

Source: Internet
Author: User
Tags constructor redis redis cluster

Realization of session sharing based on Redis cluster

Spring-data-redis support for Redis cluster password from version 1.8.0

Cosmic Conventions first on code

1. Maven Dependency Configuration

<properties> <spring-session.version>1.2.0.RELEASE</spring-session.version> <spring-d Ata-redis.version>1.8.0.release</spring-data-redis.version> <jedis.version>2.9.0</ jedis.version> </properties> <dependencies> <dependency> <groupid>org.spri Ngframework.data</groupid> <artifactId>spring-data-redis</artifactId> <versio n>${spring-data-redis.version}</version> </dependency> <dependency> <g Roupid>redis.clients</groupid> <artifactId>jedis</artifactId> <version> ${jedis.version}</version> </dependency> <dependency> &LT;GROUPID&GT;ORG.SPRINGF Ramework.session</groupid> <artifactId>spring-session</artifactId> &LT;VERSION&G T;${spring-session.version}</versioN> </dependency> </dependencies> 

2. Redis Cluster configuration

<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://www.springframework.org/schema/beans" xmlns:p= "http://www.springframework.org/schema/p" xmlns:context= "
                        Http://www.springframework.org/schema/context "xsi:schemalocation=" Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-beans-4.0.xsd HTTP://WWW.SPR Ingframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0 . xsd "> <bean id=" jedispoolconfig "class=" Redis.clients.jedis.JedisPoolConfig "> <property name=" max Idle "value=" ${redis.maxidle} "/> <property name=" Minidle "value=" ${redis.minidle} "/> <proper Ty name= "maxtotal" value= "${redis.maxtotal}"/> <property name= "Testonborrow" value= "true"/> </be an> <bean id= "ClusterreDisNodes1 "class=" Org.springframework.data.redis.connection.RedisNode "> <constructor-arg value=" 192.168.10.  "/> <constructor-arg value=" 7000 "type=" int "/> </bean> <bean id=" ClusterRedisNodes2 "
        class= "Org.springframework.data.redis.connection.RedisNode" > <constructor-arg value= "192.168.10.61"/> <constructor-arg value= "7000" type= "int"/> </bean> <bean id= "clusterRedisNodes3" class= "org . Springframework.data.redis.connection.RedisNode "> <constructor-arg value=" 192.168.10.52 "/> < Constructor-arg value= "7000" type= "int"/> </bean> <bean id= "clusterRedisNodes4" class= "Org.springfram Ework.data.redis.connection.RedisNode "> <constructor-arg value=" 192.168.10.53 "/> <constructor -arg value= "7000" type= "int"/> </bean> <bean id= "clusterRedisNodes5" class= "Org.springframework.data. Redis.connection.RedisNodE "> <constructor-arg value=" 192.168.10.54 "/> <constructor-arg value=" 7000 "type=" int "/>
        </bean> <bean id= "clusterRedisNodes6" class= "Org.springframework.data.redis.connection.RedisNode" > <constructor-arg value= "192.168.10.57"/> <constructor-arg value= "7000" type= "int"/> </bea n> <bean id= "redisclusterconfiguration" class= "Org.springframework.data.redis.connection.RedisClusterCo Nfiguration "> <property name=" clusternodes "> <set> <ref bean=" Cluste RRedisNodes1 "/> <ref bean=" ClusterRedisNodes2 "/> <ref bean=" Clusterredisnode
                S3 "/> <ref bean=" clusterRedisNodes4 "/> <ref bean=" clusterRedisNodes5 "/> <ref bean= "ClusterRedisNodes6"/> </set> </property> &LT;/BEAN&G

    T <!--Redis Connection--<bean id= "jedisconnectionfactory" class= "Org.springframework.data.redis.connection.jedis.JedisConnec Tionfactory "> <constructor-arg ref=" jedispoolconfig "/> <property name=" password "value=" ${red
    Is.password} "></property> <constructor-arg ref=" redisclusterconfiguration "/> </bean> <!--cache serialization--<bean id= "Keyserializer" class= "org.springframework.data.redis.serializer.StringRed Isserializer "/> <bean id=" ValueSerializer "class=" Org.springframework.data.redis.serializer.GenericJack Son2jsonredisserializer "/> <!--Cache--<bean id=" redistemplate "class=" Org.springframework.data.redis . Core. Redistemplate "> <property name=" connectionfactory "ref=" jedisconnectionfactory "/> <property N
        Ame= "Keyserializer" ref= "Keyserializer"/> <property name= "ValueSerializer" ref= "ValueSerializer"/> <property NAme= "Hashkeyserializer" ref= "Keyserializer"/> <property name= "Hashvalueserializer" ref= "ValueSerializer"/&
    Gt
        </bean> <bean id= "Rediscachemanager" class= "Org.springframework.data.redis.cache.RedisCacheManager" > <constructor-arg index= "0" ref= "redistemplate"/> <property name= "defaultexpiration" value= "${redis.ex Piration} "/> </bean> </beans>

3, Spring-session.xml

<?xml version= "1.0" encoding= "UTF-8"?> <beans
xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
    xmlns= "Http://www.springframework.org/schema/beans" xmlns:context= "http://www.springframework.org/schema/ Context "
    xsi:schemalocation=" Http://www.springframework.org/schema/beans/
        http Www.springframework.org/schema/beans/spring-beans-4.0.xsd ">
    <!--spring-session-
    <bean
        class= "Org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration" >
        <description> session lifetime (seconds) </description>
        <property name= "Maxinactiveintervalinseconds" Value= "/>
        <property name=" Redisnamespace "value=" zero "/>
    </bean>
</beans>

4, Redis.properties

Redis.minidle=5
redis.maxidle=20
redis.maxtotal=100
redis.expiration=1000
redis.maxwait=-1
redis.password=xxxxxxx

4. Last step Modify Web. XML Add Filter

<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>
    </filter-mapping>

Completed
One line of code does not need to be modified, session sharing integration completed, have to sigh spring decoupling.
Spring is the world's master. jpg ...

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.