20180409 最近在做的一个项目,需要支持分布式部署,第一次意识到session共享问题,于是查看了下公司portal的处理方式,发现是用的Spring Session来实现的,遂查询了下Spring Session,记录下第一次使用spring session的过程。
按照官网提供的Quick Start(https://docs.spring.io/spring-session/docs/current/reference/html5/guides/java-redis.html
使用的话还是比较简单的,主要是以下几个步骤:

1.maven引入依赖

<dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-data-redis</artifactId>
            <version>1.3.0.RELEASE</version>
</dependency>

2.添加一个配置文件,表示支开启Redis Http Session

@Configuration
@EnableRedisHttpSession(redisNamespace = "iotcmp-cc")
public class CcSessionConfiguration {
}

3.配置Redis,使用Spring Data Redis,官网也提供了参考文档:https://docs.spring.io/spring-data/data-redis/docs/2.0.4.RELEASE/reference/html/
例如我们项目中的配置:

@Configuration
@AutoConfigureAfter(RedisCacheAutoConfiguration.class)
@ConfigurationProperties(prefix = CommonZCacheConfiguration.PREFIX)
@EnableCaching
// 用于开启Spring Cache支持
public class CommonZCacheConfiguration {

    protected static final String PREFIX = "common.cache";

    @Bean("ccRedisClientProperties")
    @ConfigurationProperties(prefix = PREFIX)
    public RedisClientProperties redisClientProperties() {
        return new RedisClientProperties();
    }

    @Bean("ccRedisConnectionFactory")
    @ConditionalOnProperty(prefix = PREFIX, value = "redis-enabled", matchIfMissing = true)
    public RedisConnectionFactory redisConnectionFactory(@Qualifier("ccRedisClientProperties") RedisClientProperties config) {
        return RedisConnectionFactoryBuilder.build(config);
    }

    @Bean("ccRedisTemplate")
    @ConditionalOnBean(name = "ccRedisConnectionFactory")
    public <K, V> RedisTemplate<K, V> redisTemplate(
        @Qualifier("ccRedisConnectionFactory") RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<K, V> redisTemplate = new RedisTemplate<K, V>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        redisTemplate.setKeySerializer(new CacheKeyRedisSerializer());
        redisTemplate.setHashKeySerializer(new CacheKeyRedisSerializer());
        redisTemplate.setValueSerializer(new CacheValueRedisSerializer());
        redisTemplate.setHashValueSerializer(new CacheValueRedisSerializer());
        return redisTemplate;
    }

    @Primary
    @Bean("ccCacheManager")
    @ConditionalOnBean(name = "ccRedisTemplate")
    public CacheManager cacheManager(@Qualifier("ccRedisTemplate") RedisTemplate<?, ?> redisTemplate) {
        RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
        cacheManager.setUsePrefix(true);
        cacheManager.setTransactionAware(true);

        ConcurrentHashMap<String, Long> map = new ConcurrentHashMap<String, Long>();

        map.put(CommonCacheDef.CACHE_OFFER_DTO_BY_ID, 0L);
        map.put(CommonCacheDef.CACHE_ACCT_DTO_BY_ID, 0L);
        map.put(CommonCacheDef.CACHE_PROD_STATE_DTO_BY_ID, 0L);

        cacheManager.setExpires(map);
        cacheManager.setCacheNames(map.keySet());

        cacheManager.afterPropertiesSet();
        return cacheManager;
    }
}

4.启动程序,访问登录之后查看redis,redis中已经保存了我们的session信息

"spring:session:iotcmp-cc:sessions:expires:53cb02aa-9f5f-4cf9-812b-c2db72ed4fb1"
"spring:session:iotcmp-cc:expirations:1523257920000"
"spring:session:iotcmp-cc:sessions:53cb02aa-9f5f-4cf9-812b-c2db72ed4fb1"

观察发现,每次创建http session都会生成上面三个key。为什么需要三个key?

results matching ""

    No results matching ""