集群环境下session共享解决方案

概述

  在搭建完集群环境后,不得不考虑的一个问题就是用户访问产生的session如何处理。比如集群中存在A,B两台服务器,用户在第一次访问网站时,nginx通过其负载均衡机制将用户请求转发到A服务器中,这是A服务器就是给用户创建一个Session。当用户第二次发送请求时,nginx将其负载均衡到B服务器中,而这时B服务器并不存在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_hash配置

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

    

扫描二维码关注公众号,回复: 9099251 查看本文章

  步骤二:编写一个servlet测试  

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

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

    现在启动的端口有8080和8081

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

      

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

       

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

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

      

   步骤四:配置nginx.conf文件

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

  步骤五:启动nginx,并访问

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

      

     控制台输出结果

      

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

      

       控制台访问结果

      

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

      

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

利用spring-session+Redis

猜你喜欢

转载自www.cnblogs.com/wnwn/p/12298515.html