您的位置:首页 > 理论基础 > 计算机网络

Apache Http Server与Tomcat实现负载均衡和集群

2016-03-26 11:28 741 查看
本文转载,原文链接:

http://www.blogjava.net/obpm/archive/2011/06/22/352814.html

一、分布式实现原理



如上图所示,主要通过Apache-Server作为中转服务器,实现多个tomcat服务器之间的分布式处理,用户直接请求Apache-Server,然后Apache-Server会将请求分发到具体的tomcat-server,之后tomcat-server响应客户请求并返回结果到Apache-Server,最后Apache-Server返回结果给用户

二、负载均衡

文件说明:

mod_jk.conf

主要定义mod_jk模块的位置以及mod_jk模块的连接日志设置,还有定义worker.properties文件的位置。

worker.properties

定义worker的参数,主要是连接tomcat主机的地址和端口信息。如果Tomcat与apache不在同一台机器上,或者需要做多台机器上tomcat的负载均衡只需要更改workers.properties文件中的相应定义即可。

%APACHE_HOME%为你的安装目录

环境说明:

主要使用了一个Apache Server和两个Tomcat5.5,在同一台电脑上进行测试。

安装

1、 准备软件

Jdk1.6

下载地址:http://java.sun.com

tomcat -6.0.29

下载地址:http://jakarta.apache.org

apache_2.2.4-win32-x86-no_ssl.msi

下载地址:http://httpd.apache.org/download.cgi

mod_jk-1.2.31-httpd-2.0.52.so (主要作用是建立Apache Server与Tomcat之间的连接)

下载地址:http://www.apache.org/dist/tomcat/tomcat-connectors/jk/binaries/

说明: apache-server安装完成后,可以在浏览器中输入http://localhost/来测试,如果出现” It works!”则表示安装成功。

2、 安装mod_jk连接模块

安装好Jdk、tomcat、apache后,加入mod_jk连接模块,就是把mod_jk-1.2.31-httpd-2.2.3.so文件拷贝到%APACHE_HOME%\modules下,把jk模块的配置放到单独的文件中来,在%APACHE_HOME%\conf目录新建mod_jk.conf、workers.properties文件。

在httpd.conf最后加上:

# JK module settings

Include conf/mod_jk.conf

说明:以上表示将mod_jk.conf配置文件包含进来

3、修改mod_jk.conf文件

为了保持httpd.conf文件的简洁,把jk模块的配置放到单独的文件中来。

在mod_jk.conf文件中添加以下内容:

# Load mod_jk2 module
LoadModule jk_module modules/mod_jk-1.2.31-httpd-2.2.3.so

# Where to find workers.properties(引用workers配置文件)
JkWorkersFile conf/workers.properties

# Where to put jk logs(log文件路径)
JkLogFile logs/mod_jk2.log

# Set the jk log level [debug/error/info](log级别)
JkLogLevel info

# Select the log format(log格式)
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
# JkOptions indicate to send SSL KEY SIZE,
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories

# JkRequestLogFormat set the request format
JkRequestLogFormat "%w %V %T"

# Send JSPs for context / to worker named loadBalancer(URL转发配置,匹配的URL才转发到tomcat进行处理)
JkMount /*.jsp controller
# JkMount /*.* loadBalancer


3、 修改workers.properties文件

在workers.properties文件中添加以下内容:

#server 列表
worker.list = controller,tomcat1,tomcat2

# tomcat1(ajp13 端口号,在tomcat下server.xml配置,默认8009)
worker.tomcat1.port=8009
#tomcat的主机地址,如不为本机,请填写ip地址
worker.tomcat1.host=localhost
worker.tomcat1.type=ajp13
#server的加权比重,值越高,分得的请求越多
worker.tomcat1.lbfactor = 1

# tomcat2
worker.tomcat2.port=9009
worker.tomcat2.host=localhost
worker.tomcat2.type=ajp13
worker.tomcat2.lbfactor = 1

# controller(负载均衡控制器)
worker.controller.type=lb
# 指定分担请求的tomcat
worker.controller.balanced_workers=tomcat1,tomcat2
#worker.controller.sticky_session=true


说明:此文件配置了2个tomcat服务器进行负载均衡处理,sticky_session表述是否将对SESSION ID的请求路由回到相同的Tomcat worker。如果属性值不为0,它将被设置为JK_TRUE,session将是粘性的,即SESSION ID的请求路由回到相同的Tomcat worker;当Tomcat正使用能够跨越多个Tomcat实例持久化session数据的Session Manager时,它将被设置为JK_FALSE

5、修改tomcat配置文件server.xml

更改其中一个的设置打开tomcat2/conf/server.xml文件,修改里面所有的端口设置,将8改为9,如下:







6、 编写一个测试页面teat1.jsp

建立一个test的web应用,里面新建一个test1.jsp,内容为

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>test1</title>
</head>
<body>
<%
System.out.println("===========================");
%>
</body>
</html>


7、 启动服务器并进行测试

依次启动apache-server、tomcat1、tomcat2,通过 http://localhost/test/test1.jsp 访问,查看tomcat1的窗口,可以看到打印了一行”==========”,再刷新一次,tomcat2也打印了一条,再刷新,可以看到请求会被tomcat1,tomcat2轮流处理,实现了负载均衡



三、集群(session复制)

只配置负载均衡还不行,还要session复制,也就是说其中任何一个tomcat的添加的session,是要同步复制到其它tomcat, 集群内的tomcat都有相同的session

1、tomcat配置

1.1 修改tomcat1, tomcat2的server.xml文件添加集群内容,tomcat5.5无需添加,只需要去掉注释符,tomcat6.0需要添加,内容如下:

Tomcat5.5配置

<Cluster className="org.apache.catalina.cluster.tcp.SimpleTcpCluster" managerClassName="org.apache.catalina.cluster.session.DeltaManager"
expireSessionsOnShutdown="false"
useDirtyFlag="true"
notifyListenersOnReplication="true">
<Membership className="org.apache.catalina.cluster.mcast.McastService" mcastAddr="228.0.0.4"
mcastPort="45564"
mcastFrequency="500"
mcastDropTime="3000"/>
<Receiver className="org.apache.catalina.cluster.tcp.ReplicationListener"
tcpListenAddress="auto"
tcpListenPort="4001"
tcpSelectorTimeout="100"
tcpThreadCount="6"/>
<Sender className="org.apache.catalina.cluster.tcp.ReplicationTransmitter" replicationMode="pooled"
ackTimeout="15000"
waitForAck="true"/>
<Valve className="org.apache.catalina.cluster.tcp.ReplicationValve"       filter=".*\.gif;.*\.js;.*\.jpg;.*\.png;.*\.htm;.*\.html;.*\.css;.*\.txt;"/>
<Deployer className="org.apache.catalina.cluster.deploy.FarmWarDeployer" tempDir="/tmp/war-temp/"
deployDir="/tmp/war-deploy/"
watchDir="/tmp/war-listen/"
watchEnabled="false"/>
<ClusterListener className="org.apache.catalina.cluster.session.ClusterSessionListener"/>
</Cluster>


Tomcat6.0配置

<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
channelSendOptions="6">
<!--
<Manager className="org.apache.catalina.ha.session.BackupManager"
expireSessionsOnShutdown="false"
notifyListenersOnReplication="true"
mapSendOptions="6"/>
-->
<Manager className="org.apache.catalina.ha.session.DeltaManager"
expireSessionsOnShutdown="false"
notifyListenersOnReplication="true"/>

<Channel className="org.apache.catalina.tribes.group.GroupChannel">
<Membership className="org.apache.catalina.tribes.membership.McastService"
address="228.0.0.4"
port="45564"
frequency="500"
dropTime="3000"/>
<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
address="auto"
port="5000"
selectorTimeout="100"
maxThreads="6"/>
<Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
<Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
</Sender>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor"/>
</Channel>
<Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
filter=".*\.gif;.*\.js;.*\.jpg;.*\.png;.*\.htm;.*\.html;.*\.css;.*\.txt;"/>
<Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"
tempDir="/tmp/war-temp/"
deployDir="/tmp/war-deploy/"
watchDir="/tmp/war-listen/"
watchEnabled="false"/>

<ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
</Cluster>


分别添加以上内容后,在tomcat2中,修改tcpListenPort=”4001” 为4002

1.2. Engine增加jvmRoute属性设置,jvmRoute的值来自于workers.properties文件所设置的服务器名称。

<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">


#server 列表
worker.list = controller,tomcat1,tomcat2


2、 添加test.jsp页面

2.1. test.jsp添加以下内容:

<%@ page contentType="text/html; charset=UTF-8"%>

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

<html>

<head>

<title>Cluster App Test</title>

</head>

<body>

Server Info:

<%

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

%>

<%

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

// 如果有新的 Session 属性设置

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

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

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

session.setAttribute(dataName, dataValue);

}

out.print("<b>Session 列表</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">

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

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

<input type=submit></form>

</body>

</html>


2.2. 修改web.xml文件,加入 < distributable/>节点,如下所示:

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">

<display-name>test</display-name>

<distributable/>

<welcome-file-list>

<welcome-file>index.html</welcome-file>

<welcome-file>index.htm</welcome-file>

<welcome-file>index.jsp</welcome-file>

<welcome-file>default.html</welcome-file>

<welcome-file>default.htm</welcome-file>

<welcome-file>default.jsp</welcome-file>

</welcome-file-list>

</web-app>


3、 Session复制测试

测试步骤如下:

1) 启动apache-server、tomcat1、tomcat2

2) 访问http://localhost/test/test.jsp,输入名称:test0001、值:123并点击“提交查询内容”按钮,显示效果如下:

如上图所示,tomcat1创建了一个新的session,session中有属性test0001,值为123



3) 关闭tomcat1服务器,tomcat1端口为8080,如下图:



4) 在页面中再次点击“提交查询内容”按钮,效果如下:



前端页面并没有发生改变,接下来查看后台情况:



如图所示,可以发现session已成功复制到tomcat2中,以此证明tomcat集群已配置成功。

5) 另外来看看不关闭tomcat1服务器再次提交的情况



如图所示,请求并没有转发到tomcat2服务器,而是再次转回tomcat1服务器,这种情况是由于配置了jvmRoute所致,以个人理解,配置了此属性后,apache-server会根据session情况来进行路由,同一个session会转发给同一个服务器。

6) 打开一个新的IE窗口,并访问http://localhost/test/test.jsp





新窗口的请求转发到了tomcat2服务器,session的id为DD9E6C8181653B9BCCF534FC8760B264.tomcat2,根据测试结果可以说明,在不发生服务器关闭的情况下,每个session会绑定到同一个服务器中,而不会在服务器间发生复制。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: