先说 Redis 的配置,在一些网上资料中,Spring Boot 的 Redis 除了添加依赖外,还要使用 XML 或 Java 配置文件作些配置,不过通过实践并不须要。web
先在 pom 文件中添加 redis 的依赖:redis
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <version>xxx.xxx.xxx.RELEASE</version> </dependency>
若是 Redis 部署在 Spring Boot 运行环境的同一台服务器上,端口为默认的 6379,且没有设置密码,那么其实 Redis 已经可用了。spring
固然这种假设其实在多数状况下并不成立,因此仍是要在配置文件中设置一下,还能够对链接池作个设置:apache
spring.redis.host=xxx.xxx.xxx.xxx #Redis 服务器ip spring.redis.port=6379 #Redis 服务器端口 spring.redis.password=xxxxxx #链接密码 spring.redis.timeout=2000 #超时时间 spring.redis.pool.max-active=8 #最大链接数 spring.redis.pool.max-wait=-1 #链接超时设置 spring.redis.pool.max-idle=8 #最大空闲数 spring.redis.pool.min-idle=0 #最小空闲数
再说 Session,若是直接使用 session,是调用的 org.apache.catalina.session.StandardSessionFacade 作 Session 管理。服务器
要想将 Session 存储在 Redis 中,也很简单,只须要在 pom 中添加 spring-session-data-redis 依赖:session
<dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-data-redis</artifactId> <version>2.1.3.RELEASE</version> </dependency>
不须要在配置文件中添加以下配置(网上多数文章都提到了这个配置):spring-boot
spring.session.store-type=Redis
也不须要在 Java 配置文件中作以下配置:测试
import org.springframework.context.annotation.Configuration; import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession; @Configuration @EnableRedisHttpSession public class SessionConfiguration { }
却是能够在加入了 spring-session-data-redis 依赖后,又在某些状况下(好比本地测试时),不想将 Session 存入 Redis,能够经过在配置文件中加入以下配置项来实现屏蔽:spa
spring.session.store-type=None