在談到集羣方案的時候,第一個會遇到的問題就是session問題,在單機上,session的問題歷來都是web容器解決的,咱們主要是用,可是集羣意味着多容器。若是負載均衡是隨機分配服務器訪問的話,很容易形成在A服務器登陸後,下次訪問是走的是B服務器,結果B服務器的web容器裏面並無該用戶的session,結果就悲劇了。那麼怎麼辦呢,固然是redis來處理,redis把session集中存儲起來,無論哪臺服務器存取session都是走redis,本地服務器不保存session,這個問題就完美的解決了。這個方案落到具體的實現上,首先我想到的就是spring本身的解決方案,spring session。java
spring session+redis的方案很是的簡單,你們請按步驟來:git
步驟1:pom文件加startergithub
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-data-redis</artifactId> </dependency>
spring-boot-starter-web是引入web依賴,spring-boot-starter-data-redis是redis的存取,spring-session-data-redis就是把redis做爲session的存儲位置並作相關操做的依賴。web
步驟2:把redis的配置寫到application.properties裏面去redis
# REDIS # Redis數據庫索引(默認爲0) spring.redis.database=0 # Redis服務器地址 spring.redis.host=myip # Redis服務器鏈接端口 spring.redis.port=6379 # Redis服務器鏈接密碼(默認爲空) spring.redis.password=password # 鏈接池最大鏈接數(使用負值表示沒有限制) 默認 8 spring.redis.lettuce.pool.max-active=8 # 鏈接池最大阻塞等待時間(使用負值表示沒有限制) 默認 -1 spring.redis.lettuce.pool.max-wait=-1 # 鏈接池中的最大空閒鏈接 默認 8 spring.redis.lettuce.pool.max-idle=8 # 鏈接池中的最小空閒鏈接 默認 0 spring.redis.lettuce.pool.min-idle=0
步驟3:把相關注解加到啓動類裏面去spring
@SpringBootApplication @EnableCaching @EnableRedisHttpSession(maxInactiveIntervalInSeconds = 86400*30) public class RedisApplication { public static void main(String[] args) { SpringApplication.run(RedisApplication.class, args); } }
其中@EnableCaching表示開啓緩存,由於咱們在application.proerties文件裏面配置了redis,因此默認redis就做爲項目的緩存;@EnableRedisHttpSession表示redis存儲httpsession,後面session就會自動存儲到redis裏面了。數據庫
咱們開一個controller,試一下是否是這樣:express
@RestController public class IndexController { @RequestMapping("/saveSession") String saveSession(HttpSession session) { session.setAttribute("mySession", "lalala"); return session.getId(); } @RequestMapping("/getSession") String getSession(HttpSession session) { String mySessionString = session.getAttribute("mySession").toString(); return mySessionString; }
saveSession和getSession的方法很簡單,一個存session,一個取session緩存
咱們用redis-cli查看下這個session是否存到了redis:服務器
可見,session自動就存儲到了redis。可見,實現session到redis,而後共享,很是的簡單。
這一塊的源碼能夠看這裏
上面用redis-cli查看session的時候,能夠看到的確session存進去了,但這個存儲的方式卻不是那麼明瞭,能夠拿來講道說道。
首先是存儲的key值,好比上面截圖中的,是這麼一段:
spring:session:sessions:54abb3f7-909a-46c8-ab4c-1b515eff69b1
其中spring:session是spring session在redis裏面的命名空間,默認就是「spring:session",在org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration的源代碼裏面能夠看到:
這個namespace是能夠改的,在@EnableRedisHttpSession的源代碼裏面咱們能夠看到,有這麼幾個參數是能夠傳進去配置的
public @interface EnableRedisHttpSession { /** * The session timeout in seconds. By default, it is set to 1800 seconds (30 minutes). * This should be a non-negative integer. * @return the seconds a session can be inactive before expiring */ int maxInactiveIntervalInSeconds() default MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS; /** * Defines a unique namespace for keys. The value is used to isolate sessions by * changing the prefix from default {@code spring:session:} to * {@code <redisNamespace>:}. * <p> * For example, if you had an application named "Application A" that needed to keep * the sessions isolated from "Application B" you could set two different values for * the applications and they could function within the same Redis instance. * @return the unique namespace for keys */ String redisNamespace() default RedisOperationsSessionRepository.DEFAULT_NAMESPACE; /** * Flush mode for the Redis sessions. The default is {@code ON_SAVE} which only * updates the backing Redis when {@link SessionRepository#save(Session)} is invoked. * In a web environment this happens just before the HTTP response is committed. * <p> * Setting the value to {@code IMMEDIATE} will ensure that the any updates to the * Session are immediately written to the Redis instance. * @return the {@link RedisFlushMode} to use * @since 1.1 */ RedisFlushMode redisFlushMode() default RedisFlushMode.ON_SAVE; /** * The cron expression for expired session cleanup job. By default runs every minute. * @return the session cleanup cron expression * @since 2.0.0 */ String cleanupCron() default RedisHttpSessionConfiguration.DEFAULT_CLEANUP_CRON; }
咱們在@EnableRedisHttpSession配置一個namespace看下效果
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 86400*30,redisNamespace = "wphmoon:session") public class RedisApplication { ...... }
訪問上面的saveSession方法,看下session在redis裏面的數據結構:
上圖能夠看到命名空間已經修改了。咱們再來看下key後面的value是什麼樣的
失敗了,緣由是session存到redis並非用字符串類型來存,它存儲的格式是
用的是hash,咱們用hget來看一下
查不到,緣由是mySession並非完整的field name,完整的是這樣的
看到熟悉的lalala就知道此次終於查到了,完整的fieldName要在咱們命名的Attribute前面加上sessionAttr。可是在lalala前面的」\xac\xed\x00\x05t\x00\x06「又是什麼鬼?
這個就要靠翻源代碼了,因而我開始到spring-session-data-redis-XXX.jar裏面去找,看到了SpringSessionRedisOperations這個類,這個名字一看就象是把session推到redis的操做類(它自己是個註解,代碼裏面做者很貼心的告訴咱們具體實現去看哪些)。
在RedisOperationsSessionRepository類裏面,我意外的發現了這個
原來sessionAttr:是在這裏定義的,我還發現了這個
原來namespace要加上sessions是在這裏,但咱們此次翻代碼的主要緣由,查看value內容裏面的亂字符串卻不在這裏,在另一個類ReactiveRedisOperationsSessionRepository裏面。它實際操做session存儲的方法是調用另一個接口類:
這個接口的實現類最終只有兩個,仍是繼承關係。
看到RedisTemplate總算看到了老朋友,咱們使用redis的時候最經常使用到的工具。看下它操做hash的方法
從參數的名字就能看出來,這個確定通過了序列化(serialization)處理,因此進到RedisSerializationContext裏面能夠看到這一句
看來,全部session存入redis裏面的時候,須要作序列化的處理,而真正字符串前面的那一堆,就是序列化的標記內容。
這一章就講到這,下一章,咱們還要繼續泡在spring session和redis裏面,把cleanupCron(還記得在哪裏出現過嗎)相關的事再嘮嘮。