zoukankan      html  css  js  c++  java
  • Session共享解决方案

    使用nginx做的负载均衡添加一个ip_hash配置

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

        

      步骤二:编写一个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

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

        

      步骤二:导入依赖

    复制代码
    复制代码
            <!--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>
    复制代码
    复制代码

      步骤三:创建controller测试

    复制代码
    复制代码
    @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;
        }
    }
    复制代码
    复制代码

      步骤四:application.properties文件

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

      步骤五:启动项目测试

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

          

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

          

  • 相关阅读:
    CentOS安装JAVA后JAVA版本不对的问题
    AES加密时抛出 Illegal key size or default parameters
    Tomcat7 安装StartSSL证书笔记
    window无法启动mongodb服务:系统找不到指定的文件错误的解决方法
    springAop @AfterReturning注解 获取返回值
    springAop 使用@Around,@After等注解时,代码运行两边的问题
    htmlunit 导致高cup占用,一老内存溢出的解决办法
    spring activemq 整合
    springMVC整合Junit4进行单元测试
    socket,tcp,http三者之间的区别和原理
  • 原文地址:https://www.cnblogs.com/mayuan01/p/12391187.html
Copyright © 2011-2022 走看看