当前位置: 首页 > 知识库问答 >
问题:

如何隔离Spring Boot应用程序redis和Spring Boot会话全局redis

从渊
2023-03-14

据我所知,spring启动和spring会话为我们提供了一站式自动配置,但当我的应用程序使用会话redis和应用程序缓存redis时,它们不是同一个redis服务器;我如何配置它,非常感谢您的回复!

共有2个答案

夔高寒
2023-03-14

标记默认的RedisConnectionFactory@主

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

并标记会话RedisConnectionFactory@SpringSessionRedisConnectionFactory

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

redisConnectionFactory()配置RedisConnection工厂。例如:

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;
    }
朱阳曜
2023-03-14

事实上,默认情况下,sping-ses和sping-cache实际上都是由sping-boot使用名为ConnectionFactoryRedisConnectionFactorybean配置的。有两种方法可以做到这一点。

>

  • 使spring会话使用不同的connectionFactorybean实例,并让spring缓存使用默认的 connectionFactory。下面是一个示例解决方案:

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

    使sping-cache使用不同的ConnectionFactorybean实例,并留下sping-ses以使用默认的ConnectionFactory。下面是一个示例解决方案:

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

  •  类似资料:
    • 我有一个带post请求的控制器。我试图用一个简单的NotNull注释验证POJO。我正在使用ControllerAdvice来处理异常。 所以我尝试使用它,但当我启动应用程序时,我得到了以下信息: 因此,我想为BindException创建自己的处理程序,但当我为BindException类创建ExceptionHandler时,spring应用程序不会启动。如果我注释掉handleBindExc

    • 我有Kafka Streams java应用程序启动并运行。我试图使用KSQL创建简单的查询,并使用Kafka流来实现复杂的解决方案。我希望将KSQL和Kafka流作为Java应用程序运行。 我打算通过https://github.com/confluentinc/ksql/blob/master/ksqldb-examples/src/main/java/io/confluent/ksql/em

    • 要获取请求URL,可以在堆栈溢出中找到以下方法。 第一种方法: 第二种方法: 第三种方法: 我不知道在spring boot应用程序中使用哪一个来获取请求URL。 如果我使用第三种方法,那么我是否需要在配置类中创建RequestContextListener的bean,如下所示?

    • 问题内容: 我正在忙于建立一个包含3个不同子域的平台-example.com,auth.example.com和api.example.com。它们与在服务器的不同端口上运行的3个单独的NodeJS应用程序一起运行。 这是设置会话的代码: 这三个应用程序的配置完全相同,并且它们都位于同一台服务器上。由于某些原因,会话未共享。我似乎记得几周前就已经共享了它们,现在一切都坏了- 我偷偷地怀疑,当我们将

    • 完成干净的构建后,我将war文件复制到Tomcat的文件夹中。但是部署会发生两次,并且在上下文已经存在的情况下以异常结束。我错过了什么? 非常感谢您的帮助。