集群情况下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;
	}

}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Java全栈研发大联盟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值