zoukankan      html  css  js  c++  java
  • 多机部署之session共享解决方案

    1. 常见描述

    为了解决多机部署中的session共享问题( 场景: A服务器登录,请求负载打到B服务器会报未登录),拟采用nginx中的ip_hash,根据ip做负载,同一ip请求到同一服务器上,还有一个原因是因为页面用到多线程,也需要保持一个ip客户端请求到同一服务器端,但是因为是通过网关转发过来的,ip_hash获取到的客户端是网关的导致改方案失效。

    2. 解决方案

    2.1 测试接口

    真实测试可用,服务器端留了测试的接口,测试连接的是那台服务器。

     /**
     * 软件老王,测试
     **/
     @ResponseBody
     @GetMapping(value = "/laowang/test")
      public String tt(){
            String ip = null;
            try {
                ip = InetAddress.getLocalHost().getHostAddress();
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }
            return "ip:"+ ip +",端口号:"+port;
        }
    

    2.2 nginx配置

    http {
        include       mime.types;
        default_type  application/octet-stream;
        #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
        #                  '$status $body_bytes_sent "$http_referer" '
        #                  '"$http_user_agent" "$http_x_forwarded_for"';
        #access_log  logs/access.log  main;
        sendfile        on;
        #tcp_nopush     on;
        #keepalive_timeout  0;
        keepalive_timeout  65;
        map $http_x_forwarded_for  $clientRealIp {
            ""      $remote_addr;
            ~^(?P<firstAddr>[0-9.]+),?.*$  $firstAddr;
        }
        upstream ruanjianlaowang {
              #ip_hash;
              hash $clientRealIp;
              server 10.192.168.10:9091;
              server 10.192.168.11:9092;
            }
        server {
           listen      9090;
           server_name  localhost;
           proxy_set_header Host $host;
           proxy_set_header X-Real-IP $remote_addr;
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
           location / {
                    proxy_pass http://ruanjianlaowang;
              }
           }
      }
    

    重点说明:

    (1)ngin负载均衡配置,监控9090端口

        server {
           listen      9090;
           server_name  localhost;
           proxy_set_header Host $host;
           proxy_set_header X-Real-IP $remote_addr;
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
           location / {
                    proxy_pass http://ruanjianlaowang;
              }
           }
    

    说明:原信息替换为:proxy_pass http://ruanjianlaowang;

    (2)两台服务器进行负载

        upstream ruanjianlaowang {
              #ip_hash;
              hash $clientRealIp;
              server 10.192.168.10:9091;
              server 10.192.168.11:9092;
            }
    

    说明:upstream ruanjianlaowang,其中ruanjianlaowang会在location中引用。

    (3)采用ip_hash负载方式

    因为要通过网关域名进行访问,所以要修改ip_hash,获取真实ip。

        map $http_x_forwarded_for  $clientRealIp {
            ""      $remote_addr;
            ~^(?P<firstAddr>[0-9.]+),?.*$  $firstAddr;
        }
        upstream ruanjianlaowang {
       		 #ip_hash;
        		hash $clientRealIp;
        }
    

    说明:http_x_forwarded_for是nginx自带的变量,里面包含原始代理原始ip,存放方式:ip1,ip2,ip3---,其中ip1为最原始,ip2为第一层代理,以此类推,map $http_x_forwarded_for $clientRealIp是将真实ip赋值给变量clientRealIp


    I’m 「软件老王」,如果觉得还可以的话,关注下呗,后续更新秒知!欢迎讨论区、同名公众号留言交流!

  • 相关阅读:
    error: Microsoft Visual C++ 14.0 is required.
    pip安装其他包报错
    MapReduce
    机器学习算法使用
    结巴分词使用实例
    大数据——hbase
    机房收费系统系列一:运行时错误‘-2147217843(80040e4d)’;用户‘sa’登陆失败
    耿建玲视频总结
    学生信息管理系统系列三:验收时的改进
    学生信息管理系统系列二:常见问题
  • 原文地址:https://www.cnblogs.com/ruanjianlaowang/p/12097889.html
Copyright © 2011-2022 走看看