20180417 session共享的验证:
使用目前在做的cmp项目来做验证,搭建两个cmp进程,通过nginx实现负载均衡。前台访问连到cmp1,手动停掉cmp1,前台继续访问,检查session是否有效,是否是同一个session等等。

1.环境准备

nginx的下载:http://nginx.org/en/download.html
nginx的安装:windows版本:http://nginx.org/en/docs/windows.html
nginx的文档:http://nginx.org/en/docs/

【MARK】nginx启动会什么会有两个进程?
一个是master进程,为主进程,用于控制nginx各个进程之间的协调工作关系 另一个是worker进程,用于处理用户的请求
【MARK】nginx系统学习网址 http://blog.sina.com.cn/s/blog_6d579ff40100y5p7.html
【MARK】nginx负载均衡模式
nginx 负载均衡时,一台tomcat宕机时的问题 自动切换 https://blog.csdn.net/lxb15959168136/article/details/53113996
配置Nginx实现负载均衡 https://blog.csdn.net/daybreak1209/article/details/51554045
nginx共享session方式及redis+cookie、ip_hash方式实现 https://blog.csdn.net/chao_1990/article/details/54405759
Nginx报504 gateway timeout错误的解决方法 https://blog.csdn.net/luozi007/article/details/76152933
Nginx负载均衡的详细配置及使用案例详解 https://www.cnblogs.com/wang-meng/p/5861174.html

nginx:http://localhost:8083/
cmp1:http://10.45.82.76:8088/control/
cmp2:http://localhost:8088/control/

nginx的配置conf文件关键配置:

upstream cmpServer {  
    server localhost:8088;  
    server 10.45.82.76:8088;  
    } 
    server {
        listen       8083;
        server_name  localhost:8088;

        location ^~ /control {
            proxy_pass http://cmpServer/control;
        }        
    }

按照上面的配置,访问http://localhost:8083/control 的请求,将会被以轮训方式分发到cmp1和cmp2.

2.验证

访问http://localhost:8083/control,通过先停掉cmp1,再启动cmp1,再停掉cmp2的方式来验证,业务操作OK,redis里看到的始终是同一个session.

"spring:session:iotcmp-cc:expirations:1523972640000"
"spring:session:iotcmp-cc:sessions:7b9f85b7-c93f-4cc2-8f0d-6b2c3857ed78"
"spring:session:iotcmp-cc:sessions:expires:7b9f85b7-c93f-4cc2-8f0d-6b2c3857ed78"

前台请求信息:

General
Request URL: http://localhost:8083/control/iotcmp/v1/common/prodstate?_=1523968491595
Request Method: GET
Status Code: 200 
Remote Address: 127.0.0.1:8083
Referrer Policy: no-referrer-when-downgrade

Response Headers
Connection: keep-alive
Content-Type: application/json;charset=UTF-8
Date: Tue, 17 Apr 2018 13:12:10 GMT
Server: nginx/1.13.12
Transfer-Encoding: chunked
X-Application-Context: application:8088

Request Headers
Accept: application/json, text/javascript, */*; q=0.01
Accept-Encoding: gzip, deflate, br
Accept-Language: en,zh-CN;q=0.9,zh;q=0.8
Connection: keep-alive
Cookie: SESSION=7b9f85b7-c93f-4cc2-8f0d-6b2c3857ed78; JSESSIONID=F8C1A2BAEBECB36BA55315F1AA88209B; ZSMART_LOCALE=en; custId=45003; userId=1; roleId=1
Host: localhost:8083
Referer: http://localhost:8083/control/index.html
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36
X-Requested-With: XMLHttpRequest

3.遇到的问题
前台请求转到cmp1,停掉cmp1之后,前台请求的时候会出现“Provisional headers are shown”



这个rest接口差不多1分钟才返回结果,需要检查日志和代码查看为何这么慢。等1分钟返回结果之后页面展示正常。
关于这个问题可以参考:
provisional headers are shown 原因分析 https://blog.csdn.net/wangjun5159/article/details/46912803

4.题外话
HTTP协议本身是无状态的,客户端向服务器发送的每次请求都是独立的。
JSESSIONID是tomcat对sessionId的叫法
上面请求URL:Request URL: http://localhost:8083/control/iotcmp/v1/common/prodstate?_=1523968491595, 应该是使用了URL重写,将sessionId附加在URL的后面

results matching ""

    No results matching ""