如何隔离spring boot app redis和spring boot session global redis

时间:2017-12-25 03:32:37

标签: spring-boot redis spring-session

据我所知,spring boot和spring session为我们提供了一站式autoconfig,但是当我的应用程序使用会话redis和app cache redis不是同一个redis服务器时; 我该怎么配置它,非常感谢你的回复!

2 个答案:

答案 0 :(得分:2)

实际上,默认情况下,spring-session和spring-cache实际上都是使用名为RedisConnectionFactory的{​​{1}} bean通过spring-boot配置的。 有两种方法可以做到这一点。

  1. 使connectionFactory使用不同的spring-session bean实例,并保留spring-cache以使用默认的connectionFactory。以下是一个示例解决方案:

    connectionFactory
  2. 使@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; } } 使用不同的spring-cache bean实例,并让spring-session使用默认的connectionFactory。以下是一个示例解决方案:

    connectionFactory

答案 1 :(得分:0)

标记默认RedisConnectionFactory @Primary

    @Bean
    @Primary
    public RedisConnectionFactory redisConnectionFactory(RedisProperties properties) {
        return redisConnectionFactory(redisProperties);
    }

并标记会话RedisConnectionFactory @SpringSessionRedisConnectionFactory

    @Bean
    @SpringSessionRedisConnectionFactory
    public RedisConnectionFactory springSessionRedisConnectionFactory() {
        return redisConnectionFactory(...);
    }

redisConnectionFactory()配置RedisConnectionFactory。 例如:

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;
    }