需求描述:客户多次访问集群内tomcat服务器时,被调度器分配到未存储其session信息的tomcat服务器,需要重新输入其账户等信息。为了用户的使用方便,以及集群的更好实现,需要共享客户访问的session信息给集群中的应用服务器
环境:3台 Red Hat Enterprise Linux Server release 6.7 (Santiago)及系统镜像文件
nginx :192.168.4.10
tomcat1:192.168.4.11
tomcat2:192.168.4.12
安装包清单:
nginx-1.8.0.tar.gz
apache-tomcat-8.0.30.tar.gz
libevent-2.0.21-stable.tar.gz
jdk-8u77-linux-x64.rpm
1、安装Nginx作为调度器
1)Nginx安装环境准备,通过yum安装依赖包,创建nginx用户
# yum -y install gcc gcc-c++ openssl-devel pcre-devel zlib-devel
# useradd -s /sbin/nologin nginx
2)通过源码安装nginx
# cp nginx-1.8.0.tar.gz /opt/
# cd /opt/
# tar -xf nginx-1.8.0.tar.gz
# cd nginx-1.8.0
# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx
# make && make install
3)启动nginx,检查nginx服务状态,并通过浏览器访问nginx服务,测试是否正常访问(避免因为selinux、防火墙导致访问失败)
# /usr/local/nginx/sbin/nginx
# netstat -anput|grep 80
4)修改rc.local设置开机启动nginx
# /usr/local/nginx/sbin/nginx
# tail -1 /etc/rc.local
/usr/local/nginx/sbin/nginx
2、安装memcached
在nginx服务器上安装memcached(可单独分配一台服务器运行memcached),并启动服务,可通过yum或者源码安装,在此通过yum安装
1)yum安装memcached
# yum -y install memcached
# rpm -qa|grep memcached
memcached-1.4.4-3.el6.x86_64
2)启动memcached,检查服务状态,并设置开机自启
# /etc/init.d/memcached start
# netstat -anput|grep 11211
tcp 0 0 0.0.0.0:11211 0.0.0.0:* LISTEN 1730/memcached
# chkconfig memcached on
# chkconfig memcached --list
memcached 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
3、部署tomcat服务器集群
安装tomcat并配置context.xml实现session共享(可配置其中一台,然后拷贝到其他服务器)
1)安装JDK
# rpm -ivh jdk-8u77-linux-x64.rpm
# java -version
java version "1.8.0_77"
Java(TM) SE Runtime Environment (build 1.8.0_77-b03)
Java HotSpot(TM) 64-Bit Server VM (build 25.77-b03, mixed mode)
2)安装部署tomcat,启动服务并测试访问效果,同时设置开机自启
# tar -xf apache-tomcat-8.0.30.tar.gz -C /usr/local/
# cd /usr/local/
# mv apache-tomcat-8.0.30 tomcat
# /usr/local/tomcat/bin/catalina.sh start
# netstat -anput|grep 8080
# tail -1 /etc/rc.local
/usr/local/tomcat/bin/catalina.sh start
3)拷贝jar包到/usr/local/tomcat/lib/
# cp session/*.jar /usr/local/tomcat/lib/
4)修改context文件
# tail -5 /usr/local/tomcat/conf/context.xml|head -4
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="mem1:192.168.4.10:11211"
requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
transcoderFactoryClass="de.javakaffee.web.msm.JavaSerializationTranscoderFactory"/>
5)搭建测试页面,重启tomcat
# cat /usr/local/tomcat/webapps/ROOT/test.jsp
<html>
<body bgcolor="red">
<center>
<%String s = session.getId();%>
<%=s%>
<h1>tomcatA </h1>
</center>
</body>
</html>
# /usr/local/tomcat/bin/catalina.sh stop
# /usr/local/tomcat/bin/catalina.sh start
6)在其他tomcat节点上安装JDK,拷贝tomcat到其他tomcat节点。为测试方便需修改测试页面标识tomcatA、tomcatB,同时设置开机自启tomcat
# rpm -ivh jdk-8u77-linux-x64.rpm tomcat需要JDK环境,注意在其他节点上安装
# scp -r /usr/local/tomcat 192.168.4.11:/usr/local/
# sed -n '/tomcat/p' /usr/local/tomcat/webapps/ROOT/test.jsp
<h1>tomcatA </h1>
# sed -n '/tomcat/p' /usr/local/tomcat/webapps/ROOT/test.jsp
<h1>tomcatB </h1>
4、修改nginx配置文件,反向代理tomcat
1)修改nginx配置文件,调用后端tomcat集群
# egrep -v "#|^$" /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 tomcats {
server 192.168.4.12:8080 weight=1;
server 192.168.4.11:8080 weight=2;
}
server {
listen 192.168.4.10:80;
server_name localhost;
location / {
proxy_pass http://tomcats;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
2)重启nginx服务
# /usr/local/nginx/sbin/nginx -s stop
# /usr/local/nginx/sbin/nginx
5、测试实现效果
++++++++++++++++++++++++over++++++++++++++++++++++++++++++