詳解之:linux下tomcat、nginx的負載平衡及memcached對session共用的實現配置詳細總結

來源:互聯網
上載者:User

寫在前面:
最近幾天一直做nginx的負載平衡,折騰了將近一周,在網上查了很多資料,終於在今天將一系列的流程及功能跑通。在學習的過程中,發現網上大多數資料都寫的很籠統,或者就是寫的讓新手迷迷糊糊,似懂非懂,很多的配置參數等都不是很明白,配置的時候也就造成很多地方不確切。現在我對我這幾天的配置學習予以總結,從下載到安裝、環境的搭建,以及負載平衡的配置,參數的詳細解釋,以及memcached的session共用,都會一一介紹,並保證所用的代碼和配置都經過本人的測試並實現功能,希望給予對亟需解決linux+tomcat+nginx+memcached實現負載平衡以及session共用的同行特別是新手有一些協助。

一、環境的介紹:

作業系統採用: CentOS-5.6-i386

 項目部署在:tomcat6.0,tomcat一共有兩個,分別命名為tomcat6.0_1、tomcat.0_2.這兩台機器都運行在一台伺服器上,IP為192.168.128.129

 負載平衡採用版本:nginx-1.2

session的管理:memcached-1.4.15

由於資源有限,所有的程式安裝測試均在一台伺服器上完成。

二、jdk的環境的搭建tomcat的安裝與項目的部署:

1、具體的部署請參考我前兩天的文章:http://blog.csdn.net/jessonlv/article/details/7987434,所有的代碼都通過測試。按照上面的方式分別安裝部署兩個tomcat,並分別命名為tomcat6.0_1、tomcat6.0_2.

2、這裡重點說下文章之外的配置:由於兩個tomcat運行在一台伺服器上,所以要分別修改tomcat的連接埠,故修改/conf/server.xml,讓其能在一台機器上運行。具體代碼如下:

tomcat6.0_1配置:

<Server port="18005" shutdown="SHUTDOWN"><Connector port="18080" protocol="HTTP/1.1"                connectionTimeout="20000"                redirectPort="8443" /><Connector port="18009" protocol="AJP/1.3" redirectPort="8443" /><Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">

tomcat6.0_2配置:

<Server port="18006" shutdown="SHUTDOWN"><Connector port="18081" protocol="HTTP/1.1"                connectionTimeout="20000"                redirectPort="8443" /><Connector port="18010" protocol="AJP/1.3" redirectPort="8443" /><!--此處同上為修改Engine節點的jvmRoute為tomcat2--><Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat2">

 至此,一台伺服器上的兩個tomcat配置完畢。

三、nginx的安裝及負載平衡的配置:

nginx的安裝一般採用兩種方式,一種是yum命令的安裝,第二種是下載tar.gz包,採用編譯的方式安裝。

第一種比較簡單,但是一般伺服器都不支援yum命令,在此做簡單介紹:

 01.1、rpm -Uvh

http://nginx.org/packages/centos/5/noarch/RPMS/nginx-release-centos-5-0.el5.ngx.noarch.rpm

 01.2、執行安裝:yum安裝nginx:
yum --disablerepo=* --enablerepo=base --enablerepo=nginx install nginx
 此安裝方式有可能需要安裝gcc編譯器:gcc安裝:下載:http://www.linuxfromscratch.org/blfs/view/5.1/general/gcc2.html 

安裝命令:yum install gcc gcc-c++ kernel-devel

第二種安裝方式是重點:

01、下載所需的安裝包:

下載nginx:http://nginx.org/en/download.html

下載pcre,安裝pcre,nginx支援重寫:ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/

下載zlib:http://download.csdn.net/detail/jessonlv/4603322

下載gcc:http://download.csdn.net/detail/jessonlv/4603333

Nginx依賴一些其他PCRE、openssl(依賴libssl-dev),甚至是zlib、gcc等,下面我們簡單說下如何安裝PCRE:

tar zxvf  pcre-8.01.tar.gzcd pcre-8.01./configuremakemake install

測試pcre是否安裝成功:

[root@localhost conf]# rpm -qa pcrepcre-6.6-6.el5[root@localhost conf]# 

返回安裝的pcre的版本好則表示成功。或者:

[root@localhost conf]# ls -al /usr/lib | grep pcrelrwxrwxrwx   1 root root       19 Sep 19 13:11 libpcrecpp.so.0 -> libpcrecpp.so.0.0.0-rwxr-xr-x   1 root root    29608 Mar  6  2011 libpcrecpp.so.0.0.0lrwxrwxrwx   1 root root       21 Sep 19 13:11 libpcreposix.so.0 -> libpcreposix.so.0.0.0-rwxr-xr-x   1 root root     6616 Mar  6  2011 libpcreposix.so.0.0.0[root@localhost conf]# 

也是成功。
安裝nginx時如需其他依賴庫,上面都注有,安裝方式大致相同,在此不再都一一贅述。
02、安裝完成後,接下來所要做的就是nginx最核心的負載平衡的配置:

配置目錄是nginx安裝目錄下的/conf/nginx.conf檔案。下面張貼我的設定檔:

#user  nobody;worker_processes  1;#error_log  /usr/local/nginx/logs/error.log;#error_log  /usr/local/nginx/logs/error.log  notice;#error_log  /usr/local/nginx/logs/error.log  info;#pid        /usr/local/nginx/logs/nginx.pid;events {use epoll;    worker_connections  1024;}http {    include      /etc/nginx/mime.types;    default_type  application/octet-stream;    sendfile        on;    #keepalive_timeout  0;    keepalive_timeout  65;        gzip  on;    gzip_min_length 1k;    gzip_buffers    4  4k;    gzip_http_version  1.1;    gzip_comp_level 1;    gzip_types   test/plain application/x-javascript text/css application/xml;    gzip_proxied any;    gzip_disable "MSIE[1-6]\.(?!.*SV1)";    upstream www.ijietu.com{server 192.168.128.129:18081 weight=2;server 192.168.128.129:18080 weight=2;}    server {        listen       80;        server_name  www.ijietu.com;        #charset koi8-r;        access_log  /var/log/nginx/www.ijietu.com.log;        location / {        proxy_pass  http://www.ijietu.com;        proxy_set_header Host   $host;        proxy_set_header X-Real-IP  $remote_addr;        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;            #root   html;            #index  index.html index.htm;        }      log_format  www_ijietu_com  '$remote_addr - $remote_user [$time_local] $request '              '"$status" $body_bytes_sent "$http_referer" '              '"$http_user_agent" "$http_x_forwarded_for"';              access_log  /var/log/nginx/www.log  www_ijietu_com;        error_page   500 502 503 504  /50x.html;        location = /50x.html {            root   html;        }    }    # 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;    #    }    #}}

關於gzip的各個參數的具體含義請參考我的另一篇文章:http://blog.csdn.net/jessonlv/article/details/8016284

各個模組參數含義請參考;http://wiki.nginx.org/NginxChs

或者參考我的資源:http://download.csdn.net/detail/jessonlv/4603422 這上面有關於nginx各個參數的詳細解釋.

至此,nginx的安裝及配置算是基本完成。下面進行測試:

03、測試:

啟動tomcat6.0_1和tomcat6.0_2.具體命令在此不再贅述,相信大家都很熟悉,不然就別做Java了。

啟動nginx:

[root@localhost conf]# nginx -c /usr/local/nginx/nginx-1.2.3/conf/nginx.conf

上面試測試時nginx的安裝路徑,啟動時注意你自己的安裝路徑。沒有報錯即為啟動成功。

查看nginx進程:

[root@localhost conf]# ps -ef | grep nginxroot     16597     1  0 12:00 ?        00:00:00 nginx: master process nginx -c /usr/local/nginx/nginx-1.2.3/conf/nginx.confnginx    16598 16597  0 12:00 ?        00:00:01 nginx: worker process                                root     17385  3878  0 17:07 pts/2    00:00:00 grep nginx[root@localhost conf]# 

在瀏覽器輸入:http://192.168.128.129  訪問項目首頁。

至此,linux下tomcat+nginx的負載平衡是做好了。但是這才是個開始,memcached的session共用才是重中之重。

四、memcached的下載安裝及完成session共用的配置

1、memcached管理session共用:
安裝memcached一般需要安裝libevent支援組件。
伺服器端主要是安裝memcache伺服器端,目前的最新版本是 memcached-1.4.5
官網下載:http://memcached.org/
另外,Memcache用到了libevent這個庫用於Socket的處理,所以還需要安裝libevent,libevent的最新版本是libevent-2.0.20。(如果你的系統已經安裝了libevent,可以不用安裝)官網下載:http://libevent.org/

1.先安裝libevent。這個東西在配置時需要指定一個安裝路徑,即./configure --prefix=/usr;然後make;然後make install;

2.再安裝memcached,只是需要在配置時需要指定libevent的安裝路徑即./configure --with-libevent=/usr;然後make;然後make install;
 這樣就完成了Linux下Memcache伺服器端的安裝。詳細的方法如下:

2.1先安裝libevent:# tar zxvf libevent-2.0.20.tar.gz# cd libevent-2.0.20# ./configure --prefix=/usr# make# make install2.2安裝memcached,同時需要安裝中指定libevent的安裝位置:# cd /tmp# tar zxvf memcached-1.4.15.tar.gz# cd memcached-1.4.15# ./configure --with-libevent=/usr# make# make install

測試libevent是否安裝成功:

# ls -al /usr/lib | grep libevent

測試memcached是否安裝成功:

# ls -al /usr/local/bin/mem*

memcached必須啟動才能完成服務,啟動memcached:

memcached -d -m 20 -u root -i 192.168.128.129 -p 11211 -c 1024 -P /tmp/memcached.pid

具體各個參數的含義請看:

-p 監聽的連接埠-l 串連的IP地址, 預設是本機-d start 啟動memcached服務-d restart 重起memcached服務-d stop|shutdown 關閉正在啟動並執行memcached服務-d install 安裝memcached服務-d uninstall 卸載memcached服務-u 以的身份運行 (僅在以root啟動並執行時候有效)-m 最大記憶體使用量,單位MB。預設64MB-M 記憶體耗盡時返回錯誤,而不是刪除項-c 最大同時串連數,預設是1024-f 塊大小增長因子,預設是1.25-n 最小分配空間,key+value+flags預設是48-h 顯示協助

2、配置

memcached有多種session管理方式,這裡採用官網稱最有效率的一種kryo。
附各種方式所需要的jar包:

kryo-serializer: msm-kryo-serializer, kryo-serializers, kryo, minlog, reflectasm, asm-3.2javolution-serializer: msm-javolution-serializer, javolution-5.4.3.1xstream-serializer: msm-xstream-serializer, xstream, xmlpull, xpp3_minflexjson-serializer: msm-flexjson-serializer, flexjson

各種方式的序列化方案以及jar的下載請參考gong1208的文章:http://gong1208.iteye.com/blog/1596120以及chenzhou123520的文章:http://chenzhou123520.iteye.com/blog/1650212 寫的相當的不錯,對我有很大協助。

不管你選擇哪種序列化策略,你都需要 memcached-session-manager-${version}.jar ,如果你使用的是tomcat6,則還需要下載 memcached-session-manager-tc6-${version}.jar ,如果使

用的是tomcat7則下載 memcached-session-manager-tc7-${version}.jar 。同時還需要下載 spymemcached-2.7.3.jar.下載這完這些jar包後把jar包放到 $CATALINA_HOME/lib/目錄
在此特別需要注意的是:jar包的版本非常重要,不然容易造成jar之間的互相衝突,造成非配置性的錯誤。
在此我使用的是:

spymemcached-2.7.1.jarreflectasm-1.01.jarmsm-kryo-serializer-1.6.3.jarminlog-1.2.jarmemcached-session-manager-tc6-1.6.3.jarmemcached-session-manager-1.6.3.jarkryo-serializers-0.10.jarkryo-1.04.jarasm-3.2.jar

spymemcached-2.7.1.jar下載:http://download.csdn.net/detail/jessonlv/4603643

 reflectasm-1.01.jar下載:http://download.csdn.net/detail/jessonlv/4603628

 msm-kryo-serializer-1.6.3.jar下載:http://download.csdn.net/detail/jessonlv/4603638
 minlog-1.2.jar下載:http://download.csdn.net/detail/jessonlv/4603636
 memcached-session-manager-tc6-1.6.3.jar下載:http://download.csdn.net/detail/jessonlv/4603633
 memcached-session-manager-1.6.3.jar下載:http://download.csdn.net/detail/jessonlv/4603632

 kryo-serializers-0.10.jar下載:http://download.csdn.net/detail/jessonlv/4603628

 kryo-1.04.jar下載:http://download.csdn.net/detail/jessonlv/4603624 

 asm-3.2.jar下載:http://download.csdn.net/detail/jessonlv/4603621

尤其注意的是我之前安裝的是memcached-1.2.0不能與各個jar包很好的相容,造成session共用的失敗,換成memcached-1.4.15之後,相容報錯問題也就隨之解決。
我們還需要修改 $CATALINA_HOME/conf/context.xml檔案中Context節點下的內容,添加memcached-session-manager配置。
這裡我們採用非粘性session管理配置:

<Context>    ...    <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"      memcachedNodes="n1:192.168.128.129:11211"      failoverNodes="n1"      requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"      transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"      />  </Context> 

tomcat6.0_2的配置與此相同。
至此,memcached的配置也完畢,啟動兩個tomcat,再啟動nginx,再啟動memcached,進行與session有關的操作,然後可以關掉其中任意一台tomcat查看sessionid,之後同樣的方法測試另外一台,可以發現,不管是那個tomcat在運行,sessionid的值是相同的。

到此,已經完成了負載平衡以及session的全部配置。希望對大家有所協助。

轉載請註明出處:http://blog.csdn.net/jessonlv/article/details/8025132

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.