nginx+redis+tomcat session绑定

需求:
  公司后端两台tomcat做负载,前端nginx接收用户请求;为了使无论用户请求到后端哪台设备其session都能保持一致,需要做session绑定;
所需软件:
tomcat8、redis、nginx
用到的插件:
https://github.com/ran-jit/tomcat-cluster-redis-session-manager
部署:
1、下载安装tomcat
2、部署redis
3、解压tomcat-cluster-redis-session-manager插件包
3.1、将插件解压包中的lib目录下的所有jar包copy到tomcat的lib目录下
cp /root/tomcat-cluster-redis-session-manager/lib/* /usr/local/tomcat/lib/
3.2、将插件解压包目录中conf目录下的redis-data-cache.properties配置文件copy到tomcat的conf目录下
cp /root/tomcat-cluster-redis-session-manager/conf/* /usr/local/tomcat/conf/
3.3、配置redis-data-cache.properties

redis.hosts=r-2aaaaaaaaaaaaaa.redis.rds.aliyuncs.com:6379  # 指定redis地址
redis.password=AAAAAAAAA    # 指定redis密码
redis.cluster.enabled=false  # 关闭集群模式,如果是redis集群则需要开启
redis.sentinel.enabled=false
redis.sentinel.master=mymaster
redis.database=11  # 指定redis库
lb.sticky-session.enabled=false
session.persistent.policies=DEFAULT
redis.sso.timeout=0

4、配置测试页面
4.1、在tomcat的webapps目录下创建test目录
mkdir -pv /usr/local/tomcat/webapps/test
4.2、在test目录下定义测试页面
vim index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>首页redis-session</title>
</head>
<body>
    <div>tomcat 集群测试</div>
    <div>
        <%
          System.out.println(session.getId());
          out.println("<br> SESSION ID:" + session.getId()+"<br>");
        %>
        %    </div>
        %    </body>
        %    </html>

5、将配置好的tomcat复制一份到另一台设备
6、配置nginx

upstream backstage {
   server 192.168.4.23:8080 max_fails=1 fail_timeout=10s;
   server 192.168.4.24:8081 max_fails=1 fail_timeout=10s;
}

server {
    listen      80;
    listen 443 ssl;
     server_name  test.123.com.cn;
    ssl_certificate   cert/test.123.com.cn.pem;
    ssl_certificate_key  cert/test.123.com.cn.key;
    ssl_session_timeout 5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers HIGH:!ADH:!MD5;
    ssl_prefer_server_ciphers on;

    if ($server_port = 80) {
            rewrite ^/Admin/(.*)$ https://$server_name/Admin/$1 permanent;
       }

    if ($request_uri = /) {
            return http://$server_name/Admin/;
                }

    location / {
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Scheme https;
            proxy_set_header Via "nginx";
            client_max_body_size     10m;
            client_body_buffer_size  256k;
            proxy_connect_timeout    600;
            proxy_send_timeout       600;
            proxy_read_timeout       600;
            proxy_buffer_size        32k;
            proxy_buffers            4 64k;
            proxy_busy_buffers_size  128k;
            proxy_temp_file_write_size 512k;
            proxy_pass               http://backstage;
    }
    access_log /var/log/nginx/access.log main;
    error_log /var/log/nginx/error.log;
}

7、验证测试

无论怎么刷新,其session都是不变的

查看redis中存储的session,跟web页面中展示的session是一致的;

查看日志也是轮询的策略,但是无论请求到哪台后端tomcat其session都是不变的

 总结:

  所有客户端的session信息都将存储在redis中,而不是保存在tomcat自己内部,即使后端某台tomcat挂了,其session信息也不会丢失,同时两台tomcat的session信息也保持了共享;

posted @ 2022-03-24 17:52  潇湘神剑  阅读(151)  评论(0编辑  收藏  举报