[color=red]blog迁移至[/color]:[url=http://www.micmiu.com]http://www.micmiu.com[/url]
本文主要是记录windows下apache+JK+tomcat负载均衡的配置过程,方便自己以后备用,也希望能给刚接触的xd提供参考。
实验环境的系统已经安装好了apache2.2.17,安装路径D:\Program Files\Apache2.2\
[size=medium][color=blue]apache http server的安装过程参见[/color]:[url=http://sjsky.iteye.com/blog/918769]http://sjsky.iteye.com/blog/918769[/url][/size]
[size=medium]本文的大概章节分类:[/size]
[list]
[*]1.下载mod_jk
[*]2.修改apache相关配置
[*]3.tomcat配置修改
[*]4.测试负载均衡
[*]5.配置apache处理静态文件
[/list]
[size=large][color=blue]【一】下载mod_jk[/color][/size]
打开tomcat 的主页:http://tomcat.apache.org,左侧download页面:
[img]http://dl.iteye.com/upload/attachment/423154/70017b51-73b8-30b5-a5c2-552b5077aed4.jpg[/img]
选择图中红色标注的链接打开如下:
[img]http://dl.iteye.com/upload/attachment/423156/89f354f7-6008-35c9-8654-8f60fd4cf2c7.jpg[/img]
选择图中红色标注的链接打开如下:
[img]http://dl.iteye.com/upload/attachment/423158/fe7124f6-6c60-3c11-9ff3-425c5d9ecd04.jpg[/img]
选择图中红色标注的链接打开如下:
[img]http://dl.iteye.com/upload/attachment/423160/e9eaad27-6d92-34a1-bb89-f1808209c709.jpg[/img]
选择图中红色标注的链接打开如下:
[img]http://dl.iteye.com/upload/attachment/423162/73415a61-7ed0-370b-b082-069d2a51bf91.jpg[/img]
选择当前apache2.2.17版本对应的mod_jk文件下载,下载完把文件复制到Apache2.2\modules\mod_jk-1.2.31-httpd-2.2.3.so
[size=large][color=blue]【二】修改apache相关配置[/color][/size]
[size=medium][color=blue]httpd.conf[/color][/size]
路径:D:\Program Files\Apache2.2\conf\httpd.conf
在httpd.conf文件的末尾添加如下内容:
[size=medium][color=blue]mod_jk.conf[/color][/size]
Apache2.2\conf\extra\目录下新建文件:mod_jk.conf,内容如下:
[size=medium][color=blue]workers.properties[/color][/size]
Apache2.2\conf\目录下新建文件:workers.properties,内容如下:
[color=red][size=medium]workers.properties相关参数说明:[/size][/color]
[url=http://tomcat.apache.org/connectors-doc/reference/workers.html]http://tomcat.apache.org/connectors-doc/reference/workers.html[/url]
[list]
[*][color=red]worker.loadbalancer.sticky_session=true [/color]
该参数指定集群是否实现会话粘性,如果设为 true ,则表明为会话粘性,同一个session的请求会分发到同一台tomcat;当多个tomcat有一个session管理器时该值设为 false。
[*] [color=red]worker.loadbalancer.sticky_session_force=false [/color]
该参数仅在上面sticky_session=true时才起作用。如果上面的 sticky_session 设为 true 时,建议此处也设为 false
[/list]
[size=medium][color=blue]uriworkermap.properties[/color][/size]
Apache2.2\conf\目录下新建文件:uriworkermap.properties,内容如下:
[color=red][size=medium]uriworkermap.properties相关参数说明:[/size][/color]
[url=http://tomcat.apache.org/connectors-doc/reference/uriworkermap.html]http://tomcat.apache.org/connectors-doc/reference/uriworkermap.html[/url]
[size=large][color=blue]【三】tomcat配置修改[/color][/size]
有关JDK、tomcat的基本安装配置本文不再作详细介绍,重点介绍与当前负载均衡配置相关的内容,以tomcat7.0.2为例来配置。
[size=medium][color=blue][1#] 修改tomcat-7.0.2_1\conf\server.xml[/color][/size]
配置文件片段如下:
[size=medium][color=blue][1#] 修改tomcat-7.0.2_2\conf\server.xml[/color][/size]
配置文件片段如下:
[size=large][color=blue]【四】测试负载均衡[/color][/size]
[size=medium][color=blue][1#] 新建测试页面test.jsp[/color][/size]
新建页面tomcat-7.0.2_1\webapps\test\test.jsp内容如下:
新建页面tomcat-7.0.2_2\webapps\test\test.jsp内容如下:
[size=medium][color=blue][2#]测试负载均衡[/color][/size]
启动好两个tomcat和apache,在浏览器里输入[color=red]http://localhost/test/test.jsp[/color]回车,不断刷新页面会看到两个页面交替显示如下:
[img]http://dl.iteye.com/upload/attachment/423175/16aabb98-1cf5-36b4-a8e5-2ee1b73100c1.jpg[/img]
[img]http://dl.iteye.com/upload/attachment/423177/043bf46c-b4dc-308a-87ce-353c6d313e40.jpg[/img]
同时可以观察到两个tomcat的后台窗口会交替打印日志信息:
[img]http://dl.iteye.com/upload/attachment/423179/26ee4501-3442-363a-9e67-9d94fe98703e.jpg[/img]
[img]http://dl.iteye.com/upload/attachment/423181/0a8bab01-409f-3563-be15-0318cf3c97cf.jpg[/img]
到此基本实现了负载均衡的配置。
在浏览器里地址栏里输入[color=red]http://localhost/jkstatus[/color]回车,可以看到当前的server的一些运行情况:
[img]http://dl.iteye.com/upload/attachment/423193/8c5515fd-9096-3ad1-ba78-9a49b22d272d.jpg[/img]
[size=large][color=blue]【四】配置apache处理静态文件[/color][/size]
[color=blue][size=medium][1#] 修改页面tomcat-7.0.2_1\webapps\test\test.jsp内容如下:[/size][/color]
修改页面tomcat-7.0.2_2\webapps\test\test.jsp内容如下:
[color=blue][size=medium][2#] 修改uriworkermap.properties配置文件[/size][/color]
[color=blue][size=medium][3#] 静态文件存放位置[/size][/color]
静态文件放在Apache2.2\htdocs\下,它的目录结构和webapps下的目录结构基本相同,新建好相应的目录结构:htdocs\test\images\,复制test.jsp页面用到的inf.jpg图片到images\下,此时基本完成了相应的配置。
此时在浏览器里在打开[color=blue]http://localhost/test/test.jsp,[/color],刷新又将会看到如下两个页面交替出现:
[img]http://dl.iteye.com/upload/attachment/423206/119f5b40-c95c-3d10-8b54-baf2170d1fa2.jpg[/img]
[img]http://dl.iteye.com/upload/attachment/423208/2564c0be-efac-3e65-ad68-f2c71fde316b.jpg[/img]
此时静态图片,是由apache http server处理,就不再需要tomcat来处理。
------------------------------------分------------------------------------隔------------------------------------线------------------------------------
本文主要是记录windows下apache+JK+tomcat负载均衡的配置过程,方便自己以后备用,也希望能给刚接触的xd提供参考。
实验环境的系统已经安装好了apache2.2.17,安装路径D:\Program Files\Apache2.2\
[size=medium][color=blue]apache http server的安装过程参见[/color]:[url=http://sjsky.iteye.com/blog/918769]http://sjsky.iteye.com/blog/918769[/url][/size]
[size=medium]本文的大概章节分类:[/size]
[list]
[*]1.下载mod_jk
[*]2.修改apache相关配置
[*]3.tomcat配置修改
[*]4.测试负载均衡
[*]5.配置apache处理静态文件
[/list]
[size=large][color=blue]【一】下载mod_jk[/color][/size]
打开tomcat 的主页:http://tomcat.apache.org,左侧download页面:
[img]http://dl.iteye.com/upload/attachment/423154/70017b51-73b8-30b5-a5c2-552b5077aed4.jpg[/img]
选择图中红色标注的链接打开如下:
[img]http://dl.iteye.com/upload/attachment/423156/89f354f7-6008-35c9-8654-8f60fd4cf2c7.jpg[/img]
选择图中红色标注的链接打开如下:
[img]http://dl.iteye.com/upload/attachment/423158/fe7124f6-6c60-3c11-9ff3-425c5d9ecd04.jpg[/img]
选择图中红色标注的链接打开如下:
[img]http://dl.iteye.com/upload/attachment/423160/e9eaad27-6d92-34a1-bb89-f1808209c709.jpg[/img]
选择图中红色标注的链接打开如下:
[img]http://dl.iteye.com/upload/attachment/423162/73415a61-7ed0-370b-b082-069d2a51bf91.jpg[/img]
选择当前apache2.2.17版本对应的mod_jk文件下载,下载完把文件复制到Apache2.2\modules\mod_jk-1.2.31-httpd-2.2.3.so
[size=large][color=blue]【二】修改apache相关配置[/color][/size]
[size=medium][color=blue]httpd.conf[/color][/size]
路径:D:\Program Files\Apache2.2\conf\httpd.conf
在httpd.conf文件的末尾添加如下内容:
# module mod_jk config file
Include conf/extra/mod_jk.conf
[size=medium][color=blue]mod_jk.conf[/color][/size]
Apache2.2\conf\extra\目录下新建文件:mod_jk.conf,内容如下:
#load module mod_jk-1.2.31-httpd-2.2.3.so is for Apache 2.2.x.
LoadModule jk_module modules/mod_jk-1.2.31-httpd-2.2.3.so
#配置 mod_jk conf
#加载集群中的workers
JkWorkersFile conf/workers.properties
#加载workers的请求处理分配文件
JkMountFile conf/uriworkermap.properties
#指定jk的日志输出文件
JkLogFile logs/mod_jk.log
#指定日志级别
JkLogLevel info
[size=medium][color=blue]workers.properties[/color][/size]
Apache2.2\conf\目录下新建文件:workers.properties,内容如下:
# worker列表
worker.list=LB_worker,jkstatus
#第一个worker的配置,名为worker_1
#tomcat的主机地址,如不为本机,请填写ip地址
worker.worker_1.host=localhost
#ajp13 端口号,对应tomcat配置文件server.xml中Connector port="8009",默认8009
worker.worker_1.port=8009
worker.worker_1.type=ajp13
#负载的权重值,越高表示负载越大
worker.worker_1.lbfactor=1
#第二个worker的配置,名为worker_2
worker.worker_2.host=localhost
#ajp13 端口号,对应tomcat配置文件server.xml中Connector port="9009"
worker.worker_2.port=9009
worker.worker_2.type=ajp13
worker.worker_2.lbfactor=1
#LB_worker,用于负载均衡分发的控制器
worker.LB_worker.type=lb
#重试次数
worker.LB_worker.retries=3
#指定负载的worker列表,用逗号分隔
worker.LB_worker.balance_workers=worker_1,worker_2
#配置session会话是否为粘性
#这样负载均衡器lb就会尽量保持一个session,也就是使用户在一次会话中跟同一个Tomcat进行交互
worker.LB_worker.sticky_session=true
#如果sticky_session设为true时,此处一般设为false
worker.LB_worker.sticky_session_force=false
#设置运行状态的控制器
worker.jkstatus.type=status
[color=red][size=medium]workers.properties相关参数说明:[/size][/color]
[url=http://tomcat.apache.org/connectors-doc/reference/workers.html]http://tomcat.apache.org/connectors-doc/reference/workers.html[/url]
[list]
[*][color=red]worker.loadbalancer.sticky_session=true [/color]
该参数指定集群是否实现会话粘性,如果设为 true ,则表明为会话粘性,同一个session的请求会分发到同一台tomcat;当多个tomcat有一个session管理器时该值设为 false。
[*] [color=red]worker.loadbalancer.sticky_session_force=false [/color]
该参数仅在上面sticky_session=true时才起作用。如果上面的 sticky_session 设为 true 时,建议此处也设为 false
[/list]
[size=medium][color=blue]uriworkermap.properties[/color][/size]
Apache2.2\conf\目录下新建文件:uriworkermap.properties,内容如下:
#所有请求都由LB_worker这个worker处理
/*=LB_worker
#所有包含jkstatus请求的都由名称叫jkstatus的这个worker处理
/jkstatus=jkstatus
[color=red][size=medium]uriworkermap.properties相关参数说明:[/size][/color]
[url=http://tomcat.apache.org/connectors-doc/reference/uriworkermap.html]http://tomcat.apache.org/connectors-doc/reference/uriworkermap.html[/url]
[size=large][color=blue]【三】tomcat配置修改[/color][/size]
有关JDK、tomcat的基本安装配置本文不再作详细介绍,重点介绍与当前负载均衡配置相关的内容,以tomcat7.0.2为例来配置。
[size=medium][color=blue][1#] 修改tomcat-7.0.2_1\conf\server.xml[/color][/size]
配置文件片段如下:
<!-- 两个tomat的端口区分开 -->
<Connector URIEncoding="UTF-8" port="8082" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<!-- Define an AJP 1.3 Connector on port 8009 -->
<!-- port="8009" 对应于workers.properties中worker.worker_1.port=8009 -->
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
<!-- jvmRoute 的值对应于workers.properties中worker_1 -->
<Engine name="Catalina" defaultHost="localhost" jvmRoute="worker_1">
[size=medium][color=blue][1#] 修改tomcat-7.0.2_2\conf\server.xml[/color][/size]
配置文件片段如下:
<Connector URIEncoding="UTF-8" port="9082" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="9443" />
<!-- Define an AJP 1.3 Connector on port 9009 -->
<!-- port="9009" 对应于workers.properties中worker.worker_2.port=8009-->
<Connector port="9009" protocol="AJP/1.3" redirectPort="9443" />
<!-- jvmRoute 的值对应于workers.properties中worker_2 -->
<Engine name="Catalina" defaultHost="localhost" jvmRoute="worker_2">
[size=large][color=blue]【四】测试负载均衡[/color][/size]
[size=medium][color=blue][1#] 新建测试页面test.jsp[/color][/size]
新建页面tomcat-7.0.2_1\webapps\test\test.jsp内容如下:
<%
System.out.println("this is tomcat server nod 1");
%>
<%
out.println(request.getLocalAddr() + " : " + request.getLocalPort()+"<br>this is tomcat server nod 1");
%>
新建页面tomcat-7.0.2_2\webapps\test\test.jsp内容如下:
<%
System.out.println("this is tomcat server nod 2");
%>
<%
out.println(request.getLocalAddr() + " : " + request.getLocalPort()+"<br>this is tomcat server nod 2");
%>
[size=medium][color=blue][2#]测试负载均衡[/color][/size]
启动好两个tomcat和apache,在浏览器里输入[color=red]http://localhost/test/test.jsp[/color]回车,不断刷新页面会看到两个页面交替显示如下:
[img]http://dl.iteye.com/upload/attachment/423175/16aabb98-1cf5-36b4-a8e5-2ee1b73100c1.jpg[/img]
[img]http://dl.iteye.com/upload/attachment/423177/043bf46c-b4dc-308a-87ce-353c6d313e40.jpg[/img]
同时可以观察到两个tomcat的后台窗口会交替打印日志信息:
[img]http://dl.iteye.com/upload/attachment/423179/26ee4501-3442-363a-9e67-9d94fe98703e.jpg[/img]
[img]http://dl.iteye.com/upload/attachment/423181/0a8bab01-409f-3563-be15-0318cf3c97cf.jpg[/img]
到此基本实现了负载均衡的配置。
在浏览器里地址栏里输入[color=red]http://localhost/jkstatus[/color]回车,可以看到当前的server的一些运行情况:
[img]http://dl.iteye.com/upload/attachment/423193/8c5515fd-9096-3ad1-ba78-9a49b22d272d.jpg[/img]
[size=large][color=blue]【四】配置apache处理静态文件[/color][/size]
[color=blue][size=medium][1#] 修改页面tomcat-7.0.2_1\webapps\test\test.jsp内容如下:[/size][/color]
<%
System.out.println("this is tomcat server nod 1");
%>
<!-- 增加一个图片显示 -->
<IMG height=100 src="images/inf.jpg" width=100 border=0><br>
<%
out.println(request.getLocalAddr() + " : " + request.getLocalPort()+"<br>this is tomcat server nod 1");
%>
修改页面tomcat-7.0.2_2\webapps\test\test.jsp内容如下:
<%
System.out.println("this is tomcat server nod 2");
%>
<!-- 增加一个图片显示 -->
<IMG height=100 src="images/inf.jpg" width=100 border=0><br>
<%
out.println(request.getLocalAddr() + " : " + request.getLocalPort()+"<br>this is tomcat server nod 2");
%>
[color=blue][size=medium][2#] 修改uriworkermap.properties配置文件[/size][/color]
#所有请求都由LB_worker这个worker处理
/*=LB_worker
#所有包含jkstatus请求的都由名称叫jkstatus的这个worker处理
/jkstatus=jkstatus
#所有以.gif、.jpg等等结尾的请求都不经过名称叫LB_worker的worker处理
# ! 表示 非 的意思
!/*.jpg=LB_worker
[color=blue][size=medium][3#] 静态文件存放位置[/size][/color]
静态文件放在Apache2.2\htdocs\下,它的目录结构和webapps下的目录结构基本相同,新建好相应的目录结构:htdocs\test\images\,复制test.jsp页面用到的inf.jpg图片到images\下,此时基本完成了相应的配置。
此时在浏览器里在打开[color=blue]http://localhost/test/test.jsp,[/color],刷新又将会看到如下两个页面交替出现:
[img]http://dl.iteye.com/upload/attachment/423206/119f5b40-c95c-3d10-8b54-baf2170d1fa2.jpg[/img]
[img]http://dl.iteye.com/upload/attachment/423208/2564c0be-efac-3e65-ad68-f2c71fde316b.jpg[/img]
此时静态图片,是由apache http server处理,就不再需要tomcat来处理。
------------------------------------分------------------------------------隔------------------------------------线------------------------------------