SpringBoot SpringSession redis 共享 SESSION
号称无缝整合httpsession 共享,
但注意如果存在第三方框架,例如SESSION并发控制,这个是需要自己重写session名单的.
关于redis session 共享 的session并发控制重写,请看我另一篇http://www.cnblogs.com/sweetchildomine/p/7007242.html
POM
<!--redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>1.5.4.RELEASE</version>
</dependency>
<!-- redis session -->
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
<version>1.3.1.RELEASE</version>
</dependency>
RedisSessionConfig
/**
* Created by ZhenWeiLai on 2017/6/11.
*/
@Configuration
//maxInactiveIntervalInSeconds session超时时间,单位秒
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 600)
public class RedisSessionConfig {
}
RedisCacheConfig
package com.lzw.core.configuration; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; /**
* Created by ZhenWeiLai on 2017/6/11.
*/
@Configuration
@EnableCaching
public class RedisCacheConfig extends CachingConfigurerSupport {
Logger logger = LoggerFactory.getLogger(RedisCacheConfig.class);
@Value("${spring.redis.host}")
private String host; @Value("${spring.redis.port}")
private int port; @Value("${spring.redis.timeout}")
private int timeout; @Value("${spring.redis.pool.max-idle}")
private int maxIdle; @Value("${spring.redis.pool.max-wait}")
private long maxWaitMillis; @Value("${spring.redis.password}")
private String password; @Bean
public JedisPool redisPoolFactory() {
logger.info("JedisPool注入成功!!");
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxIdle(maxIdle);
jedisPoolConfig.setMaxWaitMillis(maxWaitMillis); JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, password); return jedisPool;
} @Bean
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory cf) {
RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>();
redisTemplate.setConnectionFactory(cf);
return redisTemplate;
} @Bean
public CacheManager cacheManager(RedisTemplate redisTemplate) {
RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate); //默认超时时间,单位秒
cacheManager.setDefaultExpiration(3000);
//根据缓存名称设置超时时间,0为不超时
Map<String,Long> expires = new ConcurrentHashMap<>();
cacheManager.setExpires(expires); return cacheManager;
}
}
application.yml
spring:
datasource:
# readSize: 1
# name: writeDataSource
type: com.alibaba.druid.pool.DruidDataSource
write:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/cloud?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&useSSL=true
username: root
password: 123
initialSize: 10
maxActive: 100
maxWait: 60000
minIdle: 5
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20 #redis配置
redis:
host: 192.168.1.11
port: 6379
# REDIS (RedisProperties)
# Redis数据库索引(默认为0)
database: 0
# Redis服务器连接密码(默认为空)
password:
# 连接池最大连接数(使用负值表示没有限制)
# 连接超时时间(毫秒)
timeout: 0
pool:
max-active: 8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
max-wait: -1
# 连接池中的最大空闲连接
max-idle: 8
# 连接池中的最小空闲连接
min-idle: 0
SpringBoot SpringSession redis 共享 SESSION的更多相关文章
- SpringBoot,Security4, redis共享session,分布式SESSION并发控制,同账号只能登录一次
由于集成了spring session ,redis 共享session,导致SpringSecurity单节点的session并发控制失效, springSession 号称 无缝整合httpses ...
- SpringBoot系列: Redis 共享Session
Web项目Session管理是一个很重要的话题, 涉及到系统横向扩展, SpringBoot已经为共享Session很好的解决方案, 这篇文章关注使用Redis共享会话, 同时这也是最常用的方法. = ...
- SpringBoot+Shiro+Redis共享Session入门小栗子
在单机版的Springboot+Shiro的基础上,这次实现共享Session. 这里没有自己写RedisManager.SessionDAO.用的 crazycake 写的开源插件 pom.xml ...
- Spring-session+Redis解决Session共享
1. 保证Redis启动 2. 导入依赖 SpringBoot+Spring-Session+Redis <!--spring boot 与re ...
- linux下实现redis共享session的tomcat集群
为了实现主域名与子域名的下不同的产品间一次登录,到处访问的效果,因此采用rediss实现tomcat的集群效果.基于redis能够异步讲缓存内容固化到磁盘上,从而当服务器意外重启后,仍然能够让sess ...
- Nginx+tomcat集群使用redis共享session
一 :nginx负载均衡 当Tomcat当做独立的Servlet容器来运行时,可看做是能运行Java Servlet的独立Web服务器. 此外 Tomcat还可以作为其他Web服务器进程内或者进程外的 ...
- Tomcat7.0.99集群使用Redis共享session方案
以前配置过给予多播的session共享方案,这回再配置一个redis共享session的. 先小小的炫耀一下: 相信大家要做Tomcat+Redis+session配置,遇到的头号麻烦就是编译的tom ...
- Spring Boot 使用 Redis 共享 Session 代码示例
参考资料 博客:spring boot + redis 实现session共享 1. 新建 Maven 工程 我新建 spring-boot-session-redis maven 工程 2. 引入 ...
- spring-session+Redis实现Session共享
关于session共享的方式有多种: (1)通过nginx的ip_hash,根据ip将请求分配到对应的服务器 (2)基于关系型数据库存储 (3)基于cookie存储 (4)服务器内置的session复 ...
随机推荐
- chroot: failed to run command `/bin/bash': No such file or directory
1 使用chroot命令时报错如下: testupgrade:/ # chroot /sb chroot: cannot change root directory to /sb: No such f ...
- maven(项目管理工具系列 maven 总结二)
♣maven是什么? ♣maven下载.安装 ♣了解maven仓库 ♣eclipse配置maven ♣创建maven项目 ♣把maven项目转化为web项目 1.maven是什么? Maven是一个项 ...
- 【转】C++易混知识点5:实例讲解Public Protected Private作用域,继承的区别和用意
大学生涯,涉及到类的作用域,继承都是用的public 共有继承,当时也没想那么多,觉得共有继承多方便,多简单,反正没有太多的限制,不管是类的成员或者是基类的成员函数都可以访问.没有深究.其实这里面真是 ...
- APP测试(转载)
(1) 非功能测试 app测试的一个重要方面是app的非功能需求.移动app在推出市场或进行进一步开发前,测试人员有一定的职责做该类需求的跟踪工作. 早期开发阶段要进行的第一个测试应该是实用性测试.通 ...
- HTTP请求和MIME介绍
HTTP请求和MIME介绍 HTTP请求由三部分组成,分别是: 请求行,消息报头,请求正文. 请求行(格式): Method Request-URI HTTP-Version CRLF Method: ...
- IIS与Apache禁止IP地址直接访问网站
一.IIS 防止恶意域名指向解决方法 首先单击"开始"-"设置"-"控制面板"-"管理工具",找到"Inter ...
- fread和fwrite的使用
fread和fwrite的使用 fread和fwrite一般用于二进制文件的输入/输出,要不然你打开fwrite写入的文件就是乱码. 1.fread和fwrite函数 数据块I/O fread与fwr ...
- Python学习笔记(二):字典
字典由多个键及与其对应的值构成的键值对构成,字典中键唯一,值不唯一. 1)dict 函数: >>>items=[('name','lilei'),('age',12)] >&g ...
- linux上的组管理
上一次我们谈了CentOS上的用户管理,现在我们再来谈下CentOS上的用户组管理. groupadd创建一个新的组 用法如下: groupadd [选项] groupname 常用选项: -f 强制 ...
- 单元测试系列:如何使用JUnit+JaCoCo+EclEmma完成单元测试
更多原创测试技术文章同步更新到微信公众号 :三国测,敬请扫码关注个人的微信号,感谢! 原文链接:http://www.cnblogs.com/zishi/p/6726664.html -----如 ...