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

概述

本篇开始继续上一篇的内容基础上进行,本篇主要介绍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,具体看下面截图,保存成功!

本次集群配置教程结束!

参考文章

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

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


本系列教程

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

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

【第三篇】Spring-Session实现Session共享实现原理以及源码解析【更新中...请期待...】

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


**如果您觉得这篇博文对你有帮助,请点个赞,让更多的人看到,谢谢!**

**如果帅气(美丽)、睿智(聪颖),和我一样简单善良的你看到本篇博文中存在问题,请指出,我虚心接受你让我成长的批评,谢谢阅读!
祝你今天开心愉快!**


欢迎访问我的csdn博客,我们一同成长!

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

博客首页:http://blog.csdn.net/u010648555

Spring-Session实现Session共享Redis集群方式配置教程的更多相关文章

  1. spring boot下JedisCluster方式连接Redis集群的配置

    最近在使用springboot做项目,使用redis做缓存.在外网开发的时候redis服务器没有使用集群配置,所有就是用了RedisTemplate的方式进行连接redis服务器.但是项目代码挪到内网 ...

  2. redis单点、redis主从、redis哨兵sentinel,redis集群cluster配置搭建与使用

    目录 redis单点.redis主从.redis哨兵 sentinel,redis集群cluster配置搭建与使用 1 .redis 安装及配置 1.1 redis 单点 1.1.2 在命令窗口操作r ...

  3. Redis集群的配置

    [转]Redis集群的配置 一:memcache 和 Redis 对比总结 [memecache 特点] 1:速度最快(没有自测,但网上有详细的测试用例) 2:支持水平扩展,可以任意添加节点 [red ...

  4. 超详细,多图文介绍redis集群方式并搭建redis伪集群

    超详细,多图文介绍redis集群方式并搭建redis伪集群 超多图文,对新手友好度极好.敲命令的过程中,难免会敲错,但为了截好一张合适的图,一旦出现一点问题,为了好的演示效果,就要从头开始敲.且看且珍 ...

  5. Spring Boot2.0之 整合Redis集群

    项目目录结构: pom: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http:// ...

  6. SpringBoot系列教程之Redis集群环境配置

    之前介绍的几篇redis的博文都是基于单机的redis基础上进行演示说明的,然而在实际的生产环境中,使用redis集群的可能性应该是大于单机版的redis的,那么集群的redis如何操作呢?它的配置和 ...

  7. Redis集群模式配置

    redis集群部署安装: https://blog.csdn.net/huwh_/article/details/79242625 https://www.cnblogs.com/mafly/p/re ...

  8. Redis集群部署一直卡在Waiting for the cluster to join ......(Redis集群总线配置)

    redis集群总线端口为redis客户端端口加上10000,比如说你的redis 6379端口为客户端通讯端口,那么16379端口为集群总线端口 我搭建的redis集群中端口号是从 7001 ~ 70 ...

  9. Redis集群方式

    Redis有三种集群方式:主从复制,哨兵模式和集群. 1.主从复制 主从复制原理: 从服务器连接主服务器,发送SYNC命令: 主服务器接收到SYNC命名后,开始执行BGSAVE命令生成RDB文件并使用 ...

随机推荐

  1. apigateway-kong(四)负载均衡理论及实现

    负载均衡(Load balancing)是一种计算机网络技术,用来在多个计算机(计算机集群).网络连接.CPU.磁盘驱动器或其他资源中分配负载,以达到最佳化资源使用.最大化吞吐率.最小化响应时间.同时 ...

  2. Delphi 的内存操作函数(5): 复制内存

    MoveMemory.CopyMemory 的功能类似, 都是复制内存, 都是调用 Move 过程; MoveMemory.CopyMemory 操作指针; Move 操作实体. 还要注意, 它们的参 ...

  3. ODPS_ele—UDF Python API

    自定义函数(UDF) UDF全称User Defined Function,即用户自定义函数.ODPS提供了很多内建函数来满足用户的计算需求,同时用户还可以通过创建自定义函数来满足不同的计算需求.UD ...

  4. 无锁并发框架Disruptor学习入门

    刚刚听说disruptor,大概理一下,只为方便自己理解,文末是一些自己认为比较好的博文,如果有需要的同学可以参考. 本文目标:快速了解Disruptor是什么,主要概念,怎么用 1.Disrupto ...

  5. HTML5 CSS Reset

    最近在学习HTML和CSS,发现一个不错的模板,放于此处. /* html5doctor.com Reset Stylesheet v1.6.1 Last Updated: 2010-09-17 Au ...

  6. 让浏览器重新下载css文件,解决不刷新缓存的问题

    网站页面源代码中的css文件和js文件后面带一个问号,后面跟着一连串数字或字符,问号起不到实际作用,仅能当作后缀,如果用问号加参数的方法,可以添加版本号等信息 它的作用有:1.作为版本号,让自己方便记 ...

  7. 关于sru源码class Model的parameters

    class Model(nn.Module): def __init__(self, words, args): super(Model, self).__init__() self.args = a ...

  8. linux backtrace()详细使用说明,分析Segmentation fault【转】

    转自:http://velep.com/archives/1032.html 在此之前,开发eCos应用程序时,经常碰到程序挂掉后,串口打印输出一大串让人看不懂的数据.今天才明白,原来这些数据是程序挂 ...

  9. python psutil监控系统资源【转】

    通过 运用 Python 第三方 系统 基础 模块, 可以 轻松 获取 服务 关键 运营 指标 数据,包括 Linux 基本 性能. 块 设备. 网卡 接口. 系统 信息. 网络 地址 库 等 信息. ...

  10. 基于 Apache 在本地配置多个虚拟主机

    如何使用 Apache 在本地配置出多个虚拟主机呢?而且使用不同的“域名”来访问本地不同的站点呢? 一般情况下,咱们都使用 localhost 来访问本机上的服务器,在我们的 C:/WINDOWS/s ...