windows Apache+Tomcat负载均衡服务器搭建
一、安装Apache
Apache
版本:Apache 2.4.25 x64
下载地址:https://www.apachehaus.com/cgi-bin/download.plx下载的是zip包,直接解压)
下载的是zip包,直接解压到E:\apache\Apache24目录。使用zip包的Apache需要注意两个地方:
1、修改默认的路径
按照Apache默认的配置我们应当将Apache24硬盘的根目录下,但是我们没有放置在根目录下所以需要配置,进入conf目录下打开httpd.conf文件,在38行有这样两行代码:
1
2
|
Define
SRVROOT "/Apache24" ServerRoot
"${SRVROOT}"
|
1
2
|
Define
SRVROOT "E:\apache\Apache24"
ServerRoot
"${SRVROOT}"
|
如果不修改会提示错误:
httpd.exe: Syntax error on line 39 of D:/Android_Develop_Tools/httpd-2.4.23-x64-
vc14/Apache24/conf/httpd.conf: ServerRoot must be a valid directory
打Apache Service Monitor窗口,如果Service status中没有Apache服务,就需要将Apache服务安装到Windows的服务中。
以管理员身份打CMD命令窗口,输入安装Apache服务命令。
1
|
httpd.exe
-k install
|
二、安装 mod_jk 连接模块
下载地址:https://archive.apache.org/dist/tomcat/tomcat-connectors/jk/binaries/windows/
1、mod_jk负责Apache连接Tomcat.。将下载下来的mod_jk.so复制到E:\apache\Apache24\modules目录下。
2、修改httpd.conf文件,在该文件最后加上以下代码。
1
|
Include
conf/mod_jk.conf
|
在E:\apache\Apache24\conf目录下创建mod_jk.conf文件,文件内容如下:
1
2
3
4
5
6
7
8
9
10
|
#加载mod_jk
Module
LoadModule
jk_module modules/mod_jk.so
#指定
workers.properties文件路径
JkWorkersFile
conf/workers.properties
#指定那些请求交给tomcat处理, "controller" 为在workers.propertise里指定的负载分配控制器
JkMount
/*.jsp controller
JkMount
/*. do
controller
JkMount
/*.htm controller
|
在E:\apache\Apache24\conf目录下创建workers.properties文件,文件内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
worker.list
= controller,tomcat1,tomcat2,tomcat3 #server 列表
#========tomcat1========
worker.tomcat1.port= 8109
#ajp13 端口号,在tomcat下server.xml配置,默认 8009 worker.tomcat1.host=localhost
#tomcat的主机地址,如不为本机,请填写ip地址
worker.tomcat1.type=ajp13
worker.tomcat1.lbfactor
= 1
#server的加权比重,值越高,分得的请求越多
#========tomcat2========
worker.tomcat2.port= 8209
#ajp13 端口号,在tomcat下server.xml配置,默认 8009 worker.tomcat2.host=localhost
#tomcat的主机地址,如不为本机,请填写ip地址
worker.tomcat2.type=ajp13
worker.tomcat2.lbfactor
= 2
#server的加权比重,值越高,分得的请求越多
#========tomcat2========
worker.tomcat3.port= 8309
#ajp13 端口号,在tomcat下server.xml配置,默认 8009 worker.tomcat3.host=localhost
#tomcat的主机地址,如不为本机,请填写ip地址
worker.tomcat3.type=ajp13
worker.tomcat3.lbfactor
= 2
#server的加权比重,值越高,分得的请求越多
#========controller,负载均衡控制器========
worker.controller.type=lb
worker.controller.balanced_workers=tomcat1,tomcat2,tomcat3
#指定分担请求的tomcat
worker.controller.sticky_session= 1
|
安装3个tomcat,最重要的一点就是修改server.xml文件中的端口。注意标红的字体部分,这是需要修改的地方。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<!--?xml
version= '1.0'
encoding= 'utf-8' ?-->
<!--如果多个tomcat部署在同一台服务器中,此处的关闭tomcat的端口需要更改,不能冲突-->
<server
port= "8105"
shutdown= "SHUTDOWN" >
<listener
classname= "org.apache.catalina.startup.VersionLoggerListener" >
<listener
classname= "org.apache.catalina.core.AprLifecycleListener"
sslengine= "on" >
<listener
classname= "org.apache.catalina.core.JreMemoryLeakPreventionListener" >
<listener
classname= "org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" >
<listener
classname= "org.apache.catalina.core.ThreadLocalLeakPreventionListener" >
<globalnamingresources>
<resource
auth= "Container"
description= "User
database that can be updated and saved"
factory= "org.apache.catalina.users.MemoryUserDatabaseFactory"
name= "UserDatabase"
pathname= "conf/tomcat-users.xml"
type= "org.apache.catalina.UserDatabase" >
</resource></globalnamingresources>
<service
name= "Catalina" ></service></listener></listener></listener></listener></listener></server>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<!--如果使用apache整合,那么此配置可以注释,因为apache是通过ajp协议来通信的,tomcat并不会直接暴露出来-->
<connector
connectiontimeout= "20000"
port= "8180"
protocol= "HTTP/1.1"
redirectport= "8443" >
<!--如果多个tomcat部署在同一个服务器中,此处的ajp协议端口必须不一样,并且需要增加jvmRoute属性,该属性的值即为workers.properties中的tomcat的名称-->
<connector
jvmroute= "tomcat1"
port= "8109"
protocol= "AJP/1.3"
redirectport= "8443" >
<engine
defaulthost= "localhost"
name= "Catalina" >
<cluster
classname= "org.apache.catalina.ha.tcp.SimpleTcpCluster" >
<realm
classname= "org.apache.catalina.realm.LockOutRealm" >
<realm
classname= "org.apache.catalina.realm.UserDatabaseRealm"
resourcename= "UserDatabase" >
</realm>
<host
appbase= "webapps"
autodeploy= "true"
name= "localhost"
unpackwars= "true" >
<valve
classname= "org.apache.catalina.valves.AccessLogValve"
directory= "logs"
pattern= "%h
%l %u %t "
prefix= "localhost_access_log"
s= ""
suffix= ".txt" >
</valve></host>
</realm></cluster></engine>
</connector></connector>
|
四、Session共享
在web.xml中加入。
1
2
3
4
5
|
<!--?xml
version= "1.0"
encoding= "UTF-8" ?-->
<web-app
id= "WebApp_ID"
version= "3.1"
xmlns= "https://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi= "https://www.w3.org/2001/XMLSchema-instance"
xsi:schemalocation= "https://xmlns.jcp.org/xml/ns/javaee
https://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" >
<display-name>tomcatCluster</display-name>
<distributable>
</distributable></web-app>
|
创建Web工程,写一个JSP。部署到tomcat1、tomcat2、tomcat3服务器,启动Apache、3个Tomcat服务器。在浏览器中输入https://localhost/TomcatCluster/test.jsp。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<%@
page contentType= "text/html;
charset=UTF-8"
%>
<%@
page import = "java.util.*"
%>
<title></title>
<%
System.out.println( "SessionID:"
+ session.getId()); %> Server Info: <% out.println(request.getServerName() +
"
: "
+ request.getServerPort()+"<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><br>
");
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
name= "dataName"
size= "20"
type= "text" ><br>
数值:<input
name= "dataValue"
size= "20"
type= "text" ><br>
<input
type= "submit" > </form>
|