迷彩 潜伏 隐蔽 伪装
分类: LINUX
2014-12-23 12:12:13
Nginx+Memcached+Tomcat session会话共享
环境:centos6.5_x64
node1:192.168.0.233 (233 上面做两方面工作,tomcat和memcache)
node2:192.168.0.234 (234,上面做两方面工作,tomcat 和nginx反向代理器)
(主机名和ip对应写入两台主机的/etc/hosts文件中)
这里用两台服务器做的实验:
node1 既做tomcat节点,又做memcache
node2既做tomcat节点也做代理(可以用四台台,一台代理,一台memcache,和两个节点)
一.配置代理服务器
1.安装依赖包:
Groupadd www
Useradd -s /sbin/nologin www
1.yum -y install gcc gcc-c++ libgcc
2.安装Ngingx nginx-1.4.4.tar.gz
tar -zxvf /source/Packages/pcre-8.33.tar.gz
cd pcre-8.33
./configure
make && make install
tar -zxvf nginx-1.4.4.tar.gz
cd nginx-1.4.4.
./configure --prefix=/usr/local/nginx --user=www --group=www--conf-path=/etc/nginx/nginx.conf --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-http_flv_module --with-pcre=/usr/local/src/pcre-8.35
make && make install
/usr/local/nginx/sbin/nginx 启动nginx
2.代理服务器配置:
user www www;
worker_processes 1;
#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;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / { #代理配置
proxy_pass
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
upstream company {
server 192.168.0.233:8081; #反向代理丢给两台主机
server 192.168.0.234:8081;
}
}
二.配置node1 node2 tomcat环境(两台机器同样配置)
1.安装jdk
root@node1 src]# sh jdk-6u45-linux-x64.bin
mv jdk1.6.0_4 /usr/jdk1.6.0_45
2.添加环境变量:
vi /etc/profile
export JAVA_HOME=/usr/jdk1.6.0_45
export PATH=$PATH:$JAVA_HOME/bin
export CLASSPATH=.:$JAVA_HOME/jre/lib:$JAVA_HOME/lib:$JAVA_HOME/lib/tools.jar
source /etc/profile 生效
3.测试
[root@node2 ~]# java -version
java version "1.6.0_45"
Java(TM) SE Runtime Environment (build 1.6.0_45-b06)
Java HotSpot(TM) 64-Bit Server VM (build 20.45-b01, mixed mode)
4.安装tomcat
tar -zxvf apache-tomcat-7.0.54.tar.gz
mv apache-tomcat-7.0.54 /usr/local/tomcat
/usr/local/tomcat/bin/startup.sh #tomcat 启动脚本
注意:复制依赖jar 包到/usr/local/tomcat/lib 里面(这些包必须拷贝到tomcat的lib库里面)
5.修改端口为8081
connectionTimeout="20000"
redirectPort="8443" />
6.tomcat 连接 memcache session 会话共享配置 :(node1和node2 配置一样)session 存储到192.168.0.233:11211 memcache里面。
vi /usr/local/tomcat/conf/context.xml
#将下面配置复制到
两台主机做同样的配置
requestUriIgnorePattern=".*/.(png|gif|jpg|css|js)$"
sticky="false"
storageKeyPrefix="context"
lockingMode="uriPattern:/path1|/path2"
sessionBackupAsync="false"
sessionBackupTimeout="100"
copyCollectionsForSerialization="false"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"/>
三.配置memcached(这里我把memcached放在233上面了)
1.分别把memcached和libevent下载回来,放到 /usr/local/src 目录下:
# cd /usr/local/src
# wget
# wget ~provos/libevent-1.2.tar.gz
2.先安装libevent:
# tar zxvf libevent-1.2.tar.gz
# cd libevent-1.2
# ./configure –prefix=/usr
# make
# make install
3.测试libevent是否安装成功:
# ls -al /usr/lib | grep libevent
lrwxrwxrwx 1 root root 21 11?? 12 17:38 libevent-1.2.so.1 ->
libevent-1.2.so.1.0.3
-rwxr-xr-x 1 root root 263546 11?? 12 17:38 libevent-1.2.so.1.0.3
-rw-r–r– 1 root root 454156 11?? 12 17:38 libevent.a
-rwxr-xr-x 1 root root 811 11?? 12 17:38 libevent.la
lrwxrwxrwx 1 root root 21 11?? 12 17:38 libevent.so -> libevent-1.2.so.1.0.3
还不错,都安装上了。
4.安装memcached,同时需要安装中指定libevent的安装位置:
# cd /tmp
# tar zxvf memcached-1.2.0.tar.gz
# cd memcached-1.2.0
# ./configure –with-libevent=/usr
# make
# make install
如果中间出现报错,请仔细检查错误信息,按照错误信息来配置或者增加相应的库或者路径。
安装完成后会把memcached放到 /usr/local/bin/memcached ,
5.测试是否成功安装memcached:
# ls -al /usr/loc
-rwxr-xr-x 1 root root 137986 11?? 12 17:39 /usr/local/bin/memcached
-rwxr-xr-x 1 root root 140179 11?? 12 17:39 /usr/local/bin/memcached-debug
6.启动memcache
useradd memcache #添加memcache用户
memcached -m 64 -d -u memcache -p 11211 -l 192.168.0.233 -c 5000 -P /tmp/memcached.pid
7.启动两个tomcat :
root@node1 ~]# tail -f /usr/local/tomcat/logs/catalina.2014-12-08.log #监控日志看看是否偶报错
启动: node1
日志查看:
启动: node2
[root@node2 bin]# /usr/local/tomcat/bin/startup.sh
日志查看:
[root@node1 ~]# tail -f /usr/local/tomcat/logs/catalina.2014-12-08.log
已经配置完毕
我们可以停掉某个Tomcat服务,然后观察Session会话数据的复制过程,能够保证当前集群中的会话数据不丢失,另一个结点仍然能提供基于该存在Session的服务。
测试
测试页面,在tomcat根目录,写一个test.jsp 复制下面的内容进去!
<%@ page contentType="text/html;charset=UTF-8" isELIgnored="false"%>
SessionID:<%=session.getId()%>
SessionIP:<%=request.getServerName()%>
SessionPort:<%=request.getServerPort()%>
<%
out.println("This is Tomcat Server 1111111");
%>