gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 11:12:53 32 4
gpt4 key购买 nike

据我所知,spring boot 和 spring session 为我们提供了一站式自动配置,但是当我的应用程序使用 session redis 和应用程序缓存 redis 时,不是同一个 redis 服务器;怎么设置呢,非常感谢您的回复!

最佳答案

事实上,默认情况下,spring-session 和 spring-cache 实际上都由 spring-boot 配置了一个名为 connectionFactoryRedisConnectionFactory bean。有两种方法可以做到这一点。

  1. 使 spring-session 使用不同的 connectionFactory bean 实例,并让 spring-cache 使用默认的 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;
    }
    }
  2. spring-cache 使用不同的 connectionFactory bean 实例,让 spring-session 使用默认的 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;
    }
    }

关于spring-boot - 如何隔离spring boot app redis和spring boot session global redis,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47965495/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com