Session共享解决方案:
1.nginx或者haproxy做的负载均衡,用nginx做的负载均衡可以添加ip_hash这个配置;用haproxy做的负载均衡可以用balance source这个配置,从而使用一个IP的请求发到同一个服务器;
2.利用数据库同步session;
3.利用cookie同步session数据,但是安全性差,http请求都需要带参增加了带宽消耗;
4.Tomcat配置session共享;
5利用session集群存放Redis;
一、Nginx通过负载均衡IP地址固定绑定,解决Session共享
1、目录展示
2、导入依赖
3、SessionIPServlet
package com.zn.servlet; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet("/SessionIPServlet") public class SessionIPServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("当前请求端口:"+request.getLocalPort()); String action=request.getParameter("action"); //向Session中存放一个数据 if(action.equals("setSession")){ request.getSession().setAttribute("username","zhangsan"); }else if(action.equals("getSession")){ response.getWriter().write((String)request.getSession().getAttribute("username")); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } }
4、没有Nginx的访问效果展示
分别访问8080和8081
5、修改Nginx的nginx.conf文件
6、启动Nginx,并访问
访问:http://www.znzn.com/SessionIPServlet?action=setSession
获取:http://www.znzn.com/SessionIPServlet?action=getSession
二、利用spring-session+Redis实现session共享
1、目录展示
2、成功连接redis
3、导入依赖
<dependency> <!-- spring-boot-starter-web是为我们提供了包括mvc,aop等需要的一些jar --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!-- 因为我们已经配置了 parent 中的version 所以这里不需要指定version了 --> </dependency> <!--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>
4、MyController
package com.zn.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
@RestController
public class MyController {
//存放Session值
@RequestMapping("/setSession")
public String setSession(HttpServletRequest request){
request.getSession().setAttribute("username","zhangsan");
return "success";
}
//获取Session值
@RequestMapping("/getSession")
public String getSession(HttpServletRequest request){
return (String)request.getSession().getAttribute("username");
}
}
5、resource文件下application.yml
6、App启动文件
package com.zn;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Hello world!
*
*/
@SpringBootApplication
public class App
{
public static void main( String[] args )
{
SpringApplication.run(App.class,args);
}
}
7、启动Tomcat8080进行存放和获取数据
存入数据是已将数据存放到redis缓存中
8、改变端口号并重启