Session共享解决方案 使用nginx做的负载均衡添加一个ip_hash配置 利用spring-session+Redis

  步骤一:创建一个工程,启动两个Tomcat

    Session共享解决方案
使用nginx做的负载均衡添加一个ip_hash配置
利用spring-session+RedisSession共享解决方案
使用nginx做的负载均衡添加一个ip_hash配置
利用spring-session+Redis

  步骤二:编写一个servlet测试  

Session共享解决方案
使用nginx做的负载均衡添加一个ip_hash配置
利用spring-session+Redis
Session共享解决方案
使用nginx做的负载均衡添加一个ip_hash配置
利用spring-session+Redis
@WebServlet("/nginxSessionServlet")
public class NginxSessionServlet  extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       doGet(req,resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("当前使用端口:"+req.getLocalPort());
        String action = req.getParameter("action");
        if (action.equals("setSession")){
            req.getSession().setAttribute("username","wnwn");
            resp.getWriter().write("success");
        }else if (action.equals("getSession")){
            resp.getWriter().write((String)req.getSession().getAttribute("username"));
        }
    }
}
Session共享解决方案
使用nginx做的负载均衡添加一个ip_hash配置
利用spring-session+Redis
Session共享解决方案
使用nginx做的负载均衡添加一个ip_hash配置
利用spring-session+Redis

   步骤三:在没有使用nginx访问的效果如下:

    现在启动的端口有8080和8081

    1.使用http://localhost:8080/nginxSessionServlet?action=setSession地址访问(端口为8080,但是是setSession存入数据)

      Session共享解决方案
使用nginx做的负载均衡添加一个ip_hash配置
利用spring-session+Redis

    2.使用http://localhost:8080/nginxSessionServlet?action=getSession地址访问(端口为8080,但是是getSession取出数据)

       Session共享解决方案
使用nginx做的负载均衡添加一个ip_hash配置
利用spring-session+Redis

    3.使用http://localhost:8081/nginxSessionServlet?action=getSession地址访问(端口为8081, 方法是getSession取出数据)

      在8081端口中使用getSession取出8080中set的数据时不可以的,所以下面我们要解决session共享问题

      Session共享解决方案
使用nginx做的负载均衡添加一个ip_hash配置
利用spring-session+Redis

   步骤四:配置nginx.conf文件

Session共享解决方案
使用nginx做的负载均衡添加一个ip_hash配置
利用spring-session+Redis
Session共享解决方案
使用nginx做的负载均衡添加一个ip_hash配置
利用spring-session+Redis
  upstream myserver{
         ip_hash;
         server 127.0.0.1:8080;
         server 127.0.0.1:8081;
    }
    server{
        listen       81;
        server_name  www.bproject.com;
        location / {
            root   html;
            proxy_pass  http://myserver;
            index  index.html index.htm;
        }
    }
Session共享解决方案
使用nginx做的负载均衡添加一个ip_hash配置
利用spring-session+Redis
Session共享解决方案
使用nginx做的负载均衡添加一个ip_hash配置
利用spring-session+Redis

  步骤五:启动nginx,并访问

    1.使用http://www.bproject.com:81/nginxSessionServlet?action=setSession地址访问

      Session共享解决方案
使用nginx做的负载均衡添加一个ip_hash配置
利用spring-session+Redis

     控制台输出结果

      Session共享解决方案
使用nginx做的负载均衡添加一个ip_hash配置
利用spring-session+Redis

    2.使用http://www.bproject.com:81/nginxSessionServlet?action=getSession地址访问

      Session共享解决方案
使用nginx做的负载均衡添加一个ip_hash配置
利用spring-session+Redis

       控制台访问结果

      Session共享解决方案
使用nginx做的负载均衡添加一个ip_hash配置
利用spring-session+Redis

    3.使用http://www.bproject.com:81/nginxSessionServlet?action=getSession地址再次访问页面

      Session共享解决方案
使用nginx做的负载均衡添加一个ip_hash配置
利用spring-session+Redis

    4.结论: 当第一次请求时,负载均衡将请求转发到8080端口上,因为配置了ip_hash,所以每次请求都会转发到8080端口上,相当于把请求和8080端口粘到一块了。

利用spring-session+Redis

   步骤一:创建一个springboot工程,启动两次,端口分别为8082和8083

    Session共享解决方案
使用nginx做的负载均衡添加一个ip_hash配置
利用spring-session+Redis

  步骤二:导入依赖

Session共享解决方案
使用nginx做的负载均衡添加一个ip_hash配置
利用spring-session+Redis
Session共享解决方案
使用nginx做的负载均衡添加一个ip_hash配置
利用spring-session+Redis
        <!--spring boot 与redis应用基本环境配置 -->
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-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>
Session共享解决方案
使用nginx做的负载均衡添加一个ip_hash配置
利用spring-session+Redis
Session共享解决方案
使用nginx做的负载均衡添加一个ip_hash配置
利用spring-session+Redis

  步骤三:创建controller测试

Session共享解决方案
使用nginx做的负载均衡添加一个ip_hash配置
利用spring-session+Redis
Session共享解决方案
使用nginx做的负载均衡添加一个ip_hash配置
利用spring-session+Redis
@RestController
public class SessionController {

    @RequestMapping("/setSession")
    public String setSession(HttpServletResponse response, HttpServletRequest request) throws IOException {
        request.getSession().setAttribute("username","wang");
        return "success";
    }

    @RequestMapping("/getSession")
    public String getSession(HttpServletRequest request,HttpServletResponse response){
        String username = (String) request.getSession().getAttribute("username");
        return username;
    }
}
Session共享解决方案
使用nginx做的负载均衡添加一个ip_hash配置
利用spring-session+Redis
Session共享解决方案
使用nginx做的负载均衡添加一个ip_hash配置
利用spring-session+Redis

  步骤四:application.properties文件

server.port=8082
#server.port=8083
#redis配置 spring.redis.password: wang2003

  步骤五:启动项目测试

   1.使用http://localhost:8082/setSession地址访问(端口为8082)

      Session共享解决方案
使用nginx做的负载均衡添加一个ip_hash配置
利用spring-session+Redis

      页面返回success时,就成功将数据添加到Redis缓存中了

      Session共享解决方案
使用nginx做的负载均衡添加一个ip_hash配置
利用spring-session+Redis