Spring-session implementation session sharing Redis cluster mode configuration tutorial

Source: Internet
Author: User
Tags sessions redis cluster redis server


Gradual, from easy to difficult, so that more fun. 

Overview



This article began to continue on the basis of the content, this article mainly introduces Spring-session implementation configuration using Redis cluster, there will be two ways to configure, one is Redis-cluster, one is Redis-sentinel, and through a simple demo demo for example.



For Redis-cluster and Redis-sentinel do not understand, or do not know how to build under the windows of the partners, please go to,



Redis High-availability cluster-Sentinel mode (Redis-sentinel) set up the Configuration Tutorial "Windows environment"



Redis create a highly available cluster tutorial "Windows environment"



Do a simple learning and configure the needs of the environment. 

spring-session integrated Redis cluster



Because of the previous introduction, add dependencies in the previous article:



Spring-session-data-redis



And this jar pack will automatically download spring-session and Jedis dependencies.



Spring-session
Jedis



This is the beginning of the code and configuration directly, and a simple description.


  redis.properties


#jedisPoolConfig
redis.maxTotal = 10
redis.maxIdle = 8
redis.minIdle = 0
redis.testOnBorrow = true
redis.testOnReturn = true
redis.maxWaitMillis = -1
redis.blockWhenExhausted = true
redis.evictionPolicyClassName = org.apache.commons.pool2.impl.DefaultEvictionPolicy
# redis-sentinel

redis.sentinel1.host = 127.0.0.1
redis.sentinel1.port = 26379

redis.sentinel2.host = 127.0.0.1
redis.sentinel2.port = 26380

redis.sentinel3.host = 127.0.0.1
redis.sentinel3.port = 26381


# redis-cluster
#Retry times, the number of retries to be performed after the execution fails, the default is 5
#When this value is set too large, it is easy to report: Too many Cluster redirections
redis.cluster.maxRedirects = 3

redis.cluster0.host = 127.0.0.1
redis.cluster0.port = 20000

redis.cluster1.host = 127.0.0.1
redis.cluster1.port = 20001

redis.cluster2.host = 127.0.0.1
redis.cluster2.port = 20002

redis.cluster3.host = 127.0.0.1
redis.cluster3.port = 20003

redis.cluster4.host = 127.0.0.1
redis.cluster4.port = 20004

redis.cluster5.host = 127.0.0.1
redis.cluster5.port = 20005



spring-session Integrated Redis-sentinel Redis-sentinel Configuration
<? xml version = "1.0" encoding = "UTF-8"?>
<beans xmlns = "http://www.springframework.org/schema/beans"
       xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
       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.3.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd ">

    <!-Open annotation method->
    <context: annotation-config />

    <!-You can write the redis configuration into the configuration file->
    <context: property-placeholder location = "classpath: redis.properties" />

    <!-Create a Spring Bean with the name springSessionRepositoryFilter to implement the filter.
    The filter is responsible for replacing the HttpSession implementation with Spring session support. In this example, Spring sessions are supported by Redis. ->
    <bean class = "org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration" />

    <!-Created a RedisConnectionFactory that connects a Spring session to a Redis server. We configure the localhost connected to the default port (6379). ->

    <!-// Stand-alone Redis
    <bean class = "org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <constructor-arg ref = "jedisPoolConfig" />
        <property name = "port" value = "6379" />
        <property name = "hostName" value = "localhost" />
    </ bean>
    ->
    <!-Cluster Redis->
    <bean id = "jedisConnectionFactory" class = "org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <!-Redis-Sentinel->
        <constructor-arg index = "0" ref = "redisSentinelConfig" />

        <!-Configure the Redis connection pool. The test use can be configured without default, just use the default. ->
        <constructor-arg index = "1" ref = "jedisPoolConfig" />

    </ bean>

    <bean id = "jedisPoolConfig" class = "redis.clients.jedis.JedisPoolConfig">

        <!-Maximum number of connections, default 8->
        <property name = "maxTotal" value = "$ {redis.maxTotal}" />

        <!-Maximum number of idle connections, default 8->
        <property name = "maxIdle" value = "$ {redis.maxIdle}" />

        <!-Minimum idle connections, default 0->
        <property name = "minIdle" value = "$ {redis.minIdle}" />

        <!-Check validity when getting connection, default false->
        <property name = "testOnBorrow" value = "$ {redis.testOnBorrow}" />

        <!-Check validity when idle, default false, newer version of jedis does not support this parameter->
        <property name = "testOnReturn" value = "$ {redis.testOnReturn}" />

        <!-The maximum number of milliseconds to wait for a connection (if set to BlockWhenExhausted when blocking), if timeout, throw an exception, less than zero: blocking indefinite time, default -1->
        <property name = "maxWaitMillis" value = "$ {redis.maxWaitMillis}" />

        <!-Whether to block when the connection is exhausted, false to report an exception, true to block until timeout, default is true->
        <property name = "blockWhenExhausted" value = "$ {redis.blockWhenExhausted}" />

        <!-Set the eviction policy class name, the default DefaultEvictionPolicy (when the connection exceeds the maximum idle time, or the number of connections exceeds the maximum idle connection)->
        <property name = "evictionPolicyClassName" value = "$ {redis.evictionPolicyClassName}" />

        <!-There are still many configuration parameters. The tuning of the parameters has not been touched yet. I will have the opportunity to organize and organize the project later.->
    </ bean>

    <!-Sentinel Mode Configuration->
    <bean id = "redisSentinelConfig" class = "org.springframework.data.redis.connection.RedisSentinelConfiguration">

        <property name = "master">
            <bean class = "org.springframework.data.redis.connection.RedisNode">
                <property name = "name" value = "mymaster"> </ property>
            </ bean>
        </ property>

        <property name = "sentinels">
            <set>
                <bean id = "sentinel1" class = "org.springframework.data.redis.connection.RedisNode">
                    <constructor-arg name = "host" value = "$ {redis.sentinel1.host}" />
                    <constructor-arg name = "port" value = "$ {redis.sentinel1.port}" />
                </ bean>
                <bean id = "sentinel2" class = "org.springframework.data.redis.connection.RedisNode">
                    <constructor-arg name = "host" value = "$ {redis.sentinel2.host}" />
                    <constructor-arg name = "port" value = "$ {redis.sentinel2.port}" />
                </ bean>
                <bean id = "sentinel3" class = "org.springframework.data.redis.connection.RedisNode">
                    <constructor-arg name = "host" value = "$ {redis.sentinel3.host}" />
                    <constructor-arg name = "port" value = "$ {redis.sentinel3.port}" />
                </ bean>
            </ set>
        </ property>
    </ bean>

</ beans>
spring-session Integrated Redis-cluster redis-cluster Configuration
<? xml version = "1.0" encoding = "UTF-8"?>
<beans xmlns = "http://www.springframework.org/schema/beans"
       xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
       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.3.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd ">

    <!-Open annotation method->
    <context: annotation-config />


    <!-You can write the redis configuration into the configuration file->
    <context: property-placeholder location = "classpath: redis.properties" />

    <!-Create a Spring Bean with the name springSessionRepositoryFilter to implement the filter.
    The filter is responsible for replacing the HttpSession implementation with Spring session support. In this example, Spring sessions are supported by Redis. ->
    <bean class = "org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration" />

    <!-Created a RedisConnectionFactory that connects a Spring session to a Redis server. We configure the localhost connected to the default port (6379). ->
    <!-Cluster Redis->
    <bean id = "jedisConnectionFactory" class = "org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <!-Redis-CLuster->
        <constructor-arg index = "0" ref = "redisClusterConfig" />

        <!-Configure the Redis connection pool. You can leave it unconfigured and use the default. ->
        <constructor-arg index = "1" ref = "jedisPoolConfig" />
    </ bean>


    <bean id = "jedisPoolConfig" class = "redis.clients.jedis.JedisPoolConfig">

        <!-Maximum number of connections, default 8->
        <property name = "maxTotal" value = "$ {redis.maxTotal}" />

        <!-Maximum number of idle connections, default 8->
        <property name = "maxIdle" value = "$ {redis.maxIdle}" />

        <!-Minimum idle connections, default 0->
        <property name = "minIdle" value = "$ {redis.minIdle}" />

        <!-Check validity when getting connection, default false->
        <property name = "testOnBorrow" value = "$ {redis.testOnBorrow}" />

        <!-Check validity when idle, default false, newer version of jedis does not support this parameter->
        <property name = "testOnReturn" value = "$ {redis.testOnReturn}" />

        <!-The maximum number of milliseconds to wait for a connection (if set to BlockWhenExhausted when blocking), if timeout, throw an exception, less than zero: blocking indefinite time, default -1->
        <property name = "maxWaitMillis" value = "$ {redis.maxWaitMillis}" />

        <!-Whether to block when the connection is exhausted, false to report an exception, true to block until timeout, default is true->
        <property name = "blockWhenExhausted" value = "$ {redis.blockWhenExhausted}" />

        <!-Set the eviction policy class name, the default DefaultEvictionPolicy (when the connection exceeds the maximum idle time, or the number of connections exceeds the maximum idle connection)->
        <property name = "evictionPolicyClassName" value = "$ {redis.evictionPolicyClassName}" />

        <!-There are still many configuration parameters. The tuning of the parameters has not been touched yet. I will have the opportunity to organize and organize the project later.->
    </ bean>

    <!-Cluster Mode Configuration->
    <bean id = "redisClusterConfig" class = "org.springframework.data.redis.connection.RedisClusterConfiguration">
        <property name = "maxRedirects" value = "$ {redis.cluster.maxRedirects}" />
        <property name = "clusterNodes">
                <set>
                    <bean id = "cluster0" class = "org.springframework.data.redis.connection.RedisNode">
                        <constructor-arg name = "host" value = "$ {redis.cluster0.host}" />
                        <constructor-arg name = "port" value = "$ {redis.cluster0.port}" />
                    </ bean>
                    <bean id = "cluster1" class = "org.springframework.data.redis.connection.RedisNode">
                        <constructor-arg name = "host" value = "$ {redis.cluster1.host}" />
                        <constructor-arg name = "port" value = "$ {redis.cluster1.port}" />
                    </ bean>
                    <bean id = "cluster2" class = "org.springframework.data.redis.connection.RedisNode">
                        <constructor-arg name = "host" value = "$ {redis.cluster2.host}" />
                        <constructor-arg name = "port" value = "$ {redis.cluster2.port}" />
                    </ bean>
                    <bean id = "cluster3" class = "org.springframework.data.redis.connection.RedisNode">
                        <constructor-arg name = "host" value = "$ {redis.cluster3.host}" />
                        <constructor-arg name = "port" value = "$ {redis.cluster3.port}" />
                    </ bean>
                    <bean id = "cluster4" class = "org.springframework.data.redis.connection.RedisNode">
                        <constructor-arg name = "host" value = "$ {redis.cluster4.host}" />
                        <constructor-arg name = "port" value = "$ {redis.cluster4.port}" />
                    </ bean>
                    <bean id = "cluster5" class = "org.springframework.data.redis.connection.RedisNode">
                        <constructor-arg name = "host" value = "$ {redis.cluster5.host}" />
                        <constructor-arg name = "port" value = "$ {redis.cluster5.port}" />
                    </ bean>
            </ set>
            </ property>
    </ bean>

</ beans>
Demo Validation


Only demo Redis-cluster,redis-cluster and Redis-cluster are configured slightly differently, the other is the same. 


Start Redis start Nginx start two Tomcat



The above three steps are the same as the previous one, here is not the introduction.

  View Session Save effect



Use Redisdesktopmanager, specific look at the screenshot below, save success.


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.