按部就班,由易到难,这样才更有乐趣!html
本篇开始继续上一篇的内容基础上进行,本篇主要介绍Spring-Session实现配置使用Redis集群,会有两种配置方式,一种是Redis-Cluster,一种是Redis-Sentinel,并经过一个简单的demo进行实例演示!git
对Redis-Cluster和Redis-Sentinel不太懂,或者不知道在Windows下面如何搭建的伙伴,请先移步到,github
Redis高可用集群-哨兵模式(Redis-Sentinel)搭建配置教程【Windows环境】web
Redis建立高可用集群教程【Windows环境】redis
进行简单的学习和配置好本次须要的环境!spring
因为有了上一篇的介绍,上一篇中添加依赖:apache
spring-session-data-redis服务器
而这个jar包会自动下载Spring-session和Jedis的依赖session
spring-session
jedis架构
本次开始就直接上代码和配置,并进行简单的说明!
#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
<?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>
<?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就配置稍有不一样,其余同样!
上述三步和上一篇同样,这里就不在介绍!
使用RedisDesktopManager,具体看下面截图,保存成功!
本次集群配置教程结束!
spring-session实现分布式集群session的共享
【第一篇】Spring-Session实现Session共享入门教程
【第二篇】Spring-Session实现Session共享Redis集群方式配置教程
【第三篇】Spring-Session实现Session共享实现原理以及源码解析【更新中...请期待...】
本系列的源码下载地址:learn-spring-session-core
<font color='blue'> **若是您以为这篇博文对你有帮助,请点个赞,让更多的人看到,谢谢!**</font>
<font color='green'> **若是帅气(美丽)、睿智(聪颖),和我同样简单善良的你看到本篇博文中存在问题,请指出,我虚心接受你让我成长的批评,谢谢阅读!<br/>祝你今天开心愉快!**</font>
<font size=4 color=blue>欢迎访问个人csdn博客,咱们一同成长!</font>
"<font size=5>无论作什么,只要坚持下去就会看到不同!在路上,不卑不亢!</font>"
<font size=4 color=red> 博客首页</font>:<a href="http://blog.csdn.net/u010648555" target="_blank">http://blog.csdn.net/u010648555</a>