问题
As I know, spring boot and spring session provided us one-stop autoconfig, but when my app use session redis and app cache redis not same redis server; How can I config it, thank you very much for your reply!
回答1:
Indeed, by default, both spring-session and spring-cache are actually configured by spring-boot with a RedisConnectionFactory
bean named as connectionFactory
.
Two ways to do this.
make
spring-session
to use a differentconnectionFactory
bean instance, and leave spring-cache to use the defaultconnectionFactory
. And below is a sample solution:@Configuration public class RedisHttpSessionConfig { @Bean StringRedisSerializer stringRedisSerializer() { return new StringRedisSerializer(); } @Bean RedisConnectionFactory redisHttpSessionConnectionFactory() { RedisConnectionFactory redisHttpSessionConnectionFactory = null; // ... add your codes here return redisHttpSessionConnectionFactory; } @Bean public RedisTemplate<Object, Object> sessionRedisTemplate( RedisConnectionFactory redisHttpSessionConnectionFactory) { RedisTemplate<Object, Object> template = new RedisTemplate<Object, Object>(); template.setKeySerializer(new StringRedisSerializer()); template.setHashKeySerializer(new StringRedisSerializer()); template.setValueSerializer(new GenericJackson2JsonRedisSerializer()); template.setDefaultSerializer(GenericJackson2JsonRedisSerializer()); template.setConnectionFactory(redisHttpSessionConnectionFactory); return template; } }
make
spring-cache
to use a differentconnectionFactory
bean instance, and leave spring-session to use the defaultconnectionFactory
. And below is a sample solution:@Configuration public class RedisCacheConfig { @Bean StringRedisSerializer stringRedisSerializer() { return new StringRedisSerializer(); } @Bean RedisConnectionFactory redisCacheConnectionFactory() { RedisConnectionFactory redisCacheConnectionFactory = null; // ... add your codes here return redisCacheConnectionFactory; } @Bean RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisCacheConnectionFactory) { RedisTemplate<Object, Object> redisTemplate = new RedisTemplate(); redisTemplate.setConnectionFactory(redisCacheConnectionFactory); redisTemplate.setKeySerializer(this.stringRedisSerializer()); redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); return redisTemplate; } @Bean(name = "stringRedisTemplate") public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisCacheConnectionFactory) throws UnknownHostException { StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(); stringRedisTemplate.setConnectionFactory(redisCacheConnectionFactory); stringRedisTemplate.setKeySerializer(this.stringRedisSerializer()); stringRedisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); return stringRedisTemplate; } @Bean CacheManager cacheManager(RedisTemplate redisTemplate) { RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate); cacheManager.setDefaultExpiration(600l); cacheManager.setUsePrefix(true); return cacheManager; } }
回答2:
Mark the default RedisConnectionFactory @Primary
@Bean
@Primary
public RedisConnectionFactory redisConnectionFactory(RedisProperties properties) {
return redisConnectionFactory(redisProperties);
}
And mark the session RedisConnectionFactory @SpringSessionRedisConnectionFactory
@Bean
@SpringSessionRedisConnectionFactory
public RedisConnectionFactory springSessionRedisConnectionFactory() {
return redisConnectionFactory(...);
}
redisConnectionFactory() config RedisConnectionFactory. For example:
private static RedisConnectionFactory redisConnectionFactory(RedisProperties redisProperties, boolean afterPropertiesSet) {
RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
redisStandaloneConfiguration.setHostName(redisProperties.getHost());
redisStandaloneConfiguration.setPassword(RedisPassword.of(redisProperties.getPassword()));
redisStandaloneConfiguration.setDatabase(redisProperties.getDatabase());
redisStandaloneConfiguration.setPort(redisProperties.getPort());
GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig();
genericObjectPoolConfig.setMaxIdle(redisProperties.getLettuce().getPool().getMaxIdle());
genericObjectPoolConfig.setMinIdle(redisProperties.getLettuce().getPool().getMinIdle());
genericObjectPoolConfig.setMaxTotal(redisProperties.getLettuce().getPool().getMaxActive());
genericObjectPoolConfig.setMaxWaitMillis(redisProperties.getLettuce().getPool().getMaxWait().toMillis());
genericObjectPoolConfig.setTestOnBorrow(true);
genericObjectPoolConfig.setTestOnReturn(true);
LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory(redisStandaloneConfiguration,
LettucePoolingClientConfiguration.builder().commandTimeout(redisProperties.getTimeout()).poolConfig(genericObjectPoolConfig).build());
if (afterPropertiesSet) {
lettuceConnectionFactory.afterPropertiesSet();
}
return lettuceConnectionFactory;
}
来源:https://stackoverflow.com/questions/47965495/how-to-isolate-spring-boot-app-redis-and-spring-boot-session-global-redis