springboot2.0-啟動cache和session同時存入redis(使用不同的資料庫)
阿新 • • 發佈:2019-02-03
1.在springboot2.0中,通過pom.xml引入spring-boot-starter-cache,spring-boot-starter-data-redis,spring-session-data-redis;可以自動將cache和session資料存入redis資料庫.
2.在application.properties中配置如下內容:
#session使用redis進行儲存 spring.session.store-type=redis spring.session.redis.namespace=authSession spring.session.redis.flush-mode=immediate #cache使用redis儲存 spring.cache.type=redis spring.security.user.password=123 spring.security.user.name=user
#redis記憶體資料庫地址配置 spring.redis.database=1 spring.redis.host=localhost spring.redis.port=6379 spring.redis.jedis.pool.max-active=8 spring.redis.jedis.pool.max-idle=8 spring.redis.jedis.pool.max-wait3.通過以後配置後,springboot的cache和session就自動存至redis中;經過以上3步的配置後,springboot非常簡單和方便的就存至redis了, 但現在有一個問題,由於cache和session的內容均存入了資料庫1中,如果想讓cache和session分別存至不同的資料庫?該如何處理呢?=-1 spring.redis.timeout=5000
4.我是通過擴充套件session的自動配置來完成這個功能需求的,首先在application.properties中增加一個引數:
spring.session.redis.database=5
5.建立一個擴充套件類MyRedisHttpSessionConfiguration繼承RedisHttpSessionConfiguration
packagecom.lxht.emos.config; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.data.redis.RedisProperties; import org.springframework.context.ApplicationEventPublisher; 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.lettuce.LettuceConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; import org.springframework.session.data.redis.RedisFlushMode; import org.springframework.session.data.redis.RedisOperationsSessionRepository; import org.springframework.session.data.redis.config.ConfigureNotifyKeyspaceEventsAction; import org.springframework.session.data.redis.config.ConfigureRedisAction; import org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration; import org.springframework.util.StringUtils; /** * Created by fanyuli on 2018/5/28. */ @Configuration public class MyRedisHttpSessionConfiguration extends RedisHttpSessionConfiguration{ private RedisSerializer<Object> defaultRedisSerializer; private String redisNamespace = "spring:session"; private ApplicationEventPublisher applicationEventPublisher; private Integer maxInactiveIntervalInSeconds = Integer.valueOf(1800); private RedisFlushMode redisFlushMode; private String cleanupCron; private ConfigureRedisAction configureRedisAction; private ClassLoader classLoader; @Autowired RedisProperties redisProperties; @Value("${spring.session.redis.database}") private int sessionRedisDatabase; public MyRedisHttpSessionConfiguration() { this.redisFlushMode = RedisFlushMode.ON_SAVE; this.cleanupCron = "0 * * * * *"; this.configureRedisAction = new ConfigureNotifyKeyspaceEventsAction(); } @Bean public RedisOperationsSessionRepository sessionRepository() { RedisTemplate<Object, Object> redisTemplate = this.createRedisTemplate(); RedisOperationsSessionRepository sessionRepository = new RedisOperationsSessionRepository(redisTemplate); sessionRepository.setApplicationEventPublisher(this.applicationEventPublisher); if(this.defaultRedisSerializer != null) { sessionRepository.setDefaultSerializer(this.defaultRedisSerializer); } sessionRepository.setDefaultMaxInactiveInterval(this.maxInactiveIntervalInSeconds.intValue()); if(StringUtils.hasText(this.redisNamespace)) { sessionRepository.setRedisKeyNamespace(this.redisNamespace); } sessionRepository.setRedisFlushMode(this.redisFlushMode); return sessionRepository; } public RedisTemplate<Object, Object> createRedisTemplate() { RedisTemplate<Object, Object> redisTemplate = new RedisTemplate(); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); if(this.defaultRedisSerializer != null) { redisTemplate.setDefaultSerializer(this.defaultRedisSerializer); } RedisConnectionFactory lettuceConnectionFactory = redisConnectionFactory(); redisTemplate.setConnectionFactory(lettuceConnectionFactory); redisTemplate.setBeanClassLoader(this.classLoader); redisTemplate.afterPropertiesSet(); return redisTemplate; } public RedisConnectionFactory redisConnectionFactory() {
String host = redisProperties.getHost(); int port = redisProperties.getPort(); LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory(host,port); lettuceConnectionFactory.setDatabase(sessionRedisDatabase); lettuceConnectionFactory.afterPropertiesSet(); return lettuceConnectionFactory; } @Autowired public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) { this.applicationEventPublisher = applicationEventPublisher; } }6.通過對RedisHttpSessionConfiguration進行擴充套件後,對應的session資料就存至database=5資料庫了;