How to isolate spring boot app redis and spring boot session global redis

ぃ、小莉子 提交于 2021-02-16 15:28:05

问题


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.

  1. make spring-session to use a different connectionFactory bean instance, and leave spring-cache to use the default connectionFactory. 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;
        }
    }
    
  2. make spring-cache to use a different connectionFactory bean instance, and leave spring-session to use the default connectionFactory. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!