在后端的的tomcat之上通过组播的方式实现session的共享

实验架构图:

Tomcat实现session会话保持(二)_第1张图片

实验设备

操作系统:CentOS7.6
nginx IP:192.168.8.134 
tomcat1 IP:192.168.8.160
tomcat2 IP:192.168.8.161

环境准备

#清空防火墙规则
iptables -F
iptables -X

#临时设置关闭selinux
setenforce 0

#安装jdk,centos7的源默认最高支持jdk1.8
yum -y install java-1.8.0-openjdk-devel 

#更改主机名解析
vim /etc/hosts
192.168.8.161 tomcat2
192.168.8.160 tomcat1

实验部署步骤:

配置Nginx

安装并配置Nginx反向代理

[root@nginx ~]#yum -y instll nginx

修改nginx配置文件

[root@nginx ~]#vim /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    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  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    include /etc/nginx/conf.d/*.conf;
}
upstream web {
        server 192.168.8.160:8080;
        server 192.168.8.161:8080;
}
server {
        listen 80 default_server;
        index index.jsp;
        location / {
                proxy_pass http://web/;
        }
}

检查无误后启动nginx

[root@nginx ~]#nginx -t
[root@nginx ~]#systemctl start nginx

配置tomcat1

安装tomcat以及对应的其他管理工具

[root@tomcat1 ~]#yum -y install tomcat tomcat-lib tomcat-admin-webapps tomcat-webapps tomcat-docs-webapp

创建必要的文件夹

[root@tomcat1 ~]#mkdir -pv /data/app/ROOT/{WEB-INF,META-INF,classes,lib}

编辑网页文件

[root@tomcat1 ~]#vim /data/app/ROOT/index.jsp
<%@ page language="java" %>
        
                TomcatA
                
                        

TomcatA

table align="centre" border="1"> Session ID <% session.setAttribute("www.zd.com","www.zd.com"); %> <%= session.getId() %> Created on <%= session.getCreationTime() %>

编辑tomcat配置文件

[root@tomcat1 ~]#vim /etc/tomcat/server.xml 

        

          

          
            
            

            
              
            
            
            
          

          
          

          

          
          
        
        
        

        
        
      

修改tomcat-users.xml配置文件

[root@tomcat1 ~]#vim /etc/tomcat/tomcat-users.xml




拷贝web.xml文件

[root@tomcat1 ~]#cp /etc/tomcat/web.xml /data/app/ROOT/WEB-INF/web.xml 
[root@tomcat1 ~]#vim /data/app/ROOT/WEB-INF/web.xml
#添加这一行

启动tomcat服务

[root@tomcat1 ~]#systemctl restart tomcat
[root@tomcat1 ~]#systemctl enable tomcat

配置tomcat2

创建必要的目录

[root@tomcat2 ~]#mkdir /data

从tomcat1上拷贝相关文件

[root@tomcat1 ~]#scp -r /data/app/ 192.168.8.161:/data/ 
[root@tomcat1 ~]#scp /etc/tomcat/server.xml 192.168.8.161:/etc/tomcat/server.xml 
[root@tomcat1 ~]#scp /etc/tomcat/tomcat-users.xml 192.168.8.161:/etc/tomcat/tomcat-users.xml 

编辑网页文件

[root@tomcat2 ~]#vim /data/app/ROOT/index.jsp
<%@ page language="java" %>
        
                TomcatB
                
                        

TomcatB

table align="centre" border="1"> Session ID <% session.setAttribute("www.test.com","www.test.com"); %> <%= session.getId() %> Created on <%= session.getCreationTime() %>

更改tomcat配置文件

[root@tomcat2 ~]#vim /etc/tomcat/server.xml 

        

          

          
            
            

            
              
            
            
            
          

          
          

          

          
          
        
        
        

        
        
      

启动tomcat服务

[root@tomcat2 ~]#systemctl restart tomcat
[root@tomcat2 ~]#systemctl enable tomcat

测试tomcat1与tomcat2的8080端口是否启动

tomcat1
Tomcat实现session会话保持(二)_第2张图片
tomcat2
Tomcat实现session会话保持(二)_第3张图片

用Nginx代理进行测试

#刷新网页后session信息依然保持不变
Tomcat实现session会话保持(二)_第4张图片
Tomcat实现session会话保持(二)_第5张图片