Linux高可用(HA)之Nginx+Redis+Tomcat集群实现session保持和共享

摘要

nginx 作为目前最流行的开源反向代理HTTP Server,用于实现资源缓存、web server负载均衡等功能,由于其轻量级、高性能、高可靠等特点在互联网项目中有着非常普遍的应用,相关概念网上有丰富的介绍。分布式web server集群部署后需要实现session共享,针对 tomcat 服务器的实现方案多种多样,比如 tomcat cluster session 广播、nginx IP hash策略、nginx sticky module等方案,本文主要介绍了使用 redis 服务器进行 session 统一存储管理的共享方案。

环境介绍

node1:Legion100.dwhd.org 172.16.6.100

node2:Legion101.dwhd.org 172.16.6.101

注意:命令开头是[root@Legion100 ~]还是[root@Legion101 ~] Linux高可用(HA)之Nginx+Redis+Tomcat集群实现session保持和共享

1、安装nginx

[[email protected] ~]# cd /tmp && wget http://nginx.org/download/nginx-1.9.1.tar.gz
[[email protected] /tmp]# Username="www" && for i in `seq 1000 1500`;do [ -z "$(awk -F: '{print$3,$4}' /etc/passwd | grep "$i")" -a -z "$(awk -F: '{print$3}' /etc/group | grep "$i")" ] && UGID=$i && break;done && groupadd -g $UGID $Username && useradd -M -u $UGID -g $UGID -s /sbin/nologin $Username
[[email protected] /tmp]# tar xf nginx-1.9.1.tar.gz
[[email protected] /tmp]# cd nginx-1.9.1
[[email protected] /tmp/nginx-1.9.1]# yum install pcre-devel pcre -y
[[email protected] /tmp/nginx-1.9.1]# mkdir -p {/tmp/nginx,/var/run/nginx,/var/lock}
[[email protected] /tmp/nginx-1.9.1]# ./configure --prefix=/usr/local/nginx/ --user=www --group=www \
--error-log-path=/tmp/nginx/error.log --http-log-path=/tmp/nginx/access.log \
--pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock \
--with-pcre --with-http_ssl_module --with-http_flv_module \
--with-http_spdy_module --with-http_gzip_static_module \
--with-http_stub_status_module --http-client-body-temp-path=/usr/local/nginx/client/ \
--http-proxy-temp-path=/usr/local/nginx/proxy/ --http-fastcgi-temp-path=/usr/local/nginx/fcgi/ \
--http-uwsgi-temp-path=/usr/local/nginx/uwsgi --http-scgi-temp-path=/usr/local/nginx/scgi
[[email protected] /tmp/nginx-1.9.1]# make -j $(awk '/processor/{i++}END{print i}' /proc/cpuinfo) && make install && cd ../ && echo $?
[[email protected] /tmp]# echo "export PATH=/usr/local/nginx/sbin:\$PATH" > /etc/profile.d/nginx1.9.1.sh
[[email protected] /tmp]# . /etc/profile.d/nginx1.9.1.sh
[[email protected] /tmp]# nginx -V
nginx version: nginx/1.9.1
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-11) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx/ --user=www --group=www --error-log-path=/tmp/nginx/error.log --http-log-path=/tmp/nginx/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --with-pcre --with-http_ssl_module --with-http_flv_module --with-http_spdy_module --with-http_gzip_static_module --with-http_stub_status_module --http-client-body-temp-path=/usr/local/nginx/client/ --http-proxy-temp-path=/usr/local/nginx/proxy/ --http-fastcgi-temp-path=/usr/local/nginx/fcgi/ --http-uwsgi-temp-path=/usr/local/nginx/uwsgi --http-scgi-temp-path=/usr/local/nginx/scgi
[[email protected] /tmp]# wget http://www.dwhd.org/script/Nginx-init-CentOS -O /etc/rc.d/init.d/nginx
[[email protected] /tmp]# chmod +x /etc/rc.d/init.d/nginx
[[email protected] /tmp]# chkconfig nginx on
[[email protected] /tmp]# chkconfig --list nginx
nginx           0:关闭  1:关闭  2:启用  3:启用  4:启用  5:启用  6:关闭
[[email protected] /tmp]# 

2、node1上安装jdk和tomcat

[[email protected] /tmp]# wget -c --header "Cookie: oraclelicense=accept-securebackup-cookie" `curl -s http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html | awk -F'"' '/jdk\-7.*\-linux\-x64.rpm/{print $12;exit}'`
[[email protected] /tmp]# rpm -ivh jdk-7u79-linux-x64.rpm
[[email protected] /tmp]# cat > /etc/profile.d/java.sh << EOF
JAVA_HOME=/usr/java/latest
PATH=\$JAVA_HOME/bin:\$PATH
export JAVA_HOME PATH
EOF
[[email protected] /tmp]# wget -c `curl -s "https://tomcat.apache.org/download-70.cgi" | awk -F'"' '/apache\-tomcat\-7.*.tar.gz/{print $2;exit}'`
[[email protected] /tmp]# tar xf apache-tomcat-7.*.tar.gz -C /usr/local/
[[email protected] /tmp]# ln -sv /usr/local/apache-tomcat-7.* /usr/local/tomcat
[[email protected] /tmp]# cat > /etc/profile.d/tomcat.sh << EOF
export CATALINA_HOME=/usr/local/tomcat
export PATH=\$CATALINA_HOME/bin:\$PATH
EOF
[[email protected] /tmp]# source /etc/profile.d/java.sh
[[email protected] /tmp]# source /etc/profile.d/tomcat.sh
[[email protected] /tmp]# catalina.sh version
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/java/latest
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Server version: Apache Tomcat/7.0.62
Server built:   May 7 2015 17:14:55 UTC
Server number:  7.0.62.0
OS Name:        Linux
OS Version:     2.6.32-504.16.2.el6.centos.plus.x86_64
Architecture:   amd64
JVM Version:    1.7.0_79-b15
JVM Vendor:     Oracle Corporation
[[email protected] /tmp]# 

3、node2上安装jdk和tomcat

[[email protected] ~]# scp 172.16.6.100:/tmp/{jdk-7u*-linux-x64.rpm,apache-tomcat-7.*.tar.gz} /tmp
[email protected]'s password:
jdk-7u79-linux-x64.rpm                                                                                                      100%  132MB 131.7MB/s   00:01
[email protected]'s password:
apache-tomcat-7.0.62.tar.gz                                                                                                 100% 8618KB   8.4MB/s   00:00
[[email protected] ~]# cd /tmp/
[[email protected] /tmp]# rpm -ivh jdk-7u79-linux-x64.rpm
[[email protected] /tmp]# cat > /etc/profile.d/java.sh << EOF
JAVA_HOME=/usr/java/latest
PATH=\$JAVA_HOME/bin:\$PATH
export JAVA_HOME PATH
EOF
[[email protected] /tmp]# tar xf apache-tomcat-7.*.tar.gz -C /usr/local/
[[email protected] /tmp]# ln -sv /usr/local/apache-tomcat-7.* /usr/local/tomcat
[[email protected] /tmp]# cat > /etc/profile.d/tomcat.sh << EOF
export CATALINA_HOME=/usr/local/tomcat
export PATH=\$CATALINA_HOME/bin:\$PATH
EOF
[[email protected] /tmp]# source /etc/profile.d/java.sh
[[email protected] /tmp]# source /etc/profile.d/tomcat.sh
[[email protected] /tmp]# catalina.sh version
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/java/latest
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Server version: Apache Tomcat/7.0.62
Server built:   May 7 2015 17:14:55 UTC
Server number:  7.0.62.0
OS Name:        Linux
OS Version:     2.6.32-504.16.2.el6.centos.plus.x86_64
Architecture:   amd64
JVM Version:    1.7.0_79-b15
JVM Vendor:     Oracle Corporation
[[email protected] /tmp]# 

4、构建 tomcat-redis-session-manager-master

[[email protected] /tmp]# wget -c http://www.dwhd.org/script/tar_gz_bz2/gradle-2.4-all.zip
[[email protected] /tmp]# unzip -q gradle-2.4-all.zip -d /usr/local/
[[email protected] /tmp]# ln -sv /usr/local/gradle-2.4 /usr/local/gradle
[[email protected] /tmp]# echo "export PATH=/usr/local/gradle/bin:\$PATH" > /etc/profile.d/gradle2.4.sh
[[email protected] /tmp]# . /etc/profile.d/gradle2.4.sh
[[email protected] /tmp]# which gradle
/usr/local/gradle/bin/gradle
[[email protected] /tmp]# git clone https://github.com/jcoleman/tomcat-redis-session-manager.git
[[email protected] /tmp]# cd tomcat-redis-session-manager/
[[email protected] /tmp/tomcat-redis-session-manager]# mv build.gradle build.gradle_backup
[[email protected] /tmp/tomcat-redis-session-manager]# wget -q http://www.dwhd.org/script/build.gradle
[[email protected] /tmp/tomcat-redis-session-manager]# gradle build -x test copyJars
:compileJava
Download https://repo1.maven.org/maven2/org/apache/tomcat/tomcat-catalina/7.0.27/tomcat-catalina-7.0.27.pom
Download https://repo1.maven.org/maven2/redis/clients/jedis/2.5.2/jedis-2.5.2.pom
Download https://repo1.maven.org/maven2/org/sonatype/oss/oss-parent/7/oss-parent-7.pom
Download https://repo1.maven.org/maven2/org/apache/commons/commons-pool2/2.2/commons-pool2-2.2.pom
Download https://repo1.maven.org/maven2/org/apache/commons/commons-parent/33/commons-parent-33.pom
Download https://repo1.maven.org/maven2/org/apache/apache/13/apache-13.pom
Download https://repo1.maven.org/maven2/org/apache/tomcat/tomcat-servlet-api/7.0.27/tomcat-servlet-api-7.0.27.pom
Download https://repo1.maven.org/maven2/org/apache/tomcat/tomcat-juli/7.0.27/tomcat-juli-7.0.27.pom
Download https://repo1.maven.org/maven2/org/apache/tomcat/tomcat-annotations-api/7.0.27/tomcat-annotations-api-7.0.27.pom
Download https://repo1.maven.org/maven2/org/apache/tomcat/tomcat-api/7.0.27/tomcat-api-7.0.27.pom
Download https://repo1.maven.org/maven2/org/apache/tomcat/tomcat-util/7.0.27/tomcat-util-7.0.27.pom
Download https://repo1.maven.org/maven2/org/apache/tomcat/tomcat-catalina/7.0.27/tomcat-catalina-7.0.27.jar
Download https://repo1.maven.org/maven2/redis/clients/jedis/2.5.2/jedis-2.5.2.jar
Download https://repo1.maven.org/maven2/org/apache/commons/commons-pool2/2.2/commons-pool2-2.2.jar
Download https://repo1.maven.org/maven2/org/apache/tomcat/tomcat-servlet-api/7.0.27/tomcat-servlet-api-7.0.27.jar
Download https://repo1.maven.org/maven2/org/apache/tomcat/tomcat-juli/7.0.27/tomcat-juli-7.0.27.jar
Download https://repo1.maven.org/maven2/org/apache/tomcat/tomcat-annotations-api/7.0.27/tomcat-annotations-api-7.0.27.jar
Download https://repo1.maven.org/maven2/org/apache/tomcat/tomcat-api/7.0.27/tomcat-api-7.0.27.jar
Download https://repo1.maven.org/maven2/org/apache/tomcat/tomcat-util/7.0.27/tomcat-util-7.0.27.jar
:processResources UP-TO-DATE
:classes
:jar
:javadoc
:javadocJar
:sourcesJar
:assemble
:check
:build
:copyJars

BUILD SUCCESSFUL

Total time: 27.804 secs

This build could be faster, please consider using the Gradle Daemon: http://gradle.org/docs/2.4/userguide/gradle_daemon.html
[[email protected] /tmp/tomcat-redis-session-manager]# mkdir /tmp/jar
[[email protected] /tmp/tomcat-redis-session-manager]# cp -a build/libs/*.jar /tmp/jar/ && cp -a dist/*.jar /tmp/jar/
[[email protected] /tmp/tomcat-redis-session-manager]# 

Linux高可用(HA)之Nginx+Redis+Tomcat集群实现session保持和共享

5、配置和修改tomcat

[[email protected] /tmp/tomcat-redis-session-manager]# cp -a /tmp/jar/* /usr/local/tomcat/lib/
[[email protected] /tmp/tomcat-redis-session-manager]# scp /tmp/jar/* 172.16.6.101:/usr/local/tomcat/lib/
The authenticity of host '172.16.6.101 (172.16.6.101)' can't be established.
RSA key fingerprint is a6:bb:83:e4:04:27:55:50:9a:e1:25:5a:7b:01:bc:8c.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.16.6.101' (RSA) to the list of known hosts.
[email protected]'s password:
commons-pool2-2.2.jar                                                                                                       100%  106KB 105.5KB/s   00:00
jedis-2.5.2.jar                                                                                                             100%  307KB 307.0KB/s   00:00
tomcat-annotations-api-7.0.27.jar                                                                                           100%   15KB  14.9KB/s   00:00
tomcat-api-7.0.27.jar                                                                                                       100% 6873     6.7KB/s   00:00
tomcat-catalina-7.0.27.jar                                                                                                  100% 1500KB   1.5MB/s   00:00
tomcat-juli-7.0.27.jar                                                                                                      100%   37KB  37.2KB/s   00:00
tomcat-redis-session-manager-2.0.0.jar                                                                                      100%   21KB  20.5KB/s   00:00
tomcat-redis-session-manager-2.0.0-javadoc.jar                                                                              100%   59KB  59.3KB/s   00:00
tomcat-redis-session-manager-2.0.0-sources.jar                                                                              100%   10KB  10.1KB/s   00:00
tomcat-servlet-api-7.0.27.jar                                                                                               100%  173KB 172.8KB/s   00:00
tomcat-util-7.0.27.jar                                                                                                      100%   23KB  22.5KB/s   00:00
[[email protected] /tmp/tomcat-redis-session-manager]# cd /usr/local/tomcat/conf/
[[email protected] /usr/local/tomcat/conf]# cat context.xml #将context.xml添加如下内容
<?xml version='1.0' encoding='utf-8'?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<!-- The contents of this file will be loaded for each web application -->
<Context>

    <!-- Default set of monitored resources -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
    <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
        host="172.16.6.100"
        port="6379"
        database="0"
        maxInactiveInterval="60" />

    <!-- Uncomment this to disable session persistence across Tomcat restarts -->
    <!--
    <Manager pathname="" />
    -->

    <!-- Uncomment this to enable Comet connection tacking (provides events
         on session expiration as well as webapp lifecycle) -->
    <!--
    <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
    -->

</Context>
[[email protected] /usr/local/tomcat/conf]# scp context.xml 172.16.6.101:/usr/local/tomcat/conf/

6、安装redis

[[email protected] /usr/local/tomcat/conf]# cd /tmp/
[[email protected] /tmp]# wget -c `curl -s http://redis.io/download | awk -F"'" '/2.8.*.tar.gz/{print $4}'`
[[email protected] /tmp]# tar xf 2.8.*.tar.gz
[[email protected] /tmp]# cd redis-2.8.*
[[email protected] /tmp/redis-2.8.20]# if [ `getconf WORD_BIT` == 32 ] && [ `getconf LONG_BIT` == 32 ];then sed -i '1i\CFLAGS= -march=i686' src/Makefile && sed -i 's@^OPT=.*@OPT=-O2 -march=i686@' src/.make-settings;fi
[[email protected] /tmp/redis-2.8.20]# make
[[email protected] /tmp/redis-2.8.20]# mkdir -p /usr/local/redis/{bin,etc,var}
[[email protected] /tmp/redis-2.8.20]# cp -af src/{redis-benchmark,redis-check-aof,redis-check-dump,redis-cli,redis-sentinel,redis-server} /usr/local/redis/bin/
[[email protected] /tmp/redis-2.8.20]# cp -a redis.conf /usr/local/redis/etc/
[[email protected] /tmp/redis-2.8.21]# echo "export PATH=/usr/local/redis/bin:\$PATH" > /etc/profile.d/redis2.8.sh
[[email protected] /tmp/redis-2.8.21]# . /etc/profile.d/redis2.8.sh
[[email protected] /tmp/redis-2.8.20]# sed -i '[email protected].*@pidfile /var/run/redis.pid@' /usr/local/redis/etc/redis.conf
[[email protected] /tmp/redis-2.8.20]# sed -i "[email protected].*@logfile /usr/local/redis/var/redis.log@" /usr/local/redis/etc/redis.conf
[[email protected] /tmp/redis-2.8.20]# sed -i "s@^dir.*@dir /usr/local/redis/var@" /usr/local/redis/etc/redis.conf
[[email protected] /tmp/redis-2.8.20]# sed -i '[email protected] [email protected] yes@' /usr/local/redis/etc/redis.conf
[[email protected] /tmp/redis-2.8.20]# [ -z "`grep ^maxmemory /usr/local/redis/etc/redis.conf`" ] && sed -i '[email protected] <bytes>@maxmemory <bytes>\nmaxmemory 360000000@' /usr/local/redis/etc/redis.conf
[[email protected] /tmp/redis-2.8.20]# wget http://www.dwhd.org/script/Redis-server-init-CentOS -O /etc/init.d/redis-server
[[email protected] /tmp/redis-2.8.20]# chmod +x /etc/init.d/redis-server
[[email protected] /tmp/redis-2.8.20]# chkconfig --add redis-server
[[email protected] /tmp/redis-2.8.20]# chkconfig redis-server on
[[email protected] /tmp/redis-2.8.20]# chkconfig --list redis-server
redis-server    0:关闭  1:关闭  2:启用  3:启用  4:启用  5:启用  6:关闭
[[email protected] /tmp/redis-2.8.21]# service redis-server start
正在启动 redis-server:                                    [确定]
[[email protected] /tmp/redis-2.8.20]# ss -tnlp | grep 6379
LISTEN     0      511                       *:6379                     *:*      users:(("redis-server",48900,5))
LISTEN     0      511                      :::6379                    :::*      users:(("redis-server",48900,4))
[[email protected] /tmp/redis-2.8.20]# 

7、修改nginx配置

[[email protected] /tmp/redis-2.8.20]# cd /usr/local/nginx/conf/
[[email protected] /usr/local/nginx/conf]# cp nginx.conf nginx.conf_backup_`date +%F`
[[email protected] /usr/local/nginx/conf]# cat /usr/local/nginx/conf/nginx.conf
worker_processes  1;

events {
	worker_connections  1024;
}

http {
	include       mime.types;
	default_type  application/octet-stream;
	sendfile        on;
	keepalive_timeout  65;

	upstream tomcat.dwhd.org {
		server 172.16.6.100:8080;
		server 172.16.6.101:8080;
	}

	server {
		listen       80;
		server_name  tomcat.dwhd.org;

		location / {
			#root   html;
			#index  index.html index.htm;
			index  index_tel.jsp index.jsp index.html index.htm;
			proxy_pass				http://tomcat.dwhd.org;
			proxy_set_header		Host    			$host;
			proxy_set_header		X-Real-IP       	$remote_addr;
			proxy_set_header		X-Forwarded-For 	$proxy_add_x_forwarded_for;
			client_max_body_size    10m;
			client_body_buffer_size 128k;
			proxy_buffers			32      			4k;
			proxy_connect_timeout   3;
			proxy_send_timeou		30;
			proxy_read_timeout		30;
			proxy_redirect			off;
		}

		error_page   500 502 503 504  /50x.html;
		location = /50x.html {
			root   html;
		}
	}
}
[[email protected] /usr/local/nginx/conf]# service nginx configtest
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] /usr/local/nginx/conf]# 

8、启动Nginx Tomcat Redis

[[email protected] /usr/local/nginx/conf]# cd
[[email protected] ~]# catalina.sh start
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/java/latest
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
[[email protected] ~]# ssh [email protected] "catalina.sh start"
[email protected]''s password:
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/java/latest
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
[[email protected] ~]# service redis-server stop
停止 redis-server:                                        [确定]
[[email protected] ~]# service redis-server start
正在启动 redis-server:                                    [确定]
[[email protected] ~]# service nginx start
正在启动 nginx:                                           [确定]
[[email protected] ~]# ss -tnlp | grep -E 'nginx|redis|java'
LISTEN     0      1          ::ffff:127.0.0.1:8005                    :::*      users:(("java",49194,56))
LISTEN     0      100                      :::8009                    :::*      users:(("java",49194,54))
LISTEN     0      511                       *:6379                     *:*      users:(("redis-server",49294,5))
LISTEN     0      511                      :::6379                    :::*      users:(("redis-server",49294,4))
LISTEN     0      511                       *:80                       *:*      users:(("nginx",49411,6),("nginx",49412,6))
LISTEN     0      100                      :::8080                    :::*      users:(("java",49194,53))
[[email protected] ~]# 

9、建立测试页

[[email protected] ~]# mkdir -pv /usr/local/tomcat/webapps/session/WEB-INF/{classes,lib}
mkdir: 已创建目录 "/usr/local/tomcat/webapps/session"
mkdir: 已创建目录 "/usr/local/tomcat/webapps/session/WEB-INF"
mkdir: 已创建目录 "/usr/local/tomcat/webapps/session/WEB-INF/classes"
mkdir: 已创建目录 "/usr/local/tomcat/webapps/session/WEB-INF/lib"
[[email protected] ~]# cd /usr/local/tomcat/webapps/session/
[[email protected] /usr/local/tomcat/webapps/session]# cat index.jsp
<%@ page language="java" %>
<html>
  <head><title>TomcatA</title></head>
  <body>
    <h1><font color="red">TomcatA.dwhd.org</font></h1>
    <table align="centre" border="1">
      <tr>
        <td>Session ID</td>
    <% session.setAttribute("tomcat.dwhd.org","tomcat.dwhd.org"); %>
        <td><%= session.getId() %></td>
      </tr>
      <tr>
        <td>Created on</td>
        <td><%= session.getCreationTime() %></td>
     </tr>
    </table>
  </body>
</html>
[[email protected] /usr/local/tomcat/webapps/session]# 
[[email protected] /tmp]# mkdir -pv /usr/local/tomcat/webapps/session/WEB-INF/{classes,lib}
[[email protected] /tmp]# cd /usr/local/tomcat/webapps/session/
[[email protected] /usr/local/tomcat/webapps/session]# cat index.jsp
<%@ page language="java" %>
<html>
  <head><title>TomcatB</title></head>
  <body>
    <h1><font color="blue">TomcatB.dwhd.org</font></h1>
    <table align="centre" border="1">
      <tr>
        <td>Session ID</td>
    <% session.setAttribute("tomcat.dwhd.org","tomcat.dwhd.org"); %>
        <td><%= session.getId() %></td>
      </tr>
      <tr>
        <td>Created on</td>
        <td><%= session.getCreationTime() %></td>
     </tr>
    </table>
  </body>
</html>
[[email protected] /usr/local/tomcat/webapps/session]# 

10、查看效果
Linux高可用(HA)之Nginx+Redis+Tomcat集群实现session保持和共享
Linux高可用(HA)之Nginx+Redis+Tomcat集群实现session保持和共享

Linux高可用(HA)之Nginx+Redis+Tomcat集群实现session保持和共享

Linux高可用(HA)之Nginx+Redis+Tomcat集群实现session保持和共享

Linux高可用(HA)之Nginx+Redis+Tomcat集群实现session保持和共享

Linux高可用(HA)之Nginx+Redis+Tomcat集群实现session保持和共享


Linux高可用(HA)之Nginx+Redis+Tomcat集群实现session保持和共享

Linux高可用(HA)之Nginx+Redis+Tomcat集群实现session保持和共享

参考文章:
http://www.cnblogs.com/lengfo/p/4260363.html
http://lovelace.blog.51cto.com/1028430/1550198
  • 本文由 发表于 2015年6月4日09:59:52
  • 除非特殊声明,本站文章均为原创,转载请务必保留本文链接
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: