首先,查找中文資料,貌似很少,有一個網友寫了點,但是1版本過老,2有些地方有錯誤。
經過我自己摸索,記錄一下。這個圖很簡潔明了
第一階段 ,配置jetty
首先從 http://download.eclipse.org/jetty/ 下載jetty-8 ,因為jetty-9 不支持 ajp【參考鏈接見 下鏈接4 HotTo/Configure_AJP13】 ,所以這里暫時選擇8。
解壓到 /usr/local/jetty/ 並將 /usr/local/jetty/bin 添加到PATH中:
echo export PATH=$PATH:/usr/local/jetty/bin >>/etc/profile
echo export JETTY_HOME=/usr/local/jetty >>/etc/profile
. /etc/profile
mkdir $JETTY_HOME/webapps/test1
vim $JETTY_HOME/webapps/test1/index.jsp
寫一個測試 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.getCreationTime()); out.println("<br> SESSION ID:" + session.getId()+"<br>"); out.println("Session serviced by jetty_a"+"<br>"); out.println("Session created time is :"+session.getCreationTime()+"<br>"); %> </body> </html>
然后不用修改任何文件,默認會使用jetty的測試文件 $JETTY_HOME/contexts/
[root@localhost ~]# ll $JETTY_HOME/contexts
總用量 20
-rw-rw-r--. 1 1000 1000 913 5月 20 21:28 javadoc.xml
-rw-rw-r--. 1 1000 1000 634 5月 20 21:28 README.TXT
drwxrwxr-x. 2 1000 1000 4096 6月 18 11:27 test.d
-rw-rw-r--. 1 1000 1000 4165 5月 20 21:02 test.xml
然后
[root@localhost ~]# cp $JETTY_HOME/bin/jetty.sh /etc/rc.d/init.d/jetty [root@localhost ~]# chkconfig --add jetty [root@localhost ~]# chkconfig --list jetty jetty 0:關閉 1:關閉 2:關閉 3:啟用 4:關閉 5:關閉 6:關閉 [root@localhost ~]# service jetty stop Stopping Jetty: OK [root@localhost ~]# [root@localhost ~]# service jetty start Starting Jetty: 2013-06-18 14:51:22.069:INFO::Redirecting stderr/stdout to /usr/local/jetty-distribution-8.1.11.v20130520/logs/2013_06_18.stderrout.log OK Tue Jun 18 14:51:25 CST 2013
這樣就開啟了jetty的服務,並開始了jetty,默認jetty的端口是 8080 ,如果服務器沒有安裝tomcat,或者其他占用8080端口的程序,此時打開 http://服務器IP:8080 應該可以瀏覽了。
我打開 http://192.168.1.107:8080/test1/index.jsp 顯示了下面的測試內容。
SESSION ID:1p8k3p99jj7sv1gfw08w6ppl6e Session serviced by jetty_b Session created time is :1371538594898
為了增加ajp的支持,確認 $JETTY_HOME/etc/jetty-ajp.xml 文件內容是:
<?xml version="1.0"?> <!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd"> <Configure id="Server" class="org.eclipse.jetty.server.Server"> <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --> <!-- Add a AJP listener on port 8009 --> <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --> <Call name="addConnector"> <Arg> <New class="org.eclipse.jetty.ajp.Ajp13SocketConnector"> <Set name="port">8009</Set> </New> </Arg> </Call> </Configure>
上面的是默認在jetty版本8.1.11的ajp配置的內容。
修改 $JETTY_HOME/start.ini
在OPTIONS后面添加ajp,即:
OPTIONS=Server,jsp,jmx,resources,websocket,ext,plus,annotations,ajp
在末尾添加 jetty-ajp.xml文件
etc/jetty-ajp.xml
start.ini的內容如下[還是比較簡短的]:
#=========================================================== # Jetty start.jar arguments # Each line of this file is prepended to the command line # arguments # of a call to: # java -jar start.jar [arg...] #=========================================================== #=========================================================== # If the arguements in this file include JVM arguments # (eg -Xmx512m) or JVM System properties (eg com.sun.???), # then these will not take affect unless the --exec # parameter is included or if the output from --dry-run # is executed like: # eval $(java -jar start.jar --dry-run) # # Below are some recommended options for Sun's JRE #----------------------------------------------------------- # --exec # -Dorg.apache.jasper.compiler.disablejsr199=true # -Dcom.sun.management.jmxremote # -Dorg.eclipse.jetty.util.log.IGNORED=true # -Dorg.eclipse.jetty.LEVEL=DEBUG # -Dorg.eclipse.jetty.util.log.stderr.SOURCE=true # -Xmx2000m # -Xmn512m # -verbose:gc # -XX:+PrintGCDateStamps # -XX:+PrintGCTimeStamps # -XX:+PrintGCDetails # -XX:+PrintTenuringDistribution # -XX:+PrintCommandLineFlags # -XX:+DisableExplicitGC # -XX:+UseConcMarkSweepGC # -XX:ParallelCMSThreads=2 # -XX:+CMSClassUnloadingEnabled # -XX:+UseCMSCompactAtFullCollection # -XX:CMSInitiatingOccupancyFraction=80 #----------------------------------------------------------- #=========================================================== # Start classpath OPTIONS. # These control what classes are on the classpath # for a full listing do # java -jar start.jar --list-options #----------------------------------------------------------- OPTIONS=Server,jsp,jmx,resources,websocket,ext,plus,annotations,ajp #----------------------------------------------------------- #=========================================================== # Configuration files. # For a full list of available configuration files do # java -jar start.jar --help #----------------------------------------------------------- #etc/jetty-jmx.xml etc/jetty.xml etc/jetty-annotations.xml # etc/jetty-ssl.xml # etc/jetty-requestlog.xml etc/jetty-deploy.xml #etc/jetty-overlay.xml etc/jetty-webapps.xml etc/jetty-contexts.xml etc/jetty-testrealm.xml etc/jetty-ajp.xml #===========================================================
services jetty restart 重啟jetty
復制以上所有操作到另外一台機器的操作系統上,我的集群環境是 centos6.4 x64 xen下的,克隆機器還算簡單,網卡被換掉,IP會通過dhcp再獲得。
可以將index.jsp中 jetty_a 替換成jetty_b jetty_c等,用於識別后面的負載均衡是否起作用的。
第二階段,配置tomcat httpd的負載均衡
centos下安裝httpd為
yum install httpd
vim /etc/httpd/conf/httpd.conf
在末尾添加:
ProxyPass / balancer://cluster/ stickysession=JESSIONID|jessionid nofailover=On lbmethod=byrequests timeout=5 maxattempts=3 # balancer: 復制會話的方式,包括JSESSIONID或PHPSESSIONID ; # nofailover:on 表示會話在worker出錯或停掉是句會中斷,當后端服務器不支持會話復制時設為on ; # lbmethod:選擇負載的調度算法,默認byrequests表示輪詢調度(就是1:1), # bytraffic表示加權重的調度,需加loadfactor指定權重值。 ProxyPassReverse / balancer://cluster/ # 此指令使Apache調整HTTP重定向應答中Location, Content-Location,URI頭里的URL。 # 這樣可以避免在Apache作為反向代理使用時,后端服務器的HTTP重定向造成的繞過反向代理的問題。 # The ProxyRequests directive should usually be set off when using ProxyPass. ProxyRequests Off # 不允許作為正向代理 ProxyPreserveHost On # 當啟用時,此選項將把傳入請求的"Host:"行傳遞給被代理的主機,而不是傳遞在ProxyPass中指定的主機名。 <proxy balancer://cluster> #BalancerMember ajp://192.168.1.109:8009 route=jetty_a BalancerMember ajp://192.168.1.109:8009 BalancerMember ajp://192.168.1.107:8009 # rout 值附加在session ID 后面 </proxy>
以上的session ID,我不知道是jetty6.x 專用還是怎么的,zhuying_linux網友沒解釋清楚,我就不用了,以下內容是 http://wiki.eclipse.org/Jetty/Howto/Configure_AJP13
在mod_proxy_ajp 這一節中示例配置
LoadModule proxy_module modules/mod_proxy.so LoadModule proxy_ajp_module modules/mod_proxy_ajp.so LoadModule proxy_balancer_module modules/mod_proxy_balancer.so # always keep the host header ProxyPreserveHost On # map to cluster ProxyPass /test balancer://my_cluster/test stickysession=JSESSIONID nofailover=On ProxyPass /demo balancer://my_cluster/demo stickysession=JSESSIONID nofailover=On # define the balancer, with http and/ or ajp connections <Proxy balancer://my_cluster> BalancerMember ajp://yourjettyhost1:8009 BalancerMember ajp://yourjettyhost2:8009 </Proxy>
service httpd restart
開啟httpd服務成功 就算成功了,httpd的ROOT目錄就會顯示 jetty的負載均衡頁面了打開 http://192.168.1.109/ 可以顯示jetty的內容了。
此時關閉任何一個服務器的jetty,httpd的負載均衡都會顯示正確的內容,全部jetty關閉,則會顯示 服務暫時不可用
頁面 http://192.168.1.109/test1/ 可以顯示出 是哪一個jetty服務器的內容,顯示jetty_a jetty_b jetty_c。我刷新一次就變化一次,各出現一次。
問題:關於session 真的會一致嗎? 服務存儲要使用另一種分布式存儲,不會單獨保存到一個服務器上。
上面是使用ajp協議進行負載均衡的,jetty文檔推薦 使用http協議,Use HTTP and mod_proxy。因為他們覺得ajp協議不標准,亂,對某些服務支持不是很好等。
mod_proxy在下面參考資料也有。
參考資料:
http://blog.csdn.net/zhuying_linux/article/details/6600071
http://blog.csdn.net/zhuying_linux/article/details/6600280
http://wiki.eclipse.org/Jetty/Howto/Configure_AJP13
http://wiki.eclipse.org/Jetty/Tutorial/Apache
http://wiki.eclipse.org/Jetty/Howto/Configure_mod_proxy
順便說下,jetty安裝成windows 服務的形式,在jetty-6.1.26還有一個jetty-service.exe ,其實這個文件版本信息其實是:
Wrapper (Version 3.2.3) http://wrapper.tanukisoftware.org
所以,jetty 7 8 9... 以后都可以使用 http://wrapper.tanukisoftware.com/doc/english/download.jsp 這個網站的wrapper來自己配置 成windows服務,適用於任何java的jar應用,或者應該適用於任何應用吧。