测试机器:192.168.18.207, 192.168.18.208,192.168.18.209
软件配置:
192.168.18.207
|
nginx/1.9.4
|
|
192.168.18.208
|
tomcat-7.0.64
|
memcached-1.2.0
|
192.168.18.209
|
tomcat-7.0.64
|
memcached-1.2.0
|
系统版本:
-
[root@localhost ~]# cat /proc/version
-
Linux version 3.10.0-229.el7.x86_64 (builder@kbuilder.dev.centos.org) (gcc version 4.8.2 20140120 (Red Hat 4.8.2-16) (GCC) ) #1 SMP Fri Mar 6 11:36:42 UTC 2015
1. 安装nginx
SERVER:192.168.18.207
下载 nginx-1.9.4.tar.gz
-
tar -zxvf nginx-1.9.4.tar.gz
-
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-pcre=/tools/pcre-8.36/
-
make && make install
修改配置文件nginx.conf,添加upstream , proxy_pass等
proxy_set_header X
-Real
-IP $remote_addr
; -- 这个在tomcat server.xml 中可以通过 %{X-Real-IP}i 获取客户端真实IP地址
-
upstream tomcat{
-
server 192.168.18.208:8080;
-
server 192.168.18.209:8080;
-
}
-
server {
-
listen 80;
-
server_name localhost;
-
#charset koi8-r;
-
-
location / {
-
root html;
-
index index.html index.htm;
-
proxy_pass http://tomcat;
-
proxy_set_header X-Real-IP $remote_addr;
-
access_log logs/test.log ;
-
}
-
error_page 500 502 503 504 /50x.html;
-
location = /50x.html {
-
root html;
-
}
-
}
启动Nginx
./nginx
2. 安装memcached
SERVER:192.168.18.208/192.168.18.209
下载 libevent-1.4.14b-stable.tar.gz , memcached-1.2.0.tar.gz
libevent-1.4.14b-stable.tar.gz
-
./configure --prefix=/usr
-
make && make instll
配置环境变量 export LD_LIBRARY_PATH=/usr/lib
memcached-1.2.0.tar.gz
-
./configure --prefix=/usr/local/memcached
-
make && make install
启动memcached
./memcached -d -uroot -p 11211 -m 20 -l
192.168.18.208 -c 200 -P /usr/local/memcached/mem.pid
3. 安装tomcat
SERVER:192.168.18.208/192.168.18.209
下载 apache-tomcat-7.0.64.tar.gz
-
tar -zxvf apache-tomcat-7.0.64.tar.gz
-
mv apache-tomcat-7.0.64 /usr/local/tomcat
修改配置文件conf/contex.xml
添加
-
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
-
memcachedNodes="n1:192.168.18.208:11211,n2:192.168.18.209:11211"
-
failoverNodes="n1"
-
requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
-
transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"
-
/>
此处使用黏性session ,failoverNodes
="n1" 是指session优先存放在n2上。 209 配置 failoverNodes="n2"
下载 msm
memcached-session-manager-1.6.1.jar
memcached-session-manager-tc7-1.6.1.jar
spymemcached-2.7.3.jar
由于采用javolution方式序列化,还需
msm-javolution-serializer-1.6.1.jar
javolution-5.4.3.1.jar
全部拷贝到tomcat lib下
启动tomcat
4. 测试
test.jsp 放到webapps/ROOT 下
192.168.18.208
-
SessionID:<%=session.getId()%>
-
<BR>
-
SessionIP:<%=request.getServerName()%>
-
<BR>
-
SessionPort:<%=request.getServerPort()%>
-
<%
-
out.println("This is Tomcat Server 1");
-
%>
192.168.18.209 改成 Server 2
测试结果:
1.第一次访问 页面显示如下:
SessionID:4CFBDB9CD9E7E19790F1567DD0BA84E7-
n2
SessionIP:tomcat
SessionPort:80
This is Tomcat Server 1
说明nginx将请求转发至208 ,session 存在n2(209)上,持续刷新,只是server 改变 ,sessionid不变
2.关闭209 网络 systemctl stop network.service
SessionID:4CFBDB9CD9E7E19790F1567DD0BA84E7-
n1
SessionIP:tomcat
SessionPort:80
This is Tomcat Server 1
如上 sessionID未变,但已切到n1
其他测试:
(1)关掉所有memcached 后,sessionid 还是保持不变,未测试能持续多少时间 。
(2)先启动tomcat ,不启动memcached ,查看日志会发现会发现有持续请求连接memcached错误,启动memcached后 恢复正常。
安装问题:
1.centos7 防火墙采用的是firewall 需要关闭防火墙
systemctl stop firewalld
systemctl stop iptables
2.安装memcached 需要配置环境变量
LD_LIBRARY_PATH=/usr/lib
本次测试参考了以下资料:
http://chenzhou123520.iteye.com/blog/1650212
http://blog.csdn.net/shimiso/article/details/8979044
阅读(1477) | 评论(0) | 转发(0) |