Nginx+Tomcat+Memcache 实现session共享

Nginx + Tomcat + Memcache 实现session共享

1. Nginx 部署

1.上传源码包到服务器,解压安装
下载地址:http://nginx.org/en/download.html

2.安装依赖包
yum install pcre pcre-devel openssl openssl-devel gcc gcc-c++ -y

3.解压安装

tar zxvf nginx-1.10.3.tar
cd nginx-1.10.3
编译(具体的模块可以自行添加)
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module && make && make install

4.添加PHP解析(前提是已经安装了PHP环境)

编辑nginx.conf文件,将下面内容取消注释
location ~ \.php$ {
        root           html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /home/work/nginx/html$fastcgi_script_name;
        include        fastcgi_params;
    }

5.启动
/usr/local/nginx/sbin/nginx
nginx默认没有启动脚本(可以自行编写一个)
将/usr/local/nginx/sbin/nginx加在/etc/rc.local中,让其开机自启

2.Tomcat部署

1.上传源码包到服务器,直接解压安装
下载地址:http://tomcat.apache.org/

2.解压到指定目录即可
tar zxvf apache-tomcat-7.0.57.tar.gz -C /usr/local/

3.启动

bin/startup.sh

下面是一个启动脚本(直接执行 sh tomcatctrl.sh start|stop) 可以启停tomcat
#!/bin/sh
# Tomcat init script for Linux.
#
# description: The Apache Tomcat servlet/JSP container.
#JAVA_HOME=/home/work/jdk/jdk1.8.0_20/
#CATALINA_HOME=/home/work/rap_tomcat8/
CATALINA_HOME=/home/work/tomcat_dianshang
export JAVA_HOME CATALINA_HOME
#exec $CATALINA_HOME/bin/catalina.sh $*
case $1 in
start)
  sh $CATALINA_HOME/bin/catalina.sh start && tail -f logs/*;;
stop)
  sh $CATALINA_HOME/bin/catalina.sh stop ;;
restart)
  $CATALINA_HOME/bin/catalina.sh stop
  sleep 5
  sh $CATALINA_HOME/bin/catalina.sh start&& tail -f logs/*;;
*)
  echo "Usage: `basename $0` (start|stop|restart)"
  exit 1
  ;;
esac

3.Memcache部署

1.上传源码包到服务器
下载地址:http://memcached.org/downloads

memcache需要libevent的支持,所以也需要下载libevent
下载地址:http://libevent.org/

2.解压安装

首先安装libevent
tar -zxvf libevent-2.0.21-stable.tar.gz
cd libevent-2.0.21-stable
./configure --prefix=/usr/local/libevent
make && make install

接着安装memcache
tar -zxvf memcached-1.5.4.tar.gz
cd memcached-1.5.4
./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent
make && make install

3.启动
bin/memcached -p 11211 -m 64m -d

4.查看端口和进程,确定是否启动成功

4.配置session共享

1.首先需要配置nginx,将请求转发到tomcat上面
其中vhosts是我自己新建的,为了配置虚拟主机,管理方便
cd /home/work/nginx/vhosts
vim test.conf
server{
listen    80 default;
server_name dianshang_server;
access_log  logs/dianshang.log  main;
location / {
        proxy_pass http://dianshang_server;
    }
}

upstream dianshang_server{
     server 192.168.1.211:8091;
}
接着修改nginx.conf的配置,让其加载我们刚才配置的虚拟主机
cd /home/work/nginx/conf
vim nginx.conf
include /home/work/nginx/vhosts/*.conf;

在这贴一个nginx的主配置文件
user work;
worker_processes 8;
worker_rlimit_nofile 65535;
error_log  logs/error.log  info; 
pid        logs/nginx.pid;

events {
	use epoll;
	worker_connections  20480;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    log_format main '$remote_addr - $remote_user [$time_local]  '
                    '"$request" [$request_uri]  $status $body_bytes_sent $request_body  '
                    '"$http_referer"  "$http_user_agent" $http_x_forwarded_for  '; 

    sendfile        on;
    keepalive_timeout  65;
    client_body_timeout 15;
    send_timeout 25; 
    client_max_body_size 8m;
    server_tokens on; #隐藏nginx版本信息

    server {

    	location ~* \.(.php|.html)$ {
            root   html;
            index  index.html index.htm index.php index.jsp;
        }
        
    	location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /home/work/nginx/html$fastcgi_script_name;
            include        fastcgi_params;
        }

    }

    fastcgi_connect_timeout 30;
    fastcgi_send_timeout 30;
    fastcgi_read_timeout 30;
    fastcgi_buffer_size 64k;
    fastcgi_buffers 4 64k;
    fastcgi_busy_buffers_size 128k;
    fastcgi_temp_file_write_size 128k;

    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 16k;
    gzip_http_version 1.1;
    gzip_comp_level 2;
    gzip_types text/plain  text/css application/x-javascript application/xml;

    include /home/work/nginx/vhosts/*.conf;

}

2.tomcat配置
编辑context.xml文件,追加下面的内容
 <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
                     sticky="false"
                     lockingMode="auto" 
                     memcachedNodes="n1:192.168.1.211:11211  n2:192.168.1.212:11211" 
                     requestUriIgnorePattern=".*\.(png|gif|jpg|css|js)$"      
                     sessionBackupAsync="false"      
                     sessionBackupTimeout="100"      
                   transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"
                     copyCollectionsForSerialization="false"  />

3.添加相应的包

添加到tomcat中的lib下
memcached-session-manager-1.8.1.jar
memcached-session-manager-tc7-1.8.1.jar
msm-flexjson-serializer-1.8.1.jar
msm-javolution-serializer-1.8.1.jar
msm-kryo-serializer-1.8.1.jar
msm-serializer-benchmark-1.8.1.jar
msm-xstream-serializer-1.8.1.jar

4.新建一个获取session的页面
在tomcat下的webapps/ROOT下新建个session.jsp
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">  
<head>  
<title><%= application.getServerInfo() %></title>  
</head>  
<body>  
   当前的session <%=session.getId()%>  
当前主机 <%=request.getLocalAddr()%>  
</body> 

4.访问测试,如果session一样说明OK
posted @ 2019-06-18 17:55  于欢水  阅读(361)  评论(0编辑  收藏  举报