码迷,mamicode.com
首页 > 系统相关 > 详细

nginx+memcached+tomcat集群 session共享完整版

时间:2015-07-30 14:55:30      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:tomcat   memcached   nginx   session共享   

集群环境

1.nginx版本 nginx-1.6.2.tar.gz

2.jdk 版本 jdk-7u21-linux-x64.tar.gz

3.tomcat 版本  7.0.29

4.memcached 版本 memcached-1.4.22.tar.gz

5. CentOS 6.5 系统采用一台服务做测试

 

一、nginx安装

安装依赖包 yum -y install gcc gcc-c++

1.安装pcre

tar zxvf pcre-8.36.tar.gz

cd pcre-8.36

./configure

make && make install

2.安装zlib

tar zxvf zlib-1.2.8.tar.gz

cd zlib-1.2.8

./configure

make && make install

3.安装openssl

wget http://www.openssl.org/source/openssl-1.0.1c.tar.gz

tar zxvf openssl-1.0.1c.tar.gz

4.安装nginx 注意指定prce zlib openssl原码包位置

tar zxvf nginx-1.6.2.tar.gz

 

cd nginx-1.6.2

 

./configure --prefix=/usr/local/nginx \

 --with-http_ssl_module \

 --with-pcre=/root/pcre-8.36 \

 --with-zlib=/root/zlib-1.2.8  \

--with-openssl=/root/openssl-1.0.1c

 

make && make install

5.安装成功后配置

vi /usr/local/nginx/conf/nginx.conf 修改配置文件

 

#user  nobody;

worker_processes  2;

 

#error_log  logs/error.log;

#error_log  logs/error.log  notice;

#error_log  logs/error.log  info;

 

#pid        logs/nginx.pid;

 

worker_rlimit_nofile 65535;

events {

   use epoll;

    worker_connections  65535;

}

 

 

http {

    include       mime.types;

    default_type  application/octet-stream;

 

    #log_format  main  ‘$remote_addr - $remote_user [$time_local] "$request" ‘

    #                  ‘$status $body_bytes_sent "$http_referer" ‘

    #                  ‘"$http_user_agent" "$http_x_forwarded_for"‘;

 

    #access_log  logs/access.log  main;

 

    sendfile        on;

    #tcp_nopush     on;

 

    #keepalive_timeout  0;

    keepalive_timeout  150;

    fastcgi_connect_timeout 300;

         fastcgi_send_timeout 300;

         fastcgi_read_timeout 300;

    #gzip  on;

    

                    gzip  on;

                    

        

           ##cache##

           client_max_body_size 10m;

           client_body_buffer_size 1m;

           proxy_connect_timeout 5;

           proxy_read_timeout 60;

           proxy_send_timeout 5;              

           proxy_buffer_size 64k;

           proxy_buffers 4 128k;

           proxy_busy_buffers_size 128k;

           proxy_temp_file_write_size 1m;

           proxy_temp_path /home/temp_dir;

           proxy_cache_path /home/cache levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;

           ##end##

 

 

 

 

    #server {

     #   listen       80;

     #   server_name  localhost;

 

        #charset koi8-r;

 

        #access_log  logs/host.access.log  main;

 

      #  location / {

       #     root   html;

        #    index  index.html index.htm;

       # }

 

        #error_page  404              /404.html;

 

        # redirect server error pages to the static page /50x.html

        #

        #error_page   500 502 503 504  /50x.html;

        #location = /50x.html {

         #   root   html;

        #}

 

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80

        #

        #location ~ \.php$ {

        #    proxy_pass   http://127.0.0.1;

        #}

 

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

        #

        #location ~ \.php$ {

        #    root           html;

        #    fastcgi_pass   127.0.0.1:9000;

        #    fastcgi_index  index.php;

        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;

        #    include        fastcgi_params;

        #}

 

        # deny access to .htaccess files, if Apache‘s document root

        # concurs with nginx‘s one

        #

        #location ~ /\.ht {

        #    deny  all;

        #}

    #}

 

 

    # another virtual host using mix of IP-, name-, and port-based configuration

    #

    #server {

    #    listen       8000;

    #    listen       somename:8080;

    #    server_name  somename  alias  another.alias;

 

    #    location / {

    #        root   html;

    #        index  index.html index.htm;

    #    }

    #}

 

 

    # HTTPS server

    #

    #server {

    #    listen       443;

    #    server_name  localhost;

 

    #    ssl                  on;

    #    ssl_certificate      cert.pem;

    #    ssl_certificate_key  cert.key;

 

    #    ssl_session_timeout  5m;

 

    #    ssl_protocols  SSLv2 SSLv3 TLSv1;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;

    #    ssl_prefer_server_ciphers   on;

 

    #    location / {

    #        root   html;

    #        index  index.html index.htm;

    #    }

    #}

 

                  upstream wazert.com {

            server 127.0.0.1:8080;  #tomcat1后面安装配置

                    server 127.0.0.1:8090;   #tomcat2

         }

                  

        server {

       listen  80;

       server_name www.test1.com; #域名

       location / {

           root   html;

            index  index.html index.htm;

            proxy_pass http://wazert.com;

            #       proxy_set_header  X-Real-IP  $remote_addr;

                            proxy_set_header        Host $host;

            proxy_set_header        X-Real-IP $remote_addr;

            proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;

 

 

        }

        location ~ .*\.(gif|jpg|png|htm|html|css|ico|flv|swf)(.*) {

              proxy_pass http://wazert.com;

              proxy_redirect off;

              proxy_set_header Host $host;

              proxy_cache cache_one;

              proxy_cache_valid 200 302 1h;

              proxy_cache_valid 301 1d;

              proxy_cache_valid any 10m;

              expires 30d;

                              proxy_cache_key $host$uri$is_args$args;

        }

        }

}

 

 

二、memcached安装

yum -y install libevent libevent-devel

tar zxvf memcached-1.4.22.tar.gz

cd memcached-1.4.22

./configure --prefix=/usr/local/memcached

make && make install

 

启动memcached ip172.19.0.102 根据自己的环境端口11211可以根据自己需要修改不同端口

 

/usr/local/memcached/bin/memcached -d -m 1024 -u root -l 172.19.0.102 -p 11211 -c 1024 -P /tmp/memcached.pid

 

测试一下如下说明成功

telnet 172.19.0.102 11211

 

Trying 172.19.0.102...

Connected to 172.19.0.102.

Escape character is ‘^]‘.

 

三、安装jdk

tar zxvf jdk-7u21-linux-x64.tar.gz

mv  jdk1.7.0_21 /opt/jdk

配置vi /etc/profile 增加到环境变量中

在最后面增加

 

JAVA_HOME=/opt/jdk

PATH=$JAVA_HOME/bin:$PATH

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

export JAVA_HOME

export PATH

export CLASSPATH

 

生效环境变量

source /etc/profile 

 

java -version 查看是版本是否生效

java version "1.7.0_21"

Java(TM) SE Runtime Environment (build 1.7.0_21-b11)

Java HotSpot(TM) 64-Bit Server VM (build 23.21-b01, mixed mode)

 

四、安装与配置tomcat

1.安装tomcat

tar zxvf tomcat7.tar.gz

mv apache-tomcat-7.0.29 /opt/tomcat1

2.添加memmsm的依赖jar


把包下载后上传/opt/tomcat1/lib/ 目录下

 

3、配置tomcat

cd  /opt/tomcat1/conf

vi context.xml

增加<Context></Context>之间      注意:172.19.0.102 11211 memcached端口

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

                memcachedNodes="n1:172.19.0.102:11211"

                requestUriIgnorePattern=".*/.(png|gif|jpg|css|js|swf|flv)$"

                sessionBackupAsync="false"

                sessionBackupTimeout="100"

                transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"

                copyCollectionsForSerialization="false"/>

 

修改vi server.xml

<!-- You should set jvmRoute to support load-balancing via AJP ie :

    <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">

    -->

    <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">

五、配置tomcat集群上面配置好后拷贝一份

cp  -r  tomcat1  tomcat2

cd tomcat2/conf/

修改vi server.xml配置把端口配置不同的

1. 找到如下把8005修改成8006

-->

<Server port="8005" shutdown="SHUTDOWN">

2.找到如下把8080修改成8090根据自己来配置,修改后的8090nginx配置一样

   -->

    <Connector port="8090" protocol="HTTP/1.1"

               connectionTimeout="20000"

               redirectPort="8443" />

3.找到如下把8009修改成8010

<!-- Define an AJP 1.3 Connector on port 8009 -->

    <Connector port="8010" protocol="AJP/1.3" redirectPort="8443" />

4. 找到如下把tomcat1修改成tomcat2

-->

    <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat2">

 

六、启动tomcat集群

启动tomcat1

sh /opt/tomcat1/bin/startup.sh

启动tomcat2

sh /opt/tomcat2/bin/startup.sh

 

netstat -tunlp查看tomcat集群启动成功

tcp        0      0 :::8080                     :::*                        LISTEN      22911/java

tcp        0      0 :::8090                     :::*                        LISTEN      22946/java 

查看80808090说明成功

七、启动 nginx

/usr/local/nginx/sbin/nginx 启动

/usr/local/nginx/sbin/nginx -s reload  重启

测试session是否共享

1.cd /opt/tomcat1/webapps/ROOT/

mkdir test

cd test

vi index.jsp

 

<%@ page contentType="text/html; charset=GBK" %>

 

<%@ page import="java.util.*" %>

 

<html><head><title>Cluster Test</title></head>

 

<body>

 

<%

 

  //HttpSession session = request.getSession(true);

 

  System.out.println(session.getId());

 

  out.println("<br> SESSION ID:" + session.getId()+"<br>");

 

%>

 

</body>

 

2.cd /opt/tomcat2/webapps/ROOT/ tomcat1 test目录拷贝过来

cp -r /opt/tomcat1/webapps/ROOT/test ./

 

打开网页

刷新几次看看session iD不变,就成功了,如果变了可能配置问题。



版权声明:本文为博主原创文章,未经博主允许不得转载。

nginx+memcached+tomcat集群 session共享完整版

标签:tomcat   memcached   nginx   session共享   

原文地址:http://blog.csdn.net/u010530506/article/details/47149085

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!