session-缓存-分布式会话session共享、多节点部署session共享解决方案
2017-08-15 13:35
337 查看
基于redis的session共享解决方案:
依赖pom包 spring-seesion-data-redis,实际该包只是将spring-data-redis 、spring-session、jedis(redis-clients) 做了个集合管理,如果项目已引入spring-data-redis、jedis,则直接引入spring-session即可。
可以为session共享所使用的redis单独做配置,即使用redis特定的实例。如果不用特定的连接,则直接在类加@EnableRedisHttpSession 即可
注意:一定要合理配置好redis的连接配置参数,否则会报can note get a jedis from pool.
依赖pom包 spring-seesion-data-redis,实际该包只是将spring-data-redis 、spring-session、jedis(redis-clients) 做了个集合管理,如果项目已引入spring-data-redis、jedis,则直接引入spring-session即可。
可以为session共享所使用的redis单独做配置,即使用redis特定的实例。如果不用特定的连接,则直接在类加@EnableRedisHttpSession 即可
注意:一定要合理配置好redis的连接配置参数,否则会报can note get a jedis from pool.
import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession; import redis.clients.jedis.JedisPoolConfig; /** * 开启session redis存储机制 * Created by YeCongZhi on 2017/8/4. */ @Configuration @EnableRedisHttpSession //maxInactiveIntervalInSeconds 默认是1800秒过期,这里测试修改为60秒 //@EnableRedisHttpSession(maxInactiveIntervalInSeconds=60) public class RedisSessionConfig { Logger logger = LoggerFactory.getLogger(getClass()); @Value("${spring.redis.host:}") private String host; @Value("${spring.redis.port:6379}") private String port; @Value("${spring.redis.timeout:3000}") private String timeout; @Value("${spring.redis.password:}") private String password; @Value("${spring.redis.default.expir:10}") private String defaultExpir; @Value("${redis.maxTotal:-1}")//最大连接数 private int maxTotal; @Value("${redis.maxIdle:20}")//最大空闲连接数 private int maxIdle; @Value("${redis.minIdle:10}")//最小空闲连接数 private int minIdle; @Value("${redis.maxWaitMillis:-1}")//获取连接时的最大等待毫秒数 private Long maxWaitMillis; @Value("${redis.testOnBorrow:false}")//在获取连接的时候检查有效性 private boolean testOnBorrow; @Value("${redis.MinEvictableIdleTimeMillis:60000}")//多长时间后回收空闲连接 private Long MinEvictableIdleTimeMillis; @Bean public JedisConnectionFactory jedisConnectionFactory() { JedisConnectionFactory factory = new JedisConnectionFactory(); factory.setHostName(host); factory.setPort(Integer.parseInt(port)); factory.setTimeout(Integer.parseInt(timeout)); //设置连接超时时间 factory.setPassword(password); JedisPoolConfig poolCofig = poolCofig( maxTotal,maxIdle,minIdle, maxWaitMillis,MinEvictableIdleTimeMillis,testOnBorrow); factory.setPoolConfig(poolCofig); /**初始化连接pool*/ factory.afterPropertiesSet(); return factory; } /**redis配置*/ public JedisPoolConfig poolCofig( int maxTotal,int maxIdle, int minIdle, long maxWaitMillis,long MinEvictableIdleTimeMillis, boolean testOnBorrow) { JedisPoolConfig poolCofig = new JedisPoolConfig(); poolCofig.setMaxTotal(maxTotal); poolCofig.setMaxIdle(maxIdle); poolCofig.setMinIdle(minIdle); poolCofig.setMaxWaitMillis(maxWaitMillis); poolCofig.setMinEvictableIdleTimeMillis(MinEvictableIdleTimeMillis); poolCofig.setTestOnBorrow(testOnBorrow); return poolCofig; } }
相关文章推荐
- 项目分布式部署那些事(2):基于OCS(Memcached)的Session共享方案
- Memcahed分布式缓存服务替换Session解决方案
- Memcahed分布式缓存服务替换Session解决方案
- Memcahed分布式缓存服务替换Session解决方案
- 部署tomcat负载均衡集群,实现节点之间内存中的Session共享。
- Memcahed分布式缓存服务替换Session解决方案
- Memcahed分布式缓存服务替换Session解决方案——简单介绍[转]
- Apache shiro集群实现 (六)分布式集群系统下的高可用session解决方案---Session共享
- 分布式集群系统下的高可用session解决方案—Session共享
- Apache shiro集群实现 (六)分布式集群系统下的高可用session解决方案---Session共享
- 分布式集群环境下Session共享的简单解决方案
- mina —— 分布式集群下长连接session共享的解决方案
- Apache shiro集群实现 (六)分布式集群系统下的高可用session解决方案---Session共享
- Apache shiro集群实现 (六)分布式集群系统下的高可用session解决方案---Session共享
- Tomcat集群节点之门会话共享
- Memcached 分布式缓存系统部署与调试 推荐
- [原创程序] 主域名二级域名共享SESSION解决方案
- 【G】开源的分布式部署解决方案(一) - 开篇
- Java集群之session共享解决方案
- 同一个Tomcat不同Web应用之间共享会话Session