NGINX + TOMCAT7 + MEMCACHED 实现SESSION 共享

时间:2022-05-20 16:45:55

原文地址: http://my.oschina.net/u/1791256/blog/283064

TOMCAT7.0+ NGINX + MEMCACHED + memcached-session-manager 实现SESSION共享

http://blog.csdn.net/nerissa/article/details/18961361  ; //参考博客

http://pan.baidu.com/s/1pJlZ0mB    //相关包下载,含lib所需的jar包

环境:centOS 6.5(64位) 、jdk8_8u5 、tomcat7.054、nginx1.47、libevent-2.0 、memcached-1.4.20

10.1.1.127 // tomcat,nginx,memcached

10.1.1.128 // tomcat

tomcat 机配置

1、卸载自带jdk ,采用rpm 包安装

rpm –qa |grep java | xargs rpm -e --nodeps //批量卸载自带的jdk相关包

2、rpm 安装

rpm -ivh jdk-8u5-linux-x64.rpm //默认安装在 /usr/java

mv 改名改为 /usr/java/jdk

java -version //验证安装成功

3、写入环境

vim /etc/profile

JAVA_HOME=/usr/java/jdk

JRE_HOME=/usr/java/jdk/jre

PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin

CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib

export JAVA_HOME JRE_HOME PATH CLASSPATH

source /etc/profile // 环境生效

4.解压tomcat 测试运行

tar –zxf apache-tomcat-7.0.54.tar.gz  -C /usr/local

mv apache-tomcat-7.054 tomcat

vim /etc/sysconfig/iptables //防火墙开放8080端口

-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT

service iptables restart

http://10.1.1.127:8080/ //开启tomcat,测试tomcat安装成功

5.导入 msm-1.7包到 tom/lib/ ( tomcat 节点都要放

NGINX + TOMCAT7 + MEMCACHED 实现SESSION 共享

http://chenzhou123520.iteye.com/blog/1650212 //参考博客

6.更改tomcat session保存方式(Non-Sticky,kryo

以下内容加到配置文件里

vim conf/context.xml

<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"

memcachedNodes="n1:10.1.1.217:11211"

sticky="false"

lockingMode="auto"

sessionBackupAsync="false"

requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"

sessionBackupTimeout="1000" transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"

/>

//参数用法官方文档

https://code.google.com/p/memcached-session-manager/wiki/SetupAndConfiguration

章节Overview over memcached-session-manager configuration attributes

nginx 安装配置

1.源码包安装

#yum install pcre* //nginx 依赖pcre类库

#useradd nginx

#tar xf /share/soft/lamp/nginx/nginx-1.4.7.tar.gz -C /usr/src/

#cd /usr/src/nginx-1.4.7

#./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_gzip_static_module --with-http_stub_status_module

# make && make install

2.nginx 反向代理,实现负载均衡代理后面两台tomcat 服务器

user nginx nginx;

worker_processes 5;

error_log logs/error.log;

#error_log logs/error.log notice;

#error_log logs/error.log info;

pid logs/nginx.pid;

events {

worker_connections 1024;

use epoll;

}

http {

upstream session {

server 10.1.1.217:8080 weight=1 max_fails=2 fail_timeout=30s;

server 10.1.1.218:8080 weight=1 max_fails=2 fail_timeout=30s;

}

server {

listen 80;

server_name 10.1.1.217;

root /nginxroot/;

location ~ \.(txt|jsp)$ {

proxy_pass http://session;

}

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_set_header Host $http_host;

}

}

mkdir /nginxroot/

//以上是简单的实现的是对jsp访问的均衡调度,实验随便改的

memcached 的安装 //先安装libevent(memcached依赖libevent)

1.安装

#tar zxvf libevent-2.0.21-stable.tar.gz

#cd  zxvf libevent-2.0.21-stable

# ./configure --prefix=/usr

#make && make install

# tar zxvf memcached-1.4.20.tar.gz

#cd memcached-1.4.20

# ./configure --with-libevent=/usr

#make && make install

2.启动,关闭命令

//启动 -d 后台运行 –m 内存大小 –p 端口 –c 最大并发数

/usr/local/bin/memcached -d -m 128 -u root -p 11211 -c 256 -P /var/run/memcached.pid

kill `cat /var/run/memcached1.pid`  //关闭

http://hxq0506.iteye.com/blog/1388971 //参考链接

测试:

启动顺序 tomcat – nginx – memcached

测试页面test.jsp,放入webapps/ROOT:

Server Info:

SessionID:<%=session.getId()%>

<br>

SessionIP:<%=request.getServerName()%>

<br>

SessionPort:<%=request.getServerPort()%>

<br>

<%

out.println("127"); //标记后端节点

%>

结果:session 保持不变