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

循序渐进,由易到难,这样才更有乐趣!概述本篇开始继续上一篇的内容基础上进行,本篇主要介绍Spring-Session实现配置使用Redis集群,会有两种配置方式,一种是Redis-Cluster,一种是Redis-Sentinel,并通过一个简单的demo进行实例演示!对Redis-Cluster和Redis-Sentinel不太懂,或者不知道在Windows下面如何搭建的…

大家好,又见面了,我是全栈君。

做一个积极的人

编码、改bug、提升自己

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

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

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

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

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

概述

本篇开始继续上一篇的内容基础上进行,本篇主要介绍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配置

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

    <!-- 打开注解方式 -->
    <context:annotation-config/>

    <!--可以將redis配置写入配置文件中-->
    <context:property-placeholder location="classpath:redis.properties"/>

    <!--创建一个Spring Bean的名称springSessionRepositoryFilter实现过滤器。 筛选器负责将HttpSession实现替换为Spring会话支持。在这个实例中,Spring会话得到了Redis的支持。-->
    <bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"/>

    <!--创建了一个RedisConnectionFactory,它将Spring会话连接到Redis服务器。我们配置连接到默认端口(6379)上的本地主机!-->

    <!-- //单机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> -->
    <!--集群Redis-->
    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <!--Redis-Sentinel-->
        <constructor-arg index="0" ref="redisSentinelConfig"/>

        <!--配置Redis连接池 ,测试使用可以不配置,使用默认就行!-->
        <constructor-arg index="1" ref="jedisPoolConfig"/>

    </bean>

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

        <!--最大连接数, 默认8个-->
        <property name="maxTotal" value="${redis.maxTotal}"/>

        <!--最大空闲连接数, 默认8-->
        <property name="maxIdle" value="${redis.maxIdle}"/>

        <!--最小空闲连接数, 默认0-->
        <property name="minIdle" value="${redis.minIdle}"/>

        <!--在获取连接的时候检查有效性, 默认false-->
        <property name="testOnBorrow" value="${redis.testOnBorrow}"/>

        <!--在空闲时检查有效性, 默认false, 新版jedis 不支持这个参数了-->
        <property name="testOnReturn" value="${redis.testOnReturn}"/>

        <!--获取连接时的最大等待毫秒数(如果设置为阻塞时BlockWhenExhausted),如果超时就抛异常, 小于零:阻塞不确定的时间, 默认-1-->
        <property name="maxWaitMillis" value="${redis.maxWaitMillis}"/>

        <!--连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true-->
        <property name="blockWhenExhausted" value="${redis.blockWhenExhausted}"/>

        <!--设置的逐出策略类名, 默认DefaultEvictionPolicy(当连接超过最大空闲时间,或连接数超过最大空闲连接数)-->
        <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配置

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

    <!-- 打开注解方式 -->
    <context:annotation-config/>


    <!--可以將redis配置写入配置文件中-->
    <context:property-placeholder location="classpath:redis.properties"/>

    <!--创建一个Spring Bean的名称springSessionRepositoryFilter实现过滤器。 筛选器负责将HttpSession实现替换为Spring会话支持。在这个实例中,Spring会话得到了Redis的支持。-->
    <bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"/>

    <!--创建了一个RedisConnectionFactory,它将Spring会话连接到Redis服务器。我们配置连接到默认端口(6379)上的本地主机!-->
    <!--集群Redis-->
    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <!--Redis-CLuster-->
        <constructor-arg index="0" ref="redisClusterConfig"/>

        <!--配置Redis连接池 ,可以不配置,使用默认就行!-->
        <constructor-arg index="1" ref="jedisPoolConfig"/>
    </bean>


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

        <!--最大连接数, 默认8个-->
        <property name="maxTotal" value="${redis.maxTotal}"/>

        <!--最大空闲连接数, 默认8-->
        <property name="maxIdle" value="${redis.maxIdle}"/>

        <!--最小空闲连接数, 默认0-->
        <property name="minIdle" value="${redis.minIdle}"/>

        <!--在获取连接的时候检查有效性, 默认false-->
        <property name="testOnBorrow" value="${redis.testOnBorrow}"/>

        <!--在空闲时检查有效性, 默认false, 新版jedis 不支持这个参数了-->
        <property name="testOnReturn" value="${redis.testOnReturn}"/>

        <!--获取连接时的最大等待毫秒数(如果设置为阻塞时BlockWhenExhausted),如果超时就抛异常, 小于零:阻塞不确定的时间, 默认-1-->
        <property name="maxWaitMillis" value="${redis.maxWaitMillis}"/>

        <!--连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true-->
        <property name="blockWhenExhausted" value="${redis.blockWhenExhausted}"/>

        <!--设置的逐出策略类名, 默认DefaultEvictionPolicy(当连接超过最大空闲时间,或连接数超过最大空闲连接数)-->
        <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,具体看下面截图,保存成功!
查看Session的保存情况

本次集群配置教程结束!

参考文章

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

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


本系列教程

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

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

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

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

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


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


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

愿你我在人生的路上能都变成最好的自己,能够成为一个独挡一面的人
【第二篇】Spring-Session实现Session共享Redis集群方式配置教程

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/121109.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • web浏览器缓存机制_网站利用浏览器缓存

    web浏览器缓存机制_网站利用浏览器缓存浏览器缓存机制之一(经典缓存)因为在接手的项目中用到过比较新的HTML5应用缓存,也用到了经典的缓存如设置max-age,Etag之类,而之前一直就是在用着没有去深究其中原理。周末天气不好,懒得出去了,正好找时间总结下。1.关于浏览器缓存记得去年看《HTTP权威指南》的时候,有一章是专门讲浏览器缓存的,一年时间过得太快,逝去的时光还真是如同人群中消失的好姑娘,眼看她嫁给别人。我这里把浏览器缓存分为…

    2022年10月21日
    0
  • xiao77论坛php,论坛

    论坛1.如何加其他人为好友?ID头像下方有“加好友”、“发消息”、“打招呼”和“串个门”的互动功能。在发帖的头像区和该人的个人资料区都可以进行互动,且有不同的奖励。2.看到喜欢的帖子如何保存?可点击帖子下方的“收藏”按钮,然后回到个人空间的收藏应用中查看即可。3.如何查看所有我参与过的帖子?登录成功后,在页面右上角点击“帖子”即可查看。4.如何知道我参与的帖子有了新的回复?当有人回复了你的…

    2022年4月5日
    316
  • 60mph和kmh换算_mph和kmh换算(mph换算器)

    60mph和kmh换算_mph和kmh换算(mph换算器)mph是英里每时的意思吗?如何换算成千米每时?mph是米/小时的意思mitersperhour也可写成m/hAkm/h=A*1000m/hmph是英里每时的意思吗?如何换算成千米每时?MILESPERHOUR英里每小时英里的计速单位,1MPH=1.609KM/H玩极品飞车12,上面的速度是mph,怎么换算啊1英里=5280英尺=63360英寸=1…

    2022年6月28日
    65
  • ubunru 7.10 source.list 标准的官方的

    ubunru 7.10 source.list 标准的官方的

    2021年5月2日
    121
  • 十二个开源UML工具

    十二个开源UML工具本文将为您介绍12个优秀的UML工具:1.StarUMLStarUML(简称SU),是一种创建UML类图,是一种生成类图和其他类型的统一建模语言(UML)图表的工具。StarUML是一个开源项目之一发展快、灵活、可扩展性强(zj)。2.NetbeansUMLPlugin目前支持:Activity图,Class图,Sequence图,State图以及Use…

    2022年7月12日
    14
  • B+树|MYSQL索引使用原则

    B+树|MYSQL索引使用原则

    2020年11月12日
    171

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号