一般来说,在多个tomcat集群业务中,session会话共享是必须的需求,不然前端nginx转发过来的请求不知道之前请求在哪台tomcat节点上,从而就找不到session以至于最终导致请求失败。要实现tomcat session共享有多种方案,今天介绍下使用tomcat自带的cluster方式,在多个tomcat节点间自动实时复制session信息,配置起来很简单。tomcat自带的这种session共享方案,对于规模较小的tomcat集群来说够用了,但这个方案的效率比较低,在大并发下表现并不好。所以大规模的tomcat集群还是建议使用memcache或者redis来实现session共享。
1)基础环境
ip 主机名 应用 端口 192.168.10.200 Nginx-node nginx1.12.2 80 192.168.10.201 Tomcat-node1 java8.131、tomcat8.0.53 8080 192.168.10.202 Tomcat-node2 java8.131、tomcat8.0.53 8080 下面操作在三台机器上同样执行: [[email protected] ~]# cat /etc/redhat-release CentOS release 6.9 (Final) 为了方便测试,关闭iptables防火墙和selinux。如果是生产环境,开启iptables后,需要开放对应的应用端口。 [[email protected] ~]# setenforce 0 [[email protected] ~]# getenforce disabled [[email protected] ~]# cat /etc/sysconfig/selinux |grep "SELINUX=disabled" SELINUX=disabled [[email protected] ~]# /etc/init.d/iptables stop 本案例环境部署中所需的软件下载地址:https://pan.baidu.com/s/1732gmtRpVh1zY_i4H85Ecw 提取密码:ze9d 下载到服务器上的/usr/local/src目录下.另外:节点服务器的系统时间一定要保持一致!!
2)安装Nginx(在192.168.10.200机器上操作)
[[email protected] ~]# cd /usr/local/src/ [[email protected] src]# ll total 8920 -rw-rw-r--. 1 root root 981687 Oct 27 2017 nginx-1.12.2.tar.gz -rw-rw-r--. 1 root root 5453234 Aug 23 2018 openssl-1.1.0i.tar.gz -rw-rw-r--. 1 root root 2081413 Aug 23 2018 pcre-8.42.tar.gz -rw-rw-r--. 1 root root 607698 Jan 16 2017 zlib-1.2.11.tar.gz 安装依赖包 [[email protected] src]# yum -y install gcc gcc-c++ 安装pcre库 [[email protected] src]# tar -zvxf pcre-8.42.tar.gz [[email protected] src]# cd pcre-8.42 [[email protected] pcre-8.42]# ./configure && make && make install 安装zlib库 [[email protected] pcre-8.42]# cd /usr/local/src/ [[email protected] src]# tar -zvxf zlib-1.2.11.tar.gz [[email protected] src]# cd zlib-1.2.11 [[email protected] zlib-1.2.11]# ./configure && make && make install 安装openssl [[email protected] zlib-1.2.11]# cd /usr/local/src/ [[email protected] src]# tar -zvxf openssl-1.1.0i.tar.gz [[email protected] src]# cd openssl-1.1.0i [[email protected] openssl-1.1.0i]# ./config && make && make install 安装nginx,特别注意要指定prce zlib openssl原码包位置 [[email protected] openssl-1.1.0i]# cd /usr/local/src/ [[email protected] src]# tar -zvxf nginx-1.12.2.tar.gz [[email protected] src]# cd nginx-1.12.2 [[email protected] nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.42 --with-zlib=/usr/local/src/zlib-1.2.11 --with-openssl=/usr/local/src/openssl-1.1.0i [[email protected] nginx-1.12.2]# make && make install 安装成功后配置nginx [[email protected] nginx-1.12.2]# cd /usr/local/nginx/conf/ [[email protected] conf]# cp nginx.conf nginx.conf.bak [[email protected] conf]# cat nginx.conf #user nobody; worker_processes 8; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; worker_rlimit_nofile 65535; events { use epoll; worker_connections 65535; } http { include mime.types; default_type application/octet-stream; charset utf-8; ###### ## set access log format ###### log_format main ‘$http_x_forwarded_for $remote_addr $remote_user [$time_local] "$request" ‘ ‘$status $body_bytes_sent "$http_referer" ‘ ‘"$http_user_agent" "$http_cookie" $host $request_time‘; ####### ## http setting ####### sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; fastcgi_connect_timeout 30000; fastcgi_send_timeout 30000; fastcgi_read_timeout 30000; fastcgi_buffer_size 256k; fastcgi_buffers 8 256k; fastcgi_busy_buffers_size 256k; fastcgi_temp_file_write_size 256k; fastcgi_intercept_errors on; ##cache## client_header_timeout 60s; client_body_timeout 60s; client_max_body_size 10m; client_body_buffer_size 1m; proxy_connect_timeout 5; proxy_read_timeout 60; proxy_send_timeout 5; proxy_buffer_size 64k; proxy_buffers 4 128k; proxy_busy_buffers_size 128k; proxy_temp_file_write_size 1m; proxy_temp_path /home/temp_dir; proxy_cache_path /home/cache levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g; ##end## gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.1; gzip_comp_level 9; gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php; gzip_vary on; ## includes vhosts include vhosts/*.conf; } [[email protected] conf]# mkdir vhosts [[email protected] conf]# cd vhosts/ [[email protected] vhosts]# vim lb_tomcat.conf upstream tomcat-lb { server 192.168.10.201:8080; server 192.168.10.202:8080; } server { listen 80; server_name www.kevin.com; location / { proxy_pass http://tomcat-lb; proxy_set_header X-Real-IP $remote_addr; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location ~ .*\.(gif|jpg|png|htm|html|css|ico|flv|swf)(.*) { proxy_pass http://tomcat-lb; proxy_redirect off; proxy_set_header Host $host; proxy_cache cache_one; proxy_cache_valid 200 302 1h; proxy_cache_valid 301 1d; proxy_cache_valid any 10m; expires 30d; proxy_cache_key $host$uri$is_args$args; } } [[email protected] vhosts]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [[email protected] conf]# /usr/local/nginx/sbin/nginx [[email protected] conf]# lsof -i:80 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME nginx 25292 root 6u IPv4 19679665 0t0 TCP *:http (LISTEN) nginx 25293 nobody 6u IPv4 19679665 0t0 TCP *:http (LISTEN) nginx 25294 nobody 6u IPv4 19679665 0t0 TCP *:http (LISTEN) nginx 25295 nobody 6u IPv4 19679665 0t0 TCP *:http (LISTEN) nginx 25296 nobody 6u IPv4 19679665 0t0 TCP *:http (LISTEN) nginx 25297 nobody 6u IPv4 19679665 0t0 TCP *:http (LISTEN) nginx 25298 nobody 6u IPv4 19679665 0t0 TCP *:http (LISTEN) nginx 25299 nobody 6u IPv4 19679665 0t0 TCP *:http (LISTEN) nginx 25300 nobody 6u IPv4 19679665 0t0 TCP *:http (LISTEN) 将域名www.kevin.com解析到192.168.10.200上,访问http://www.kevin.com,发现访问请求结果会负载到192.168.10.201和192.168.10.202的tomcat上了。
3)安装tomcat(在192.168.10.201和192.168.10.202两台机器上操作)
安装java8环境。先卸载掉系统自带的java7,然后安装java8 [[email protected] ~]# java -version java version "1.7.0_131" OpenJDK Runtime Environment (rhel-2.6.9.0.el6_8-x86_64 u131-b00) OpenJDK 64-Bit Server VM (build 24.131-b00, mixed mode) [[email protected] ~]# yum -y remove java-1.7.0-openjdk* [[email protected] ~]# yum -y remove tzdata-java.noarch [[email protected] ~]# java -version -bash: /usr/bin/java: No such file or directory [[email protected] ~]# ll /usr/local/src/jdk-8u131-linux-x64_.rpm -rw-rw-r--. 1 root root 169983496 Nov 19 2017 /usr/local/src/jdk-8u131-linux-x64_.rpm [[email protected] ~]# rpm -ivh /usr/local/src/jdk-8u131-linux-x64_.rpm --force [[email protected] ~]# vim /etc/profile ...... JAVA_HOME=/usr/java/jdk1.8.0_131 JAVA_BIN=/usr/java/jdk1.8.0_131/bin PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/bin:/sbin/ CLASSPATH=.:/lib/dt.jar:/lib/tools.jar export JAVA_HOME JAVA_BIN PATH CLASSPATH [[email protected] ~]# source /etc/profile [[email protected] ~]# java -version java version "1.8.0_131" Java(TM) SE Runtime Environment (build 1.8.0_131-b11) Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode) You have new mail in /var/spool/mail/root 安装配置tomcat8 [[email protected] ~]# cd /usr/local/src/ [[email protected] src]# ll apache-tomcat-8.0.53.tar.gz -rw-rw-r--. 1 root root 9472492 Nov 9 2017 apache-tomcat-8.0.53.tar.gz [[email protected] src]# tar -zvxf apache-tomcat-8.0.53.tar.gz [[email protected] src]# mv apache-tomcat-8.0.53 /usr/local/tomcat8 现在开始进行tomcat自带cluster方式的session共享配置: [[email protected] ~]# cd /usr/local/tomcat8/conf/ [[email protected] conf]# cp server.xml server.xml.bak [[email protected] conf]# vim server.xml ....... <Engine name="Catalina" defaultHost="localhost"> 找到上面这一行,在此行的下面直接复制以下内容,不用修改,直接复制就可以 <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOptions="6"> <Manager className="org.apache.catalina.ha.session.BackupManager" expireSessionsOnShutdown="false" notifyListenersOnReplication="true" mapSendOptions="6"/> <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="192.168.10.201" port="5000" selectorTimeout="100" 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.MessageDispatch15Interceptor"/> <Interceptor className="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor"/> </Channel> <Valve className="org.apache.catalina.ha.tcp.ReplicationValve" filter=".*\.gif;.*\.js;.*\.jpg;.*\.png;.*\.htm;.*\.html;.*\.css;.*\.txt;"/> <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> 记得多个tomcat都需要配置,同一个集群里面配置信息保持一直就可以了。如果是其他集群,修改一下address就好了。 另一个tomcat节点直接拷贝这个server.xml文件,然后将address修改为192.168.10.202就可以了 ================================================================================================== 配置好的server.xml可以直接使用,下载地址: https://pan.baidu.com/s/14Lgq97pyB_XLLu9j_CHtuw 提取密码:mrz4 ================================================================================================== 然后还有一个重要配置不要忘记了!否则session将不能实现共享!! 在tomcat项目的WEB-INF/web.xml文件中添加<distributable/>. [[email protected] ~]# cd /usr/local/tomcat8/webapps/ROOT/WEB-INF/ #两台tomcat节点都要修改 [[email protected] WEB-INF]# ls web.xml [[email protected] WEB-INF]# vim web.xml #直接在底部</web-app>的前面添加既可以 ....... <distributable/> 编写一个测试页面(直接修改index.jsp文件): [[email protected] ~]# cat /usr/local/tomcat8/webapps/ROOT/index.jsp <html> <head> <title> session test</title> </head> <body> SessionID is <%=session.getId()%> <BR> SessionIP is <%=request.getServerName()%> <BR> SessionPort is <%=request.getServerPort()%> <% out.println("Response from tomcat-node1"); %> %</body> %</html> 另一个tomcat2节点的测试内容: [[email protected] ~]# cat /usr/local/tomcat8/webapps/ROOT/index.jsp <html> <head> <title> session test</title> </head> <body> SessionID is <%=session.getId()%> <BR> SessionIP is <%=request.getServerName()%> <BR> SessionPort is <%=request.getServerPort()%> <% out.println("Response from tomcat-node2"); %> %</body> %</html> 然后重启两个节点的tomcat服务: 启动tomcat [[email protected] src]# /usr/local/tomcat8/bin/startup.sh Using CATALINA_BASE: /usr/local/tomcat8 Using CATALINA_HOME: /usr/local/tomcat8 Using CATALINA_TMPDIR: /usr/local/tomcat8/temp Using JRE_HOME: /usr/java/jdk1.8.0_131 Using CLASSPATH: /usr/local/tomcat8/bin/bootstrap.jar:/usr/local/tomcat8/bin/tomcat-juli.jar Tomcat started. You have new mail in /var/spool/mail/root [[email protected] src]# ps -ef|grep tomcat root 8477 1 87 03:11 pts/0 00:00:03 /usr/java/jdk1.8.0_131/bin/java -Djava.util.logging.config.file=/usr/local/tomcat8/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -classpath /usr/local/tomcat8/bin/bootstrap.jar:/usr/local/tomcat8/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat8 -Dcatalina.home=/usr/local/tomcat8 -Djava.io.tmpdir=/usr/local/tomcat8/temp org.apache.catalina.startup.Bootstrap start root 8528 6829 0 03:11 pts/0 00:00:00 grep tomcat [[email protected] src]# lsof -i:8080 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME java 8477 root 49u IPv6 12974768 0t0 TCP *:webcache (LISTEN)
4) Session共享测试
访问http://www.kevin.com,不断刷新页面,发现页面信息会改变,但是SessionID一直保持不变!!
关闭192.168.10.201或192.168.10.202两个中的任意一个tomcat服务,访问页面,不断刷新,SessionID都会一直保持不变!!
原文地址:https://www.cnblogs.com/kevingrace/p/9539005.html
时间: 2024-11-06 03:47:05