CentOS 7 nginx+tomcat9 session处理方案之session复制

我们的目标是所有服务器上都要保持用户的Session,那么将每个应用服务器中的Session信息复制到其它服务器节点上是不是就可以呢?

这就是Session的第二中处理办法:会话复制

 

192.168.94.11(nginx)

192.168.94.22(tomcat1)

192.168.94.33(tomcat2)

关闭SElinux和防火墙

# 每台服务器都一样
[root@nginx ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.94.66   nginx
192.168.94.22   tomcat1
192.168.94.33   tomcat2
[root@nginx ~]# scp /etc/hosts 192.168.94.22:/etc/hosts
The authenticity of host '192.168.94.22 (192.168.94.22)' can't be established.
ECDSA key fingerprint is SHA256:LztMUHktwCLql3Iw5/phDFbJoKLLO/BBNUVNV7DcAcU.
ECDSA key fingerprint is MD5:f7:4e:9a:f2:22:bc:c5:62:d2:a9:29:b0:bf:4b:bb:7c.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.94.22' (ECDSA) to the list of known hosts.
root@192.168.94.22's password: 
hosts                                                     100%  228   211.3KB/s   00:00    
[root@nginx ~]# scp /etc/hosts 192.168.94.33:/etc/hosts
The authenticity of host '192.168.94.33 (192.168.94.33)' can't be established.
ECDSA key fingerprint is SHA256:LztMUHktwCLql3Iw5/phDFbJoKLLO/BBNUVNV7DcAcU.
ECDSA key fingerprint is MD5:f7:4e:9a:f2:22:bc:c5:62:d2:a9:29:b0:bf:4b:bb:7c.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.94.33' (ECDSA) to the list of known hosts.
root@192.168.94.33's password: 
hosts                                                     100%  228   391.0KB/s   00:00    

安装nginx

[root@nginx ~]# wget http://nginx.org/packages/centos/7/x86_64/RPMS/nginx-1.14.0-1.el7_4.ngx.x86_64.rpm
[root@nginx ~]# yum -y install pcre-devel zlib-devel openssl-devel
[root@nginx ~]# rpm -i nginx-1.14.0-1.el7_4.ngx.x86_64.rpm
[root@nginx ~]# vim /etc/nginx/nginx.conf    # 修改配置文件 添加以下内容

    upstream tomcat {
        server 192.168.94.22:8080;
        server 192.168.94.33:8080;
                    
    }   

    server {
        listen 80; 
            server_name 192.168.94.11:80;                                              
                location / { 
                   proxy_pass http://tomcat;
                   proxy_set_header X-Real-IP $remote_addr;
                                        
                }   

CentOS 7 配置Tomcat  有Tomcat的安装方法

配置tomcat

修改tomcat配置文件

[root@tomcat1 ~]# cp /usr/local/tomcat/conf/server.xml{,.bak}
[root@tomcat1 ~]# vim /usr/local/tomcat/conf/server.xml
将<Engine name="Catalina" defaultHost="localhost">修改为
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">   #tomcat2的配置修改成 jvmRoute="tomcat2"<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>的注释去掉
[root@tomcat1 ~]# vim /usr/local/tomcat/webapps/ROOT/WEB-INF/web.xml 
在</web-app>的上面添加<distributable/>

编写测试页面

[root@tomcat1 ~]# vim /usr/local/tomcat/webapps/ROOT/s.jsp
Session ID:<%= session.getId() %><BR>          # 显示session ID                                       
SessionPort:<%= request.getServerPort() %>   # 显示端口
<% out.println("This tomcat server 192.168.94.22");%>    # IP自己修改

[root@tomcat2 ~]# vim /usr/local/tomcat/webapps/ROOT/s.jsp
Session ID:<%= session.getId() %><BR>                                                        
SessionPort:<%= request.getServerPort() %>
<% out.println("This tomcat server 192.168.94.33");%>
[root@nginx ~]# systemctl start nginx

[root@tomcat1 ~]# /usr/local/tomcat/bin/startup.sh 
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/local/jdk10
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.

[root@tomcat2 ~]# /usr/local/tomcat/bin/startup.sh 
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/local/jdk10
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.

测试

浏览器访问测试 http://192.168.94.22:8080/s.jsp 刷新后 session ID 未发生变化 

浏览器访问测试 http://192.168.94.33:8080/s.jsp 刷新后 session ID 未发生变化 

浏览器访问测试 http://192.168.94.66/s.jsp 刷新后 session ID 未发生变化  但两个tomcat在轮询

 

 

 

会话复制在Tomcat上得到了支持,它是基于IP组播(multicast)来完成Session的复制,Tomcat的会话复制分为两种:

全局会话复制:利用Delta Manager复制会话中的变更信息到集群中的所有其他节点

非全局复制:使用Backup Manager进行复制,它会把Session复制给一个指定的备份节点

会话复制不适合大的集群 , 不推荐生产使用 , 否则会导致 Session 复制时性能代价过高

posted @ 2018-08-01 17:46  damowang~  阅读(784)  评论(0编辑  收藏  举报