nginx+memcahed+tomcat实现session的资源共享

一、前提
参考的博客:http://blog.csdn.net;
相关包下载,含lib所需的jar包:http://pan.baidu.com/;
环境: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


二、配置
(1)、tomcat机的配置
1、卸载自带jdk ,采用rpm 包安装
//批量卸载自带的jdk相关包

rpm –qa |grep java | xargs rpm -e --nodeps

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 每个节点都要放)
参考这篇博客
6.更改tomcat session保存方式(Non-Sticky,kryo)
– 以下内容加到配置conf/context.xml文件里
#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

(2)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 服务器
nginx.conf 配置

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;
}
}

(3).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`&#160; //关闭

参考链接: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 保持不变

本文转自:http://my.oschina.net/u/1791256/blog/283064?fromerr=ZsKHawuG

你可能感兴趣的:(Linux,nginx,nosql)