【第二篇】Spring-Session实现Session共享Redis集群方式配置教程

做一个积极的人

编码、改bug、提升自己

我有一个乐园,面向编程,春暖花开!

=================================================

对人工智能感兴趣的伙伴,分享一个我朋友的人工智能教程。零基础!通俗易懂!风趣幽默!大家可以看看是否对自己有帮助,点击这里查看教程。

=================================================

文章目录

    • 概述
    • Spring-Session 集成Redis集群
        • redis.properties
      • Spring-Session 集成Redis-Sentinel
        • Redis-Sentinel配置
      • Spring-Session 集成Redis-Cluster
        • Redis-Cluster配置
      • 演示验证
      • 启动Redis
      • 启动Nginx
      • 启动两台Tomcat
      • 查看Session保存效果
    • 参考文章
    • 本系列教程

循序渐进,由易到难,这样才更有乐趣!

概述

本篇开始继续上一篇的内容基础上进行,本篇主要介绍Spring-Session实现配置使用Redis集群,会有两种配置方式,一种是Redis-Cluster,一种是Redis-Sentinel,并通过一个简单的demo进行实例演示!

对Redis-Cluster和Redis-Sentinel不太懂,或者不知道在Windows下面如何搭建的伙伴,请先移步到,

Redis高可用集群-哨兵模式(Redis-Sentinel)搭建配置教程【Windows环境】

Redis创建高可用集群教程【Windows环境】

进行简单的学习和配置好本次需要的环境!

Spring-Session 集成Redis集群

由于有了上一篇的介绍,上一篇中添加依赖:

spring-session-data-redis

而这个jar包会自动下载Spring-session和Jedis的依赖

spring-session
jedis

本次开始就直接上代码和配置,并进行简单的说明!

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
#重试次数,在执行失败后,进行的重试次数,默认是5
#此值设置过大时,容易报: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 集成Redis-Sentinel

Redis-Sentinel配置


<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">

    
    <context:annotation-config/>

    
    <context:property-placeholder location="classpath:redis.properties"/>

    
    <bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"/>

    

    
    
    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        
        <constructor-arg index="0" ref="redisSentinelConfig"/>

        
        <constructor-arg index="1" ref="jedisPoolConfig"/>

    bean>

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

        
        <property name="maxTotal" value="${redis.maxTotal}"/>

        
        <property name="maxIdle" value="${redis.maxIdle}"/>

        
        <property name="minIdle" value="${redis.minIdle}"/>

        
        <property name="testOnBorrow" value="${redis.testOnBorrow}"/>

        
        <property name="testOnReturn" value="${redis.testOnReturn}"/>

        
        <property name="maxWaitMillis" value="${redis.maxWaitMillis}"/>

        
        <property name="blockWhenExhausted" value="${redis.blockWhenExhausted}"/>

        
        <property name="evictionPolicyClassName" value="${redis.evictionPolicyClassName}"/>

        
    bean>

    
    <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 集成Redis-Cluster

Redis-Cluster配置


<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">

    
    <context:annotation-config/>


    
    <context:property-placeholder location="classpath:redis.properties"/>

    
    <bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"/>

    
    
    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        
        <constructor-arg index="0" ref="redisClusterConfig"/>

        
        <constructor-arg index="1" ref="jedisPoolConfig"/>
    bean>


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

        
        <property name="maxTotal" value="${redis.maxTotal}"/>

        
        <property name="maxIdle" value="${redis.maxIdle}"/>

        
        <property name="minIdle" value="${redis.minIdle}"/>

        
        <property name="testOnBorrow" value="${redis.testOnBorrow}"/>

        
        <property name="testOnReturn" value="${redis.testOnReturn}"/>

        
        <property name="maxWaitMillis" value="${redis.maxWaitMillis}"/>

        
        <property name="blockWhenExhausted" value="${redis.blockWhenExhausted}"/>

        
        <property name="evictionPolicyClassName" value="${redis.evictionPolicyClassName}"/>

        
    bean>

    
    <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>

演示验证

只演示Redis-Cluster,Redis-Cluster和Redis-Cluster就配置稍有不同,其他一样!

启动Redis

启动Nginx

启动两台Tomcat

上述三步和上一篇一样,这里就不在介绍!

查看Session保存效果

使用RedisDesktopManager,具体看下面截图,保存成功!
【第二篇】Spring-Session实现Session共享Redis集群方式配置教程_第1张图片

本次集群配置教程结束!

参考文章

架构设计之Spring-Session分布式集群会话管理

spring-session实现分布式集群session的共享


本系列教程

【入门】分布式Session一致性入门简介

【第一篇】Spring-Session实现Session共享入门教程

【第二篇】Spring-Session实现Session共享Redis集群方式配置教程

【第三篇】Spring-Session实现Session共享实现原理以及源码解析

本系列的源码下载地址:learn-spring-session-core


谢谢你的阅读,如果您觉得这篇博文对你有帮助,请点赞或者喜欢,让更多的人看到!祝你每天开心愉快!



不管做什么,只要坚持下去就会看到不一样!在路上,不卑不亢!

愿你我在人生的路上能都变成最好的自己,能够成为一个独挡一面的人

© 每天都在变得更好的阿飞云

你可能感兴趣的:(Spring技术)