1、集群情況下session會產生什么原因?
由於session存放在服務器端,集群下用戶可能訪問不同的服務器,則可能session無法共享。
2、Session共享解決方案
1)NGINX做的負載均衡可以綁定ip_hash,從而使同一個IP訪問同一個服務器 ------------------該方案使得集群失去意義。
2)利用數據庫同步session----------------------太過復雜
3)利用cookie同步session(保存一個session到本地,再次訪問將其帶到服務器端)----------------------安全性差、http請求都需要帶參數增加了帶寬消耗
4)使用session集群,存放到redis中(spring-session)
3、spring-session項目,解決session共享問題
<!--spring boot 與redis應用基本環境配置 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> </dependency> <!--spring session 與redis應用基本環境配置,需要開啟redis后才可以使用,不然啟動Spring boot會報錯 --> <dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-data-redis</artifactId> </dependency>
創建SessionConfig
import org.springframework.beans.factory.annotation.Value; 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; //這個類用配置redis服務器的連接 //maxInactiveIntervalInSeconds為SpringSession的過期時間(單位:秒) @EnableRedisHttpSession(maxInactiveIntervalInSeconds = 1800) Public class SessionConfig { // 冒號后的值為沒有配置文件時,制動裝載的默認值 @Value("${redis.hostname:localhost}") String HostName; @Value("${redis.port:6379}") int Port; @Bean Public JedisConnectionFactory connectionFactory() { JedisConnectionFactory connection = new JedisConnectionFactory(); connection.setPort(Port); connection.setHostName(HostName); return connection; } }
初始化Session:
//初始化Session配置 Public class SessionInitializer extends AbstractHttpSessionApplicationInitializer{ Public SessionInitializer() { super(SessionConfig.class); } }
控制層代碼
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class SessionController { @Value("${server.port}") private String PORT; @RequestMapping("/index") public String index() { return "index:" + PORT; } /** * @methodDesc: 功能描述:(往session存放值) */ @RequestMapping("/setSession") public String setSession(HttpServletRequest request, String sessionKey, String sessionValue) { HttpSession session = request.getSession(true); session.setAttribute(sessionKey, sessionValue); return "success,port:" + PORT; } /** * @methodDesc: 功能描述:(從Session獲取值) */ @RequestMapping("/getSession") public String getSession(HttpServletRequest request, String sessionKey) { HttpSession session =null; try { session = request.getSession(false); } catch (Exception e) { e.printStackTrace(); } String value=null; if(session!=null){ value = (String) session.getAttribute(sessionKey); } return "sessionValue:" + value + ",port:" + PORT; } }
六、高並發解決方案
業務數據庫 -》 數據水平分割(分區分表分庫)、讀寫分離
業務應用 -》 邏輯代碼優化(算法優化)、公共數據緩存
應用服務器 -》 反向靜態代理、配置優化、負載均衡(apache分發,多tomcat實例)
系統環境 -》 JVM調優
頁面優化 -》 減少頁面連接數、頁面尺寸瘦身
動態資源和靜態資源分離
CDN加速
服務分布式部署