1.Spring session簡介:
說白了就是解決session共享問題,spring-session將web的session存到一個指定的位置,集群的所有web服務(wù)器在操作session的時候就去這個位置找。(我比較喜歡用最土的語言解釋,不官方但是容易理解)
參考項目:https://github.com/bigbeef/cppba-web
開源地址:https://github.com/bigbeef
個人博客:http://blog.cppba.com
注意:本文只教你怎么使用spring-session-redis,不會深入探討為什么這么實現(xiàn),因為暫時我也沒弄清他的源源碼,但是照下面的做法你一定能跑起來。
2.maven在pom.xml中配置
<!--spring-session-->
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
<version>1.2.1.RELEASE</version>
</dependency>
3.創(chuàng)建RedisHttpSessionConfig
package com.cppba.config;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
@EnableRedisHttpSession
public class RedisHttpSessionConfig {
@Bean
public JedisConnectionFactory connectionFactory() {
JedisConnectionFactory connection = new JedisConnectionFactory();
connection.setHostName("127.0.0.1");
connection.setPort(6379);
return connection;
}
}
4.創(chuàng)建SpringSessionInitializer
package com.cppba.config;
import org.springframework.session.web.context.AbstractHttpSessionApplicationInitializer;
public class SpringSessionInitializer extends AbstractHttpSessionApplicationInitializer {
}
接下來,把項目跑起來,在瀏覽器上訪問一下,我們再在redis的控制臺輸入keys *,如果你能看到像下圖的結(jié)果,恭喜你,配置成功!

https://github.com/bigbeef