隨著IT行業(yè)的發(fā)展,,linux服務(wù)器在企業(yè)中應(yīng)用廣泛,,人們對(duì)linux上的應(yīng)用服務(wù)要求也越來越高,,早先的apache服務(wù)器,、apache有優(yōu)點(diǎn)也 有不足,,apache漸漸不能滿足人們的要求,,目前nginx被大量采用做高性能web服務(wù)器,,無論是個(gè)人網(wǎng)站還是大型門戶網(wǎng)站都在使用nginx作為 web搭建web服務(wù)器的首選,。
接下來我們親自來搭建一個(gè)屬于自己的高性能web服務(wù)器,并且?guī)Ч收献詣?dòng)轉(zhuǎn)移的群集,、和負(fù)載均衡服務(wù)器,。
一、Nginx+Tomcat+Session 高性能群集服務(wù)搭建
- 本文采用目前主流版本搭建,,搭建環(huán)境及版本如下列表:
- 系統(tǒng)版本:Centos 5.3 64位系統(tǒng)
- Nginx版本為:nginx-1.0.5.tar.gz
- Jdk版本為:1.6.0_18_64
- Tomcat版本為:Apache Tomcat/6.0.30
- 其他相似版本也可以,!安裝服務(wù)所需的包如下下載路徑:本網(wǎng)站集成部分下載:
-
- jdk請(qǐng)到以下網(wǎng)站下載:請(qǐng)選擇自己系統(tǒng)版本的jdk。
-
- https://cds./is-bin/INTERSHOP.enfinity/WFS/CDS-CDS_Developer-Site/en_US/-/USD/ViewFilteredProducts-SingleVariationTypeFilter
-
- 其他版本請(qǐng)到官網(wǎng)下載
二,、首先安裝Nginx:安裝nginx之前需要安裝pcre包和zlib以支持重寫,正則以及網(wǎng)頁壓縮等等】
把所需的包下載到/usr/src下【根據(jù)自己的習(xí)慣,,路徑可以改變】
1、首先安裝pcre:
- cd /usr/src &&tar xzf pcre-8.01.tar.gz &&cd pcre-8.01 && ./configure --prefix=/usr/local/pcre &&make &&make install
2,、然后再安裝nginx :【給nginx安裝jvmroute模塊】
- cd /usr/src &&useradd www &&wget http://friendly.sinaapp.com//LinuxSoft/nginx-upstream-jvm-route-0.1.tar.gz && tar xzf nginx-upstream-jvm-route-0.1.tar.gz && tar xzf nginx-1.0.5.tar.gz &&cd nginx-1.0.5&& patch -p0 <../nginx_upstream_jvm_route/jvm_route.patch && ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-openssl=/usr/ --with-pcre=/usr/src/pcre-8.01 --add-module=../nginx_upstream_jvm_route/ --user=www --group=www &&make &&make install
- 【nginx注意* –with-pcre=/usr/src/pcre-8.01指向的是源碼包解壓的路徑,,而不是安裝的路徑,否則會(huì)報(bào)
-
- make[1]: *** [/usr/local/pcre/Makefile] Error 127 錯(cuò)誤
Nginx 安裝完畢,!我們先不著急nginx配置文件的配置,,先來修改tomcat配置文件:
三、Tomcat安裝配置:
1,、將所需的包下載至/usr/src 目錄,;首先安裝jdk:
- chmod o+x jdk* && ./jdk* ;程序包會(huì)提示按回車鍵,我們根據(jù)提示安裝即可,,解壓完畢,,會(huì)在當(dāng)前目錄生成 jdk1.6.0_18 這個(gè)文件夾,
- mkdir -p /usr/java && mv jdk1.6.0_18 /usr/java/下
-
- vi /etc/profile 最后面加入以下語句:
- export JAVA_HOME=/usr/java/jdk1.6.0_18
- export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
- export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH:$HOMR/bin
- source /etc/profile //使環(huán)境變量馬上生效
- java –version //查看java版本,,顯示版本是1.6.0_18,,并且是64位的證明安裝成功!
- 【 Java HotSpot(TM) 64-Bit Server VM (build 16.0-b10, mixed mode) 】
-
- cd /usr/src && tar xzf apache-tomcat-6.0.30.tar.gz
- 解壓完成執(zhí)行:
- mv apache-tomcat-6.0.30 /usr/local/tomcat_1 && cp /usr/local/tomcat_1 /usr/local/tomcat_2 -r
- 【移動(dòng)到/usr/local下并重命名為tomcat_1文件夾,并cp一個(gè)tomcat_2】
- 部署兩個(gè)Tomcat群集做負(fù)載均衡用
2,、設(shè)置tomcat的java變量
- cd /usr/local/tomcat_1/bin/ && vi setenv.sh 創(chuàng)建一個(gè)setenv.sh腳本文件,,添加如下語句即可
- JAVA_HOME=/usr/java/jdk1.6.0_18
- JAVA_JRE=/usr/java/jdk1.6.0_18/jre
- 并且拷貝setenv.sh文件到tomcat_2/bin/下 :
-
- cp -p /usr/local/tomcat_1/bin/setenv.sh /usr/local/tomcat_2/bin 下即可!
- 保存退出
- 檢查兩個(gè)tomcat下bin目錄的*.sh 文件是否有可執(zhí)行權(quán)限,,如果沒有則進(jìn)入到相應(yīng)的目錄執(zhí)行 chmod o+x *.sh 即可,!
-
- 給兩個(gè)tomcat分別創(chuàng)建不同的目錄如下:mkdir -p /usr/webapps/{www_1,www_2}
3、設(shè)置Tomcat的 server.xml文件:
【以下是我的tomcat配置文件具體內(nèi)容,,本想已附件的形式發(fā)的,本博客目前還不支持附件功能,重要修改的地方供大家參考,,如果把所有配置都粘貼在這里,,顯示有問題,所有用了兩個(gè)web鏈接,!這樣一來部分解釋就產(chǎn)生了影響,。不明白的歡迎留言】
這個(gè)是完整的server.xml文件下載地址:
http://chinaapp.sinaapp.com/download/server.xml.tgz
- 兩個(gè)Tomcat配置的cluster里面的端口分別為4000、4001 ,,必須設(shè)置成不同的端口,;而且得分別設(shè)置兩個(gè)jvmroute名稱,一會(huì)在nginx中會(huì)用到,!
-
- 配置Tomcat session會(huì)話復(fù)制:
- 分別在兩個(gè)tomcat的conf下web.xml文件里面加入如下一行即可:
-
- <distributable/>
- 這個(gè)是加入tomcat的session復(fù)制的,,做tomcat集群必須需要這一步,否則用戶的session就無法正常使用 <distributable/>直接加在</web-app>之前就可以了
-
- 配置完畢后,,我們測(cè)試一下廣播
- java -cp tomcat-replication.jar MCaster 224.0.0.1 45564 Terminal1
- java -cp tomcat-replication.jar MCaster 224.0.0.1 45564 Terminal2
-
- 如果不報(bào)錯(cuò)則能正常廣播
- tomcat-replication.jar 下載:http://cvs./~fhanik/tomcat-replication.jar
- 如果是兩臺(tái)機(jī)器的話,,可以用tcpdump 抓包看看!
Tomcat配置完畢,,啟動(dòng)兩個(gè)tomcat,,先啟動(dòng)tomcat1,tomcat_1啟動(dòng)日志如下:
- 信息: Initializing Coyote HTTP/1.1 on http-8080
- 2011-3-18 19:56:21 org.apache.catalina.startup.Catalina load
- 信息: Initialization processed in 1088 ms
- 2011-3-18 19:56:21 org.apache.catalina.core.StandardService start
- 信息: Starting service Catalina
- 2011-3-18 19:56:21 org.apache.catalina.core.StandardEngine start
- 信息: Starting Servlet Engine: Apache Tomcat/6.0.18
- 2011-3-18 19:56:21 org.apache.catalina.ha.tcp.SimpleTcpCluster start
- 信息: Cluster is about to start
- 2011-3-18 19:56:21 org.apache.catalina.tribes.transport.ReceiverBase bind
- 信息: Receiver Server Socket bound to:/192.168.2.79:4000
- 2011-3-18 19:56:21 org.apache.catalina.tribes.membership.McastServiceImpl setupSocket
- 信息: Setting cluster mcast soTimeout to 500
- 2011-3-18 19:56:21 org.apache.catalina.tribes.membership.McastServiceImpl waitForMembers
- 信息: Sleeping for 1000 milliseconds to establish cluster membership, start level:4
- 2011-3-18 19:56:22 org.apache.catalina.tribes.membership.McastServiceImpl waitForMembers
- 信息: Done sleeping, membership established, start level:4
- 2011-3-18 19:56:22 org.apache.catalina.tribes.membership.McastServiceImpl waitForMembers
- 信息: Sleeping for 1000 milliseconds to establish cluster membership, start level:8
- 2011-3-18 19:56:23 org.apache.catalina.tribes.membership.McastServiceImpl waitForMembers
- 信息: Done sleeping, membership established, start level:8
- 2011-3-18 19:56:23 org.apache.catalina.ha.deploy.FarmWarDeployer start
- 嚴(yán)重: FarmWarDeployer can only work as host cluster subelement!
- 2011-3-18 19:56:23 org.apache.catalina.ha.session.DeltaManager start
- 信息: Register manager to cluster element Engine with name Catalina
- 2011-3-18 19:56:23 org.apache.catalina.ha.session.DeltaManager start
- 信息: Starting clustering manager at
- 2011-3-18 19:56:23 org.apache.catalina.ha.session.DeltaManager getAllClusterSessions
- 信息: Manager [localhost#]: skipping state transfer. No members active in cluster group.
- 2011-3-18 19:56:23 org.apache.catalina.ha.session.JvmRouteBinderValve start
- 信息: JvmRouteBinderValve started
- 2011-3-18 19:56:23 org.apache.coyote.http11.Http11Protocol start
- 信息: Starting Coyote HTTP/1.1 on http-8080
- 2011-3-18 19:56:24 org.apache.jk.common.ChannelSocket init
- 信息: JK: ajp13 listening on /0.0.0.0:8411
- 2011-3-18 19:56:24 org.apache.jk.server.JkMain start
- 信息: Jk running ID=0 time=0/42 config=null
- 2011-3-18 19:56:24 org.apache.catalina.startup.Catalina start
- 信息: Server startup in 2676 ms
Tomcat_2的日志如下:
- 000,{-64, -88, 2, 79},4000, alive=86901,id={78 42 25 -19 -102 3 64 10 -76 93 69 -43 45 76 95 -112 }, payload={}, command={}, domain={}, ]. This operation will timeout if no session state has been received within 60 seconds.
- 2011-3-18 19:57:48 org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor report
- 信息: ThroughputInterceptor Report[
- Tx Msg:1 messages
- Sent:0.00 MB (total)
- Sent:0.00 MB (application)
- Time:0.01 seconds
- Tx Speed:0.04 MB/sec (total)
- TxSpeed:0.04 MB/sec (application)
- Error Msg:0
- Rx Msg:1 messages
- Rx Speed:0.00 MB/sec (since 1st msg)
- Received:0.00 MB]
-
- 2011-3-18 19:57:48 org.apache.catalina.ha.session.DeltaManager waitForSendAllSessions
- 信息: Manager [localhost#]; session state send at 11-3-18 下午7:57 received in 125 ms.
- 2011-3-18 19:57:48 org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor report
- 信息: ThroughputInterceptor Report[
- Tx Msg:2 messages
- Sent:0.00 MB (total)
- Sent:0.00 MB (application)
- Time:0.05 seconds
- Tx Speed:0.02 MB/sec (total)
- TxSpeed:0.02 MB/sec (application)
- Error Msg:0
- Rx Msg:2 messages
- Rx Speed:0.00 MB/sec (since 1st msg)
- Received:0.00 MB]
-
- 2011-3-18 19:57:49 org.apache.catalina.ha.session.JvmRouteBinderValve start
- 信息: JvmRouteBinderValve started
- 2011-3-18 19:57:49 org.apache.coyote.http11.Http11Protocol start
- 信息: Starting Coyote HTTP/1.1 on http-8081
- 2011-3-18 19:57:49 org.apache.jk.common.ChannelSocket init
- 信息: JK: ajp13 listening on /0.0.0.0:8412
- 2011-3-18 19:57:49 org.apache.jk.server.JkMain start
- 信息: Jk running ID=0 time=0/34 config=null
- 2011-3-18 19:57:49 org.apache.catalina.startup.Catalina start
- 信息: Server startup in 2962 ms
|