简单拓补图
一、负载均衡、反向代理
7-1nginx代理服务器配置
[root@dlnginx ~]#yum install epel-release.noarch -y ###安装额外源
[root@dlnginx ~]#yum install nginx -y
[root@dlnginx ~]#systemctl start nginx.service
[root@dlnginx ~]#systemctl status nginx.service
[root@dlnginx ~]#vim /etc/nginx/nginx.conf ###主配置文件
upstream tomcat {
server 192.168.10.101:8080;
server 192.168.10.102:8080;
}
location ~* \.jsp$ {
proxy_pass http://tomcat;
}
7-2和7-3同时进行配置
[root@Tomcat1 ~]#cd /data/
上传
[root@Tomcat1 data]#ls
apache-tomcat-9.0.16.tar(1).gz jdk-8u291-linux-x64.tar(1).gz
[root@Tomcat1 data]#tar xf jdk-8u291-linux-x64.tar\(1\).gz -C /usr/local/
[root@Tomcat1 data]#ls /usr/local/
bin etc games include jdk1.8.0_291 lib lib64 libexec sbin share src
[root@Tomcat1 data]#cd /usr/local/
[root@Tomcat1 local]#ls
bin etc games include jdk1.8.0_291 lib lib64 libexec sbin share src
[root@Tomcat1 local]#ln -s jdk1.8.0_291/ jdk
[root@Tomcat1 local]#ls
bin etc games include jdk jdk1.8.0_291 lib lib64 libexec sbin share src
[root@Tomcat1 local]#vim /etc/profile.d/env.sh
export JAVA_HOME=/usr/local/jdk
export PATH=$JAVA_HOME/bin:$PATH
export JRE_HOME=$JAVA_HOME/jre
export CLASSPATH=$JAVA_HOME/lib/:$JRE_HOME/lib/
[root@Tomcat1 local]#. /etc/profile.d/env.sh
[root@Tomcat1 local]#java -version
[root@Tomcat1 data]#tar xf apache-tomcat-9.0.16.tar\(1\).gz -C /usr/local/
[root@Tomcat1 data]#cd /usr/local/
[root@Tomcat1 local]#ls
apache-tomcat-9.0.16 bin etc games include jdk jdk1.8.0_291 lib lib64 libexec sbin share src
[root@Tomcat1 local]#ln -s apache-tomcat-9.0.16/ tomcat
[root@Tomcat1 local]#ls
apache-tomcat-9.0.16 bin etc games include jdk jdk1.8.0_291 lib lib64 libexec sbin share src tomcat
[root@Tomcat1 local]#useradd -s /sbin/nologin tomcat
[root@Tomcat1 local]#chown tomcat:tomcat tomcat/ -R
[root@Tomcat2 local]#cat > /usr/lib/systemd/system/tomcat.service <<EOF
> [Unit]
> Description=Tomcat
> After=syslog.target network.target
>
> [Service]
> Type=forking
> ExecStart=/usr/local/tomcat/bin/startup.sh
> ExecStop=/usr/local/tomcat/bin/shutdown.sh
> RestartSec=3
> PrivateTmp=true
> User=tomcat
> Group=tomcat
>
> [Install]
> WantedBy=multi-user.target
>
> EOF
[root@Tomcat2 local]#systemctl daemon-reload
[root@Tomcat2 local]#systemctl start tomcat.service
[root@Tomcat2 local]#ss -ntap |grep 8080
LISTEN 0 100 :::8080 :::* users:(("java",pid=2767,fd=54))
[root@Tomcat2 local]#systemctl status tomcat.service
进入主页面进行配置
检测:
去访问代理服务器会发现,每次调度到192.168.246.8和192.168.246.9上面,sessionID都会改变,我们要让他保持会话连接(sessionID不会变)
客户端访问代理服务器,第一次访问被调度到tomcat1服务器,由于是第一次访问,客户的cookie缓存中不会有sessionid,服务器会生成sessionid 1给 c1客户,c1客户会把sessionid 记录下来
客户端第二次访问代理服务器,由于调度器的原因,不能保证一直去访问tomcat1,如果被调度到tomcat2, tomcat2服务器是没有sessionid 1, 它会认为我要生成新的sessionid 给c1客户,c1客户会更新自己的sessionid变成sessionid2
1.当客户端去访问代理服务器;代理服务器调度给Tomcat1;如果是第一次访问:客户端cookie里面没有sessionID,Tomcat1服务器就给给客户端生成一个sessionID并且发给客户端;客户端会将sessionID存在cookie中;当客户端第二次去访问Tomcat1时,会带着Tomcat1的sessionID,第二次,Tomcat1就不会给客户端生成新的sessionID
2.由于代理服务器调度的原因;不会每次都调度给Tomcat1;当调度给Tomcat2时;cookie带的是sessionID1;Tomcat2上要用sessionID2,他就会生成一个新的ID给客户端
3.所以,来回调度的时候,每次都是新的sessionID
二、会话保持
方法一:
检验: 访问代理服务器
session id固定了,但是服务器也固定了不调度了
方法二:
Apache Tomcat 9 (9.0.87) - Clustering/Session Replication How-To
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
channelSendOptions="8">
<Manager className="org.apache.catalina.ha.session.DeltaManager"
expireSessionsOnShutdown="false"
notifyListenersOnReplication="true"/>
<Channel className="org.apache.catalina.tribes.group.GroupChannel">
<Membership className="org.apache.catalina.tribes.membership.McastService"
address="228.0.0.4"
port="45564"
frequency="500"
dropTime="3000"/>
<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
address="auto"
port="4000"
autoBind="100"
selectorTimeout="5000"
maxThreads="6"/>
<Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
<Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
</Sender>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatchInterceptor"/>
</Channel>
<Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
filter=""/>
<Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>
<Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"
tempDir="/tmp/war-temp/"
deployDir="/tmp/war-deploy/"
watchDir="/tmp/war-listen/"
watchEnabled="false"/>
<ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
</Cluster>
把官网中的这个页面内容加入到主配置文件中,把前面写入nginx的ip_hash给删除了,不影响方法二
tomcat2进行配置
在子配置文件中加入下面的阴影部分
7-3 Tomcat2进行配置
和tomcat1是一样的配置方法
检测:访问代理服务器192.168.246.7/index.jsp
不管调度7-2还是7-3,sessionID都保持一致,都不变化了。