集群情况下Session共享解决方案
集群情况下session会产生什么原因?
因为Session存放在JVM内存中,集群的话多个JVM不会共享
Session共享解决方案
1.nginx或者haproxy做的负载均衡)
用Nginx 做的负载均衡可以添加ip_hash这个配置,
用haproxy做的负载均衡可以用 balance source这个配置。
从而使同一个ip的请求发到同一台服务器。(这个ip是你的电脑或者手机或者pad的ip)
2.利用数据库同步session
3.利用cookie同步session数据原理图如下
缺点:安全性差、http请求都需要带参数增加了带宽消耗
4.使用spring-session+redis解决
引入maven依赖
<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);
}
}
配置文件
server.port=8080
spring.redis.database=0
spring.redis.host=192.168.110.185
spring.redis.port=6379
spring.redis.password=123456
spring.redis.pool.max-idle=8
spring.redis.pool.min-idle=0
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1
spring.redis.timeout=5000
redis.hostname=192.168.110.185
redis.port=6379
redis.password=123456
控制器层代码
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;
public static void main(String[] args) {
SpringApplication.run(SessionController.class, args);
}
@RequestMapping("/index")
public String index() {
return "index:" + PORT;
}
/**
*
* @methodDesc: 功能描述:(往session存放值)
* @author: 余胜军
* @param: @param
* httpSession
* @param: @param
* sessionKey
* @param: @param
* sessionValue
* @param: @return
* @createTime:2017年10月8日 下午3:55:26
* @returnType:@param httpSession
* @returnType:@param sessionKey
* @returnType:@param sessionValue
* @returnType:@return String
* @copyright:上海每特教育科技有限公司
* @QQ:644064779
*/
@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获取值)
* @author: 余胜军
* @param: @param
* httpSession
* @param: @param
* sessionKey
* @param: @return
* @createTime:2017年10月8日 下午3:55:47
* @returnType:@param httpSession
* @returnType:@param sessionKey
* @returnType:@return String
* @copyright:上海每特教育科技有限公司
* @QQ:644064779
*/
@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;
}
}