Session共享问题有哪些解决方案?

1、使用模拟spring-session+ redis【可靠】

2、使用token重写session【可靠】

3、使用cookie,不安全

4、使用nginx负载均衡策略,ip_hash绑定,不存在session共享问题

5、使用数据库同步session,对数据库有压力

6、tomcat配置session共享

 

利用cookie同步session数据原理图如下

Session共享问题有哪些解决方案?_第1张图片

缺点:安全性差、http请求都需要带参数增加了带宽消耗

 

使用spring-session把session存放在Redis

1、创建一个springboot项目

2、引入maven依赖


        
            org.springframework.boot
            spring-boot-starter-redis
        
        
        
            org.springframework.session
            spring-session-data-redis
        

3、创建SessionConfig.java

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;
        @Value("${redis.password}")
        String password;

	@Bean
	public JedisConnectionFactory connectionFactory() {
		JedisConnectionFactory connection = new JedisConnectionFactory();
		connection.setPort(Port);
		connection.setHostName(HostName);
                connection.setPassword(password);
		return connection;
	}
}

 

4、初始化Session

//初始化Session配置
public class SessionInitializer extends AbstractHttpSessionApplicationInitializer{
    public SessionInitializer() {
        super(SessionConfig.class);
    }
}

 

5、控制器层代码

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;
	}

	@RequestMapping("/setSession")
	public String setSession(HttpServletRequest request, String sessionKey, String sessionValue) {
		HttpSession session = request.getSession(true);
		session.setAttribute(sessionKey, sessionValue);
		return "success,port:" + PORT;
	}

	@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;
	}

}

 

6、配置application.properties

########################################################
###Redis (RedisConfiguration)
########################################################
spring.redis.database=0
spring.redis.host=10.37.129.3
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=10.37.129.3
redis.port=6379
redis.password=123456

#path
server.context-path=/

 

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(分布式)