Lnmp 搭建(nginx,mysql,php,memcache,tomcat)

實驗環境:rhel6.5  seliunx and iptables disabled

nginx                  1.6.2

mysql                 5.5.12

php                   5.4.36

memcached           1.4.4

server1  172.25.12.1    tomcat     nginx    mysql     php              memcached

server2    172.25.12.2    tomcat    memcached

server3    172.25.12.3   tomcat

server4    172.25.12.4   tomcat

 

一.nginx

1.1安裝

安裝依賴性:yum install gcc openssl-devel pcre-devel  -y

解壓:tar  zxf nginx-1.6.2.tar.gz

編譯前稍作改動:

掩蓋版本號,爲了安全curl  -I IP/hostname 可查看服務器信息

root@server1core]# pwd

/pags/nginx-1.6.2/src/core

[root@server1core]# vim nginx.h

#definenginx_version      1006002

#defineNGINX_VERSION      "1.6.2"

#defineNGINX_VER          "nginx/" NGINX_VERSION

改爲

#definenginx_version      1006002

#defineNGINX_VERSION      "1.6.2"

#defineNGINX_VER          "nginx/"

關閉gccdebug功能,這樣編譯出來的內容會更小

[root@server1cc]# pwd

/pags/nginx-1.6.2/auto/cc

[root@server1cc]# vim  gcc

# debug

CFLAGS="$CFLAGS-g"

改爲:

# debug

#CFLAGS="$CFLAGS -g"

編譯安裝:

 ./configure--prefix=/usr/local/lnmp/nginx --with-http_ssl_module                                  --with-http_stub_status_module

 make  && make  install

 

1.2修改配置文件,加以優化

[root@server1nginx]# useradd -M -s /sbin/nologin nginx  #增加nginx用戶,以此身份運行

[root@server1nginx]# pwd

/usr/local/lnmp/nginx

[root@server1nginx]# vim conf/nginx.conf

user  nginx;                 #以此身份運行

worker_processes  2;   #默認啓動幾個進程

 

 

#error_log  logs/error.log;

#error_log  logs/error.log  notice;

#error_log  logs/error.log  info;

 

#pid        logs/nginx.pid;

 

 

events {

    use epoll;               #高效方法,用於Linux內核2.6以後版本;

    worker_connections  1024;

}

 

軟連命令到/usr/sbin 方便使用;或者寫在~/.bash_profile;source

[root@server1nginx]# pwd

/usr/local/lnmp/nginx

ln -s  /usr/local/lnmp/nginx/sbin/nginx  /usr/sbin/  #一定要寫絕對路徑

 

1.3檢測語法;啓動nginx;

[root@server1lnmp]# nginx  -t

nginx: theconfiguration file /usr/local/lnmp/nginx/conf/nginx.conf syntax is ok

nginx: configurationfile /usr/local/lnmp/nginx/conf/nginx.conf test is successful

[root@server1lnmp]# nginx

[root@server1lnmp]# netstat   -anltp |  grep 80

tcp        0     0 0.0.0.0:80                 0.0.0.0:*                    LISTEN     3595/nginx          

測試:

[root@server1html]# pwd

/usr/local/lnmp/nginx/html

[root@server1html]# echo "test nginx" > index.html

[root@foundation12~]# hostname

foundation12.ilt.example.com

[root@foundation12~]# curl 172.25.12.1

test nginx 成功

1.4查看nginx處理狀態:

[root@server1conf]# vim nginx.conf

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

        #

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }

 

        location  /status {

        stub_status  on;

        }

 

        # proxy the PHP scripts to Apachelistening on 127.0.0.1:80

 

[root@server1conf]# nginx   -s reload

在瀏覽器中輸入http://172.25.12.1/status

可以看到:

Active connections: 1

server accepts handled requests

 5 5 32

Reading: 0 Writing: 1 Waiting: 0

 

1.5配置https

生成證書: cd /etc/pki/tls/certs

[root@server1 certs]# make wxl_nginx.pem

umask 77 ; \

              ...................

Country Name (2 letter code) [XX]:cn

State or Province Name (full name) []:shaxi

Locality Name (eg, city) [Default City]:xi'an      

Organization Name (eg, company) [Default Company Ltd]:Linux-lover

Organizational Unit Name (eg, section) []:Linux-lovers

Common Name (eg, your name or your server's hostname)[]:server1.example.com

Email Address []:[email protected]

 

[root@server1 certs]# mv wxl_nginx.pem /usr/local/lnmp/nginx/conf/

[root@server1 conf]# vim nginx.conf

    server {

        listen       443 ssl;

        server_name  server1.example.com;

 

        ssl_certificate      wxl_nginx.pem;

       ssl_certificate_key  wxl_nginx.pem;

 

        ssl_session_cache    shared:SSL:1m;

       ssl_session_timeout  5m;

 

        ssl_ciphers  HIGH:!aNULL:!MD5;

       ssl_prefer_server_ciphers  on;

 

        location / {

            root   html;

            index  index.html index.htm;

        }

    }

 

 nginx -s reload

瀏覽器輸入https://172.25.12.1/  驗證

spacer.gif

1.6搭建虛擬主機:

   #   location / {

   #        root   html;

   #        index  index.html index.htm;

   #    }

   #}

 

       server {

                listen  80;

                server_name www.linux-lover.com;             #在主機中做好解析

                location / {

                        index index.html;

                        root/usr/local/lnmp/nginx/vhost/linux-lover;

                }

       }

 

 

    #HTTPS server

    #

[root@server1 nginx]# ls  -Rvhost/

vhost/:

linux-lover

 

vhost/linux-lover:

index.html

spacer.gif

1.7nginx實現負載均衡

   定義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"';

        upstream blance {

                server172.25.12.2:80   weight=2;  #添加權重。

                server172.25.12.3:80;

        }

 

    #access_log logs/access.log  main;

#使用上面定義的負載均衡

        server {

                listen 80;

                server_name www.loadblance.com;

                location / {

                proxy_pass http://blance;

                }

        }

1.8使用sticky模塊實現session粘滯

重新編譯 ./configure --prefix=/usr/local/lnmp/nginx --with-http_ssl_modul    --with-http_stub_status_module     --add-module=/usr/local/lnmp/nginx/nginx-sticky-module-1.0  #添加sticky模塊,直接加絕對路徑即可

                                                                                           

make && make install

 

注意此字段表示成功加進sticky 模板

adding module in /usr/local/lnmp/nginx/nginx-sticky-module-1.0/

 + ngx_http_sticky_module wasconfigured

可以發現之前的配置文件並未改變;

注意加sticky模板就不能加權重了;

nginx: [emerg] invalid parameter"weight=2" in /usr/local/lnmp/nginx/conf/nginx.conf:27

 

       upstream blance {

                sticky;

                server172.25.12.2:80;

                server172.25.12.3:80;

        }

效果就是,對於同一ip來源,讓其訪問它之前一直訪問的主機;而當其中一節點down之後nginx會調度其訪問online的節點;說明nginx具備健康檢查功能;


mysql安裝配置

2.1安裝:

安裝之前移除系統已經安裝的數據庫;

解決依賴性: yum install -y gcc gcc-c++ make ncurses-devel bison openssl-devel    zlib-devel cmake

[[email protected]]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/lnmp/mysql\ 

>  -DMYSQL_DATADIR=/usr/local/lnmp/mysql/data \#數據庫存放目錄

>  -DMYSQL_UNIX_ADDR=/usr/local/lnmp/mysql/data/mysql.sock  \  #套接字文件位置

>  -DWITH_MYISAM_STORAGE_ENGINE=1 \ 

>  -DWITH_INNOBASE_STORAGE_ENGINE=1 \

>  -DWITH_ARCHIVE_STORAGE_ENGINE=1 \

>  -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \

>  -DWITH_PARTITION_STORAGE_ENGINE=1 \

>  -DDEFAULT_CHARSET=utf8  \         #使用 utf8 字符

>-DDEFAULT_COLLATION=utf8_general_ci \  #校驗字符

>-DEXTRA_CHARSETS=all

make  && make  install

spacer.gif

重新編譯時,需要清除舊的對象文件和緩存信息

make clean

rm -f CmakeCache.txt

2.2初始化數據庫

爲了方便使用命令:在~/.bash_profile聲明mysql路徑;

# User specific environment and startup programs

 

PATH=$PATH:$HOME/bin:/usr/local/lnmp/mysql/bin

source ~/.bash_profile    #使之生效

建立mysql用戶:useradd  -M -s /sbin/nologin mysql

根據主機內存複製 mysql 配置文件;

[root@server1 support-files]# pwd

/usr/local/lnmp/mysql/support-files

 

cp my-medium.cnf  /etc/my.cnf   #之前的最好備份下

初始化數據庫

chown -Rmysql.mysql .

scripts/mysql_install_db --user=mysql--basedir=/usr/local/mysql --datadir=/usr/local/mysql/data

chown -R root .

chown -Rmysql.mysql data

cpsupport-files/mysql.server /etc/init.d/mysqld

2.3啓動;設祕密

[root@server1 mysql]# /etc/init.d/mysqld  start

Starting MySQL.. SUCCESS

[root@server1 support-files]#mysql_secure_installation

三.php安裝配置

3.1安裝mcrypt(加密擴展)

3.1.1安裝libmycrypt

tar  jxf libmcrypt-2.5.8.tar.bz2

cdlibmcrypt-2.5.8

./configure  --prefix=/usr/local/lnmp/php/modules/libmcrypt

make  && make install

cd libltdl/

./configure--prefix=/usr/local/lnmp/php/modules/libmcrypt/ --enable-ltdl-install

      添加系統動態連接庫:

vim/etc/ld.so.conf

includeld.so.conf.d/*.conf

/usr/local/lnmp/php/module/libmcrypt/lib/

刷新;查看

 ldconfig -v | grep libmcrypt

3.1.2安裝mhash

tar jxf mhash-0.9.9.9.tar.bz2

cd mhash-0.9.9.9

./configure--prefix=/usr/local/lnmp/php/modules/mhash

 make && make install

vim /etc/ld.so.conf

include ld.so.conf.d/*.conf

/usr/local/lnmp/php/module/libmcrypt/lib/

/usr/local/lnmp/php/modules/mhash/lib/

ldconfig -v | grep mhash

軟鏈到系統能查找的庫裏

ln -s /usr/local/lnmp/php/module/mhash/lib/* /usr/local/lib/

ln -s /usr/local/lnmp/php/module/mhash/include/* /usr/local/include/

3.1.3安裝mcrypt

 ./configure    --prefix=/usr/local/lnmp/php/modules/mcrypt      --with-libmcrypt-prefix=/usr/local/lnmp/php/modules/libmcrypt/

也可以直接安裝在系統的庫裏面方便查找

3.2安裝php

./configure--prefix=/usr/local/lnmp/php/ --with-config-file-path=/usr/local/lnmp/php/etc--with-mysql=/usr/local/lnmp/mysql/  --with-mysqli=/usr/local/lnmp/mysql/bin/mysql_config --with-openssl-with-gd --with-zlib --with-curl --enable-bcmath --with-libxml-dir --with-png-dir  --with-jpeg-dir--with-freetype-dir  --without-pear  --with-gettext  --with-gmp --enable-inline-optimization   --enable-soap --enable-ftp --enable-sockets--enable-mbstring --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx--with-mcrypt=/usr/local/lnmp/php/modules/libmcrypt/  --with-mhash

 make &&  make install

 

3.3PHPnginx整合

創建三個文件:

[root@server1 php-5.4.36]# pwd

/pags/php-5.4.36

配置文件:cp php.ini-production  /usr/local/lnmp/php/etc/php.ini

啓動腳本: cpsapi/fpm/init.d.php-fpm  /etc/init.d/fpm

chmod +x /etc/init.d/fpm

fpm配置文件: cpphp-fpm.conf.default php-fpm.conf

spacer.gif

啓動php  /etc/init.d/fpm start

修改nginx配置,測試php

        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.conf;

        }

寫入測試文件cat htmls/index.php

<?php

phpinfo()

?>

 nginx -s reload

修改php的時間;不然nginx日誌會有警告:

vim/usr/local/lnmp/php/etc/php.ini

[Date]

; Defines thedefault timezone used by the date functions

;http://php.net/date.timezone

date.timezone =Asia/Shanghai

 

 

四.lnmp平臺簡單測試:搭建論壇

 unzip  Discuz_X3.2_SC_UTF8.zip   -d /usr/local/lnmp/nginx/html/

只有upload有用;改名bbs   mv upload/ bbs/

 chmod   777  config/  -R

       chmod   777  data/  uc_*  -R

瀏覽器輸入http://172.25.12.1/bbs/install進行安裝;

mysql要開啓 

修改nginx配置文件:

        location / {

            root   html;

            index  index.html index.htm index.php;

        }

五.添加memcache模塊

PHP 擴展的 Memcache 實際上是連接 Memcache 的方式

 

爲了方便使用php有關命令:

vim ~/.bash_profile

PATH=$PATH:$HOME/bin:/usr/local/lnmp/mysql/bin:/usr/local/lnmp/php/bin

 source  ~/.bash_profile

 

安裝php擴展memcachetar zxf  memcache-2.2.5.tgz

生成configure文件  [root@server1 memcache-2.2.5]# phpize

編譯安裝 ./configure  --prefix=/usr/local/lnmp/php/modules/memcache --with-php-config=/usr/local/lnmp/php/bin/php-config

[root@server1 memcache-2.2.5]#make && make install

Installing shared extensions:                /usr/local/lnmp/php/lib/php/extensions/no-debug-non-zts-20100525/

檢測:ll /usr/local/lnmp/php/lib/php/extensions/no-debug-non-zts-20100525/

-rwxr-xr-x 1 root root 259040 Apr 3 08:03 memcache.so

    使php加載該模塊

[root@server1etc]# pwd

/usr/local/lnmp/php/etc

[root@server1etc]# vim php.ini

 

; ... or under UNIX:

;

;   extension=msql.so

 extension=memcache.so

重載生效:/etc/init.d/fpm relaod

檢測是否加入:

瀏覽器輸入 http://172.25.12.1/index.php 查看是否有memcache模板

spacer.gif

而數據真正緩存在memcached服務端當中,上面所做的只是讓php連接進memcache,使用它。所以下面真正安裝memcache

安裝memcached軟件:yum install  memcached  -y

按照生產環境修改配置文件:vim /etc/sysconfig/memcached

PORT="11211"

USER="memcached"

MAXCONN="1024"

CACHESIZE="64"

OPTIONS="

啓動服務:/etc/init.d/memcached restart

 

pwd  /root/memcache-2.2.5

cp  memcache.php   example.php  /usr/local/lnmp/nginx/html/

vim  /usr/local/lnmp/nginx/html/memcache.php

 

$VERSION='$Id: memcache.php,v 1.2 2008/09/11 19:21:06 mikl Exp $';

 

define('ADMIN_USERNAME','wxl');         // Admin Username

define('ADMIN_PASSWORD','redhat');      // Admin Password

define('DATE_FORMAT','Y/m/d H:i:s');

define('GRAPH_SIZE',200);

define('MAX_ITEM_DUMP',50);

 

$MEMCACHE_SERVERS[] = '172.25.12.1:11211';// add more as an array #memcache緩存服務器地址

$MEMCACHE_SERVERS[] = '172.25.12.2:11211';// add more as an array

telnet測試下memcache

 telnet   localhost 11211

stats

使用測試文件測試:

[root@server1 memcache-2.2.5]# cat /usr/local/lnmp/nginx/html/test.php

<?php

$memcache = new Memcache;

$memcache->connect('172.25.12.2',11211) or die ("Could not connect");

$version = $memcache->getVersion();

echo "Server's version: ".$version."\n";

$tmp_object = new stdClass;

$tmp_object->str_attr = 'test';

$tmp_object->int_attr = 123;

$memcache->set('key', $tmp_object, false, 10) or die ("Failedto save data at the

server");

echo "Store data in the cache (data will expire in 10seconds)\n";

$get_result = $memcache->get('key');

echo "Data from the cache:\n";

var_dump($get_result);

?>

瀏覽器訪問 http://172.25.12.1/test.php不斷刷新

訪問http://172.25.12.1/memcache.php 檢測麼麼擦車命中率

spacer.gif

 

六.搭建jsp平臺

6.1搭建java運行平臺:

下載安裝;運行  chmod +x jdk-6u32-linux-x64.bin

sh jdk-6u32-linux-x64.bin 

之後會生成一個包 mv jdk1.6.0_32/  /usr/local/lnmp/jdk

修改環境變量 vim  /etc/profile

unset -f pathmunge

export JAVA_HOME=/usr/local/lnmp/jdk

export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib

export PATH=$PATH:$JAVA_HOME/bin

source /etc/profile

檢測:

[root@server1 ~]# cat test.java

public class test

{

       public staticvoid  main(String[] args)

       {

              System.out.println("testjava");

       }

}

編譯:javactest.java

運行:[root@server1 ~]# java test  #後面不接什麼。自動補齊

test java

6.2安裝tomcat

 tar  zxf apache-tomcat-7.0.37.tar.gz

 mvapache-tomcat-7.0.37 /usr/local/lnmp/tomcat

 pwd   /usr/local/lnmp/tomcat/

啓動: cd bin/ ;  ./startup.sh

檢測:ss -antpl | grep 8080

瀏覽訪問 http://172.25.12.1:8080/

6.3tomact部署在nginx的後端

tomcat的端口是8080 nginx的端口是80  用戶自己是不會輸入端口號的。所以請求先到       nginxnginx再交給tomacat

創建測試文件:[root@server1 ROOT]# cattest.jsp

 Time  is : <%= new java.util.Date()

 nginx配置文件:

        location ~ \.jsp$ {

            proxy_pass   http://127.0.0.1:8080;

        }

    6.4 tomcatnginx發佈目錄統一起來:

pwd    /usr/local/lnmp/tomcat

vimconf/server.xml

spacer.gif

改爲    <Hostname="localhost"  appBase="/usr/local/lnmp/nginx/html"

            unpackWARs="true"autoDeploy="true">

                        <Context path=""docBase="" debug="0"/>

重啓tomcat

 ./bin/shutdown.sh

 ./bin/startup.sh

測試:把test.jsp 移動到/usr/local/lnmp/nginx/html;

 

 

6.5nginx實現tomcat後端的負載均衡

server1server2 測試;同步server1的配置到sever2

scp  -r tomcat/  jdk/   172.25.12.2:/usr/local/lnmp/

scp /etc/profile 172.25.12.2:/etc/profile

server2上:source /etc/profile

因爲之前在server1上的tomcat默認目錄被改過,現在server2上沒有之前的目錄;所以改過來:

vim conf/server.xml

      <Hostname="localhost" appBase="webapps"

           unpackWARs="true" autoDeploy="true">

        <!--Context path=""docBase="" debug="0"/-->

./bin/startup.sh

scp測試文件:scp  test.jsp  172.25.12.2:/usr/local/lnmp/webapps/ROOT

server1. server2 上的測試文件應做相應的改動;便於負載均衡測試。

配置server1 nginx

#tomcat loadblance

        upstream tomcat {

                server 172.25.12.1:8080;

                server 172.25.12.2:8080;

        }

 

#       location ~ \.jsp$ {

#            proxy_pass   http://127.0.0.1:8080;

#        }

#

        location ~ \.jsp$ {

            proxy_pass   http://tomcat;

        }

6.6使用memcached保持session會話

 

爲什麼要保持session會話;

會話保持是指在負載均衡器上有一種機制,在作負載均衡的同時,還保證同一用戶相關連的訪問請求會被分配到同一臺服務器上;

說明:

如果有一個用戶訪問請求被分配到服務器A,並且在服務器A登錄了,並且在很短的時間,這個用戶又發出了一個請求,如果沒有會話保持功能的話,這個用戶的    請求很有可能會被分配到服務器B去,這個時候在  服務器B上是沒有登錄的,所以你要重新登錄,但是用戶並不知道自己的請求被分配到了哪裏,用戶的感覺就是登錄了,怎麼又要登錄,用戶體驗很不好。

還有你在淘寶上面買東西,從登錄->拍得東西->添加地址->付款,這是一個一系    列的過程,應當由一臺服務器完成,而不能被負載均衡器分配到不同的服務器上。

會話保持都會有時間的限制(映射到固定某一臺的服務器除外,如:ip_hash,  種負載均衡工具都會提供這種會話保持時間的設置,LVSapache等。連php語言都提供了會話保持時間的設定        session.gc_maxlifetime

web集羣時session同步的3種方法:

1.利用cookie同步session

session是文件的形式存放在服務器端的,cookie是文件的形式存在客戶端的。當客戶端瀏覽器向某個站點第一次發送請求的時候,WEB服務器這個時候,會判斷是否是第一次請求,如果確定是第一次,那麼web服務器會自己生成一個Session_Id,保存到內存中。並且在返回響應頭(Response Header)的時候,會把這個Session_Id帶回客戶端(如瀏覽器),這時候瀏覽器接收到sessionId後,會把它保存在客戶端cookie。第二次請求站點的時候,會把Session_Id封裝到request中去。這樣服務器端就能判斷這個是已建立的會話還是新的會話。怎麼實現同步呢?方法很簡單,就是把用戶訪問頁面產生的session放到cookie裏面,就是以cookie爲中轉站。你訪問web服務器A,產生了session把它放到cookie裏面了,你訪問被分配到web服務器B,這個時候web服務器B先判斷服務器有沒有這個session,如果沒有,在去看看客戶端的cookie裏面有沒有這個session,如果也沒有,說明session真的不存,如果cookie裏面有,就把cookie裏面的sessoin同步到web服務器B,這樣就可以實現session的同步了。

說明:這種方法實現起來簡單,方便,也不會加大數據庫的負擔,但是如果客戶端cookie禁掉了的話,那麼session就無從同步了,這樣會給網站帶來損失;cookie安全性不高,雖然它已經加了密,但是還是可以僞造的;

2.利用memcache同步session

memcache可以做分佈式,如果沒有這功能,他也不能用來做session同步。他可以把 web服務器中的內存組合起來,成爲一個"內存池",不管是哪個服務器產生的sessoin都可以放到這個"內存池"中,其他的都可以使用。

優點:以這種方式來同步session,不會加大數據庫的負擔,並且安全性比用cookie   大的提高,把session放到內存裏面,比從文件中讀取要快很多。

缺點:memcache把內存分成很多種規格的存儲塊,有塊就有大小,這種方式也就決定  了,memcache不能完全利用內存,會產生內存碎片,如果存儲塊不足,還會產生內存溢出。

3.利用數據庫同步session:影響系統性能,不推薦使用;

 

6.6.1創建測試網頁:

[root@server1 html]# cat test.jsp

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

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

<html><head><title>Cluster AppTest</title></head>

<body>

Server Info:

<%

out.println(request.getLocalAddr() + " : " +request.getLocalPort()+"<br>");%>

<%

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

String dataName = request.getParameter("dataName");

if (dataName != null && dataName.length() > 0) {

String dataValue = request.getParameter("dataValue");

session.setAttribute(dataName, dataValue);

}

out.print("<b>Session list</b>");

Enumeration e = session.getAttributeNames();

while (e.hasMoreElements()) {

String name = (String)e.nextElement();

String value = session.getAttribute(name).toString();

out.println( name + " = " + value+"<br>");

System.out.println( name + " = " + value);

}

%>

<form action="test.jsp" method="POST">

name:<input type=text size=20 name="dataName">

<br>

key:<input type=text size=20 name="dataValue">

<br>

<input type=submit>

</form>

</body>

</html>

 

6.6.2配置msmmemcached-session-manager)創建交叉存儲,避免但點故障;

原理:

 



spacer.gif


Tomcat-1 (T1) session 存儲在 memcached-2 (T2)上。只有當 M2 不可用時,T1 纔將        session存儲在memcached-1 (M1 T1 failoverNode)。使用這種配置的好處是, T1        M1 同時崩潰時也不會丟失 session 會話,避免單點故障。

 

nginx加入sticky;粘滯會話

 

配置tomcat使用msm

<ManagerclassName="de.javakaffee.web.msm.MemcachedBackupSessionManager"

memcachedNodes="n1:172.25.12.1:11211,n2:172.25.12.2:11211"

failoverNodes="n1"#server2上改成n2

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

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

/>

 

</Context>

 

重啓tomcat: ./bin/shutdown.sh;  ./bin/startup.sh

 

日誌文件中出現下面的內容代表memcache成功:

INFO:MemcachedSessionService finished initialization, sticky true, operation timeout1000,        with node ids [n2] andfailover node ids [n1]

效果是即使server1tomcat down掉或者memcached down 或者都down掉。會話都能保持;

 

另外測試如果加入4個節點tomcattomcatdown掉會話依然正常;

server3:

 

<ManagerclassName="de.javakaffee.web.msm.MemcachedBackupSessionManager"

memcachedNodes="n1:172.25.12.1:11211,n2:172.25.12.2:11211"

failoverNodes="n1"  #server4改成n2

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

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

/>

 

 

spacer.gif


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章