apache+jetty 配置web jsp服务器负载均衡

时间:2023-03-09 13:32:53
apache+jetty 配置web jsp服务器负载均衡

首先,查找中文资料,貌似很少,有一个网友写了点,但是1版本过老,2有些地方有错误。

经过我自己摸索,记录一下。这个图很简洁明了

apache+jetty 配置web jsp服务器负载均衡

第一阶段 ,配置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= maxattempts=
# 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:
BalancerMember ajp://yourjettyhost2:
</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.****.net/zhuying_linux/article/details/6600071

http://blog.****.net/zhuying_linux/article/details/6600280

http://wiki.eclipse.org/Jetty

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应用,或者应该适用于任何应用吧。