1. 程式人生 > >便宜有便宜的辦法-小微企業雲上的springboot叢集方案2:session和redis

便宜有便宜的辦法-小微企業雲上的springboot叢集方案2:session和redis

在談到叢集方案的時候,第一個會遇到的問題就是session問題,在單機上,session的問題從來都是web容器解決的,我們主要是用,但是叢集意味著多容器。如果負載均衡是隨機分配伺服器訪問的話,很容易造成在A伺服器登入後,下次訪問是走的是B伺服器,結果B伺服器的web容器裡面並沒有該使用者的session,結果就悲劇了。那麼怎麼辦呢,當然是redis來處理,redis把session集中儲存起來,不管哪臺伺服器存取session都是走redis,本地伺服器不儲存session,這個問題就完美的解決了。這個方案落到具體的實現上,首先我想到的就是spring自己的解決方案,spring session。

1、spring session+redis跑起來

spring session+redis的方案非常的簡單,大家請按步驟來:

步驟1:pom檔案加starter

        <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的儲存位置並做相關操作的依賴。

步驟2:把redis的配置寫到application.properties裡面去

# 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:把相關注解加到啟動類裡面去

@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,試一下是不是這樣:

@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,然後共享,非常的簡單。

這一塊的原始碼可以看這裡

2、session在redis的儲存結構

上面用redis-cli檢視session的時候,可以看到的確session存進去了,但這個儲存的方式卻不是那麼明瞭,可以拿來說道說道。

2.1、namespace和其他屬性

首先是儲存的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;

}
  • maxInactiveIntervalInSeconds是session中的資料的過期時間(不是session在redis中的過期時間)
  • redisNamespace就是session在spring session在redis裡面key的名稱空間
  • redisFlushMode是redis儲存session的方式,預設 ON_SAVE。有兩種方式:?IMMEDIATE:一旦建立session的時候就立即儲存;ON_SAVE:建立session的時候不會儲存,但當往session中新增資料的時候就會儲存
  • cleanupCron session過期時的資料清掃定時任務,預設的配置時1分鐘1次,為什麼要配置這個,後面會講。

我們在@EnableRedisHttpSession配置一個namespace看下效果

@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 86400*30,redisNamespace = "wphmoon:session")
public class RedisApplication {
......
}

訪問上面的saveSession方法,看下session在redis裡面的資料結構:

2.2 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(還記得在哪裡出現過嗎)相