Tomcat自带的Session共享方案实施记录

一般来说,在多个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

Tomcat自带的Session共享方案实施记录的相关文章

细说tomcat之集群session共享方案

1. Tomcat Cluster官网:http://tomcat.apache.org/tomcat-7.0-doc/cluster-howto.htmlTomcat原生支持的集群方案. <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOptions="8"> <Manager className="org.apache.catali

生产-tomcat的session共享方案

生产-tomcat的session共享方案 在部署nginx+tomcat集群后,nginx采用负载均衡轮训模式,会出现访问时每个tomcat应用都会互相覆盖sessionID,导致sessionID不断变化,从而导致java一些功能实现不正常.作者查阅了不少文档,并且在测试环境和预生产环境进行各类测试,有两种方法可行. 应为目的只是实现session共享,杜绝session不断变化,所以解决方案越简单越好,本文属于实践文档,不会过多涉及原理和技术方面的东东,谷歌有详细相关技术介绍. 1    

Nginx+Tomcat+Memcached集群Session共享

提供给大家整合实例: http://download.csdn.net/detail/zld1987/9577962 cookie是怎样工作的? 例如,我们创建了一个名字为login的Cookie来包含访问者的信息,创建Cookie时,服务器端的Header如下面所示,这里假设访问者的注册名是"Michael Jordan",同时还对所创建的Cookie的属性如path.domain.expires等进行了指定. Set-Cookie:login=Michael Jordan;path

【转】Nginx+Tomcat+Memcached集群Session共享

cookie是怎样工作的? 例 如,我们创建了一个名字为login的Cookie来包含访问者的信息,创建Cookie时,服务器端的Header如下面所示,这里假设访问者的注册名 是“Michael Jordan”,同时还对所创建的Cookie的属性如path.domain.expires等进行了指定. Set-Cookie:login=Michael Jordan;path=/;domain=msn.com; expires=Monday,01-Mar-99 00:00:01 GMT 上面这个H

基于tomcat集群做session共享

前端代理服务器nginx:192.168.223.136 tomcat服务器:采用的一台多实例192.168.223.146:8081,192.168.223.146:8082(如何构建多实例tomcat,请参考前面的文章) 首先查看下tomcat的webapps目录架构: [[email protected] tomcat-instance]# pwd/usr/local/tomcat-instance[[email protected] tomcat-instance]# ll总用量 24-

二级域名session 共享方案

二级域名session 共享方案 1.利用COOKIE存放session_id(); 实例: 域名一文件php代码: <?php session_start(); setcookie("session_id",session_id(),time()+3600*24*365*10,"/",".session.com"); $_SESSION['user_name'] = '梁山良民'; echo $_SESSION['user_name'];

Tomcat集群环境下session共享方案梳理(1)-通过memcached(MSM)方法实现

对于web应用集群的技术实现而言,最大的难点就是:如何能在集群中的多个节点之间保持数据的一致性,会话(Session)信息是这些数据中最重要的一块.要实现这一点, 大体上有两种方式: 一种是把所有Session数据放到一台服务器上或者数据库中,集群中的所有节点通过访问这台Session服务器来获取数据: 另一种就是在集群中的所有节点间进行Session数据的同步拷贝,任何一个节点均保存了所有的Session数据. Tomcat集群session同步方案有以下几种方式: 1)使用tomcat自带的

MSM实现tomcat集群中session共享的高可用

目录 1.测试环境概述 2.MSM简介 2.1.MSM的特性 2.2.MSM要解决的问题 2.3.MSM的工作原理 3.环境搭建 3.1.memcached安装 3.2.jkd与tomcat安装配置 3.3.MSM sticky session + kryo模式的配置 3.4.MSM no-sticky session + kryo模式的配置 4.思考与总结 1.测试环境概述 采用两台linux x64主机,主机上分别安装memcached与tomcat,memcached提供key/value

tomcat集群及session共享

一般来说,java web app主要用作两个领域: 1.api.api一般是无状态的,所以无需考虑session共享的问题 2.传统web应用和网站,如crm,oa,erp,b2c,bbs等.尤其bbs,b2c这样的互联网应用,单台tomcat往往无法满足高并发,所以需要做集群.如果做了集群,往往需要考虑session共享的问题,因为LB(apache/nginx)等等往往会将同一用户的请求分发到不同server 常用的servlet容器主要有tomcat(老牌的servlet容器,像apac