您的位置:首页 > 数据库 > Redis

spring session +cluster redis(spring-session-data-redis)

2017-12-20 16:20 495 查看
pom.xml
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
<version>1.2.1.RELEASE</version>
</dependency>

<!-- redis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.8.1</version>
</dependency>


web.xml

<filter>
<filter-name>springSessionRepositoryFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSessionRepositoryFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


applicationContext.xml

<!--引入配置文件-->
<bean id="placeholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="order" value="1"/>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="locations">
<list>
<value>classpath:redis.properties</value>
</list>
</property>
</bean>

<bean id="redisHttpSessionConfiguration"
class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
<property name="maxInactiveIntervalInSeconds" value="600"/>
</bean>

<!--配置 jedis pool-->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!-- 最大连接数 -->
<property name="maxTotal" value="${redis.pool.maxTotal}"/>
<!-- 最大空闲时间 -->
<property name="maxIdle" value="${redis.pool.maxIdle}"/>
<!-- 每次最大连接数 -->
<property name="numTestsPerEvictionRun" value="${redis.pool.numTestsPerEvictionRun}"/>
<!-- 释放扫描的扫描间隔 -->
<property name="timeBetweenEvictionRunsMillis" value="${redis.pool.timeBetweenEvictionRunsMillis}"/>
<!-- 连接的最小空闲时间 -->
<property name="minEvictableIdleTimeMillis" value="${redis.pool.minEvictableIdleTimeMillis}"/>
<!-- 连接控歘按时间多久后释放,当空闲时间>该值且空闲连接>最大空闲连接数时直接释放 -->
<property name="softMinEvictableIdleTimeMillis" value="${redis.pool.softMinEvictableIdleTimeMillis}"/>
<!-- 获得链接时的最大等待毫秒数,小于0:阻塞不确定时间,默认-1 -->
<property name="maxWaitMillis" value="${redis.pool.maxWaitMillis}"/>
<!-- 在获得链接的时候检查有效性,默认false -->
<property name="testOnBorrow" value="${redis.pool.testOnBorrow}"/>
<!-- 在空闲时检查有效性,默认false -->
<property name="testWhileIdle" value="${redis.pool.testWhileIdle}"/>
<!-- 连接耗尽时是否阻塞,false报异常,true阻塞超时 默认:true-->
<property name="blockWhenExhausted" value="${redis.pool.blockWhenExhausted}"/>
</bean>

<!--配置RedisClusterConfiguration-->
<bean id="redisClusterConfiguration" class="org.springframework.data.redis.connection.RedisClusterConfiguration">
<property name="maxRedirects" value="${redis.maxRedirects}"></property>
<property name="clusterNodes">
<set>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="${redis.host1}"/>
<constructor-arg name="port" value="${redis.port1}"/>
</bean>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="${redis.host2}"/>
<constructor-arg name="port" value="${redis.port2}"/>
</bean>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="${redis.host3}"/>
<constructor-arg name="port" value="${redis.port3}"/>
</bean>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="${redis.host4}"/>
<constructor-arg name="port" value="${redis.port4}"/>
</bean>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="${redis.host5}"/>
<constructor-arg name="port" value="${redis.port5}"/>
</bean>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="${redis.host6}"/>
<constructor-arg name="port" value="${redis.port6}"/>
</bean>
</set>
</property>
</bean>

<!--配置JedisConnectionFactory-->
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<constructor-arg name="poolConfig" ref="jedisPoolConfig"/>
<constructor-arg name="clusterConfig" ref="redisClusterConfiguration"/>
</bean>


redis.properties

#JedisPoolConfig的参数
#最大连接数
redis.pool.maxTotal=30
#最大空闲时间
redis.pool.maxIdle=10
#每次最大连接数
redis.pool.numTestsPerEvictionRun=1024
#释放扫描的扫描间隔
redis.pool.timeBetweenEvictionRunsMillis=30000
#连接的最小空闲时间
redis.pool.minEvictableIdleTimeMillis=1800000
#连接控歘按时间多久后释放,当空闲时间>该值且空闲连接>最大空闲连接数时直接释放
redis.pool.softMinEvictableIdleTimeMillis=10000
#获得链接时的最大等待毫秒数,小于0:阻塞不确定时间,默认-1
redis.pool.maxWaitMillis=1500
#在获得链接的时候检查有效性,默认false
redis.pool.testOnBorrow=true
#在空闲时检查有效性,默认false
redis.pool.testWhileIdle=true
#连接耗尽时是否阻塞,false报异常,true阻塞超时,默认true
redis.pool.blockWhenExhausted=false
#RedisClusterConfiguration配置
redis.maxRedirects=5
#主机和端口号
redis.host1=20.26.17.186
redis.port1=7000
redis.host2=20.26.17.186
redis.port2=7001
redis.host3=20.26.17.186
redis.port3=7002
redis.host4=20.26.17.186
redis.port4=7003
redis.host5=20.26.17.186
redis.port5=7004
redis.host6=20.26.17.186
redis.port6=7005


将应用部署到2台机器上
hello.jsp

机器A的jsp:

机器B的jsp:

地址栏输入
http://127.0.0.1:8080/ssm-web2/jsp/hello.jsp http://127.0.0.1:9090/ssm-web2/jsp/hello.jsp
可以发现,访问8080时设置的session  AA属性在访问9090时就可以看到

在redis中可以看到

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  session spring redis