首页 > 其他分享 >tomcat session cluster的实现

tomcat session cluster的实现

时间:2023-10-08 09:47:42浏览次数:45  
标签:tomcat com server cluster session proxy tan root

#1.环境准备
	IP           主机名              服务
	10.0.0.150   proxy.tan.com      httpd
	10.0.0.160   tomcat1.tan.com    JDK8、tomcat8
	10.0.0.170   tomcat2.tan.com    JDK8、tomcat8

#2.在proxy主机配置httpd实现后端tomcat主机轮询
[root@proxy ~]#vim /etc/httpd/conf.d/tomcat.conf
<Proxy balancer://tomcat-server>
  BalancerMember http://tomcat1.tan.com:8080 loadfactor=1
  BalancerMember http://tomcat2.tan.com:8080 loadfactor=1
</Proxy>
<VirtualHost *:80>
  ServerName proxy.tan.com
  ProxyRequests Off
  ProxyVia On
  ProxyPreserveHost On
  ProxyPass / balancer://tomcat-server/
  ProxyPassReverse / balancer://tomcat-server/
</VirtualHost>

[root@proxy ~]#systemctl restart httpd

#或者使用nginx来实现后端轮询
	#在http语句块中添加upstream模块
    upstream tomcat-server{
        server tomcat1.tan.com:8080;
        server tomcat2.tan.com:8080;
        }
        
    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  proxy.tan.com;  #修改此行
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        proxy_pass http://tomcat-server;#添加此行
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
[root@proxy ~]#systemctl start nginx

#编写hosts解析
[root@proxy ~]#cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
10.0.0.160 tomcat1.tan.com
10.0.0.170 tomcat2.tan.com

#3.在后端tomcat1主机上安装tomcat并修改conf/server.xml
#安装脚本请见上一此作业
[root@tomcat1 ~]#bash tomcat8.5.sh
[root@tomcat1 ~]#vim /usr/local/tomcat/conf/server.xml
...#略
 <Engine name="Catalina" defaultHost="tomcat1.tan.com">
...#略
      <Host name="tomcat1.tan.com"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
 <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="230.188.188.188"  #配置多播地址
 port="45564"
 frequency="500"
 dropTime="3000"/>
 <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
 address="10.0.0.160"  #配置本机ip
 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>

      </Host>
...#略

[root@tomcat1 ~]#cat /usr/local/tomcat/webapps/ROOT/index.jsp
<%@ page import="java.util.*" %>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>tomcat test</title>
</head>
<body>
<div>On <%=request.getServerName() %></div>
<div><%=request.getLocalAddr() + ":" + request.getLocalPort() %></div>
<div>SessionID = <span style="color:blue"><%=session.getId() %></span></div>
<%=new Date()%>
</body>
</html>

[root@tomcat1 ~]#systemctl restart tomcat


#4.在后端tomcat2主机上安装tomcat并修改conf/server.xml
#安装脚本请见上一此作业
[root@tomcat2 ~]#bash tomcat8.5.sh
[root@tomcat2 ~]#vim /usr/local/tomcat/conf/server.xml
...#略
 <Engine name="Catalina" defaultHost="tomcat2.tan.com">
...#略
      <Host name="tomcat2.tan.com"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
 <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="230.188.188.188"
 port="45564"
 frequency="500"
 dropTime="3000"/>
 <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
 address="10.0.0.170"
 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>

      </Host>
...#略
[root@tomcat2 ~]#cat /usr/local/tomcat/webapps/ROOT/index.jsp
<%@ page import="java.util.*" %>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>tomcat test</title>
</head>
<body>
<div>On <%=request.getServerName() %></div>
<div><%=request.getLocalAddr() + ":" + request.getLocalPort() %></div>
<div>SessionID = <span style="color:blue"><%=session.getId() %></span></div>
<%=new Date()%>
</body>
</html>

[root@tomcat2 ~]#systemctl restart tomcat


#5.修改应用的web.xml文件开启该应用程序的分布式
 #修改tomcat1主机的应用的web.xml文件
 [root@tomcat1 ~]#tail -n3 /usr/local/tomcat/webapps/ROOT/WEB-INF/web.xml
  </description>
<distributable/> #添加此行
</web-app>
[root@tomcat1 ~]#systemctl restart tomcat


#修改tomcat2主机的应用的web.xml文件
 [root@tomcat2 ~]#tail -n3 /usr/local/tomcat/webapps/ROOT/WEB-INF/web.xml
  </description>
<distributable/> #添加此行
</web-app>
[root@tomcat2 ~]#systemctl restart tomcat

#6.访问测试,此时主机轮询但是session不会变
[root@proxy ~]#curl -I proxy.tan.com/index.jsp                                            HTTP/1.1 200
Server: nginx/1.14.1
Date: Mon, 29 Aug 2022 05:05:28 GMT
Content-Type: text/html;charset=ISO-8859-1
Connection: keep-alive
Set-Cookie: JSESSIONID=06CEDA54AEFBBE67465BC4307874000D; Path=/; HttpOnly

[root@proxy ~]#curl -b "JSESSIONID=06CEDA54AEFBBE67465BC4307874000D" proxy.tan.com/index.jsp

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>tomcat test</title>
</head>
<body>
<div>On tomcat-server</div>
<div>10.0.0.170:8080</div>
<div>SessionID = <span style="color:blue">06CEDA54AEFBBE67465BC4307874000D</span></div>
Mon Aug 29 13:05:48 CST 2022
</body>
</html>
[root@proxy ~]#curl -b "JSESSIONID=06CEDA54AEFBBE67465BC4307874000D" proxy.tan.com/index.jsp

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>tomcat test</title>
</head>
<body>
<div>On tomcat-server</div>
<div>10.0.0.160:8080</div>
<div>SessionID = <span style="color:blue">06CEDA54AEFBBE67465BC4307874000D</span></div>
Mon Aug 29 13:05:52 CST 2022
</body>
</html>

标签:tomcat,com,server,cluster,session,proxy,tan,root
From: https://www.cnblogs.com/tanll/p/17748104.html

相关文章

  • 编写脚本实现tomcat一键安装8.5版本
    #下载包到/root目录[root@tomcat~]#lljdk-8u212-linux-x64.tar.gzapache-tomcat-8.5.82.tar.gz-rw-r--r--1rootroot10610025Aug2717:14apache-tomcat-8.5.82.tar.gz-rw-r--r--1rootroot195013152Aug2717:08jdk-8u212-linux-x64.tar.gz#编写脚本[root@to......
  • nginx实现后端tomcat的负载均衡调度
    1.负载均衡主机和网络地址规划10.0.0.152proxy.magedu.orgnginx10.0.0.150t1.magedu.orgtomcat110.0.0.160t2.magedu.orgtomcat2#只需在10.0.0.52的nginx主机上实现域名解析[root@localhost~]#cat/etc/hosts127.0.0.1localhost......
  • 总结tomcat优化方法
     ##1、概述Tomcat的运行依赖于JVM,从虚拟机的角度把Tomcat的调整分为外部环境调优和Tomcat自身调优两部分##2、外部环境JVM调优Tomcat首先跑在JVM之上的,因为它的启动其实也只是一个java命令行,首先我们需要对这个JAVA的启动命令行进行调优。帮助:manjava......
  • 基于redis5的redis cluster部署
     1.创建rediscluster集群的环境准备1.1.#每个redis节点采用相同的相同的redis版本、相同的密码、硬件配置;所有redis服务器必须没有任何数据;准备六台主机,地址如下:10.0.0.15010.0.0.16010.0.0.17010.0.0.18010.0.0.19010.0.0.2002.启用redis......
  • sessionStorage的setItem和getItem使用
    一、vue文件使用sessionStorage:(简单存值取值)1.存储数据:sessionStorage.setItem('取得k的名字','要存储的值')vuesessionStorage.setItem('loadClaim','this.node')2.获取数据:sessionStorage.getItem('取得k的名字')vuesessionStorage.getItem......
  • ES 关于 remote_cluster 的一记小坑
    最近有小伙伴找到我们说Kibana上添加不了RemoteCluster,填完信息点Save直接跳回原界面了。具体页面,就和没添加前一样。我们和小伙伴虽然隔着网线但还是进行了深入、详细的交流,梳理出来了如下信息:两个集群:集群A和集群B,版本都是7.10.0;集群A没区分节点角色;集群B设置了......
  • ES 关于 remote_cluster 的一记小坑
    最近有小伙伴找到我们说Kibana上添加不了RemoteCluster,填完信息点Save直接跳回原界面了。具体页面,就和没添加前一样。我们和小伙伴虽然隔着网线但还是进行了深入、详细的交流,梳理出来了如下信息:两个集群:集群A和集群B,版本都是7.10.0;集群A没区分节点角色;集群B......
  • tomcat/logs几种清空catalina.out日志方式
    查看文件大小[root@hblogs]#du-hcatalina.out重定向清空文件[root@hblogs]#>catalina.out重定向true命令清空文件[root@hblogs]#true>catalina.out使用cat/cp/dd命令及/dev/null来清空文件[root@hblogs]#cat/dev/null>catalina.out[root@hblogs]#cp/......
  • SqlSession与SqlSessionFactory
    2.2.3SqlSessionSqlSession是MyBatis框架中极其重要的接口。SqlSession类似于JDBC中的Connection,它代表MyBatis和数据库的一次会话,主要用于执行持久化操作。SqlSession对象底层封装了JDBC连接,所以可以直接使用SqlSession对象执行已映射的SQL语句。SqlSession中包含了所有执行SQL......
  • 06 配置IDEA中的maven和tomcat
    一开始按照狂神的教程一步步来出错了,改了很久还是不行,最后按照系统默认的就可以了。尤其是看到他的有一个.iml文件(我没有),还以为是那里的原因。最后还是搞定了。50分钟的视频,1.5倍速,花了两个钟,我测你的健康码......