本文主要是记录windows下apache+JK+tomcat负载均衡的配置过程,方便自己以后备用,也希望能给刚接触的xd提供参考。实验环境的系统已经安装好了apache2.2.17,安装路径D:\Program Files\Apache2.2\
apache http server的安装过程参见:ttp://www.micmiu.com/enterprise-app/server/apache-httpd-install/
本文的大概章节分类:
- 1.下载mod_jk
- 2.修改apache相关配置
- 3.tomcat配置修改
- 4.测试负载均衡
- 5.配置apache处理静态文件
【一】下载mod_jk
打开tomcat 的主页:http://tomcat.apache.org,左侧download页面:
![apache_jk_01](https://i-blog.csdnimg.cn/blog_migrate/d00af589684d34f633a5ea74418fbe3a.jpeg)
选择图中红色标注的链接打开如下:
![apache_jk_02](https://i-blog.csdnimg.cn/blog_migrate/d57740023de999ace8ccd1683509d580.jpeg)
选择图中红色标注的链接打开如下:
![apache_jk_03](https://i-blog.csdnimg.cn/blog_migrate/61f7c4d92c985d7652a9591600f0864d.jpeg)
选择图中红色标注的链接打开如下:
![apache_jk_04](https://i-blog.csdnimg.cn/blog_migrate/38566683d3fa0491b8ae84d08d4191a7.jpeg)
选择图中红色标注的链接打开如下:
![apache_jk_05](https://i-blog.csdnimg.cn/blog_migrate/947a24deccb73e60d6016b9074ec0b2c.jpeg)
【二】修改apache相关配置
httpd.conf 路径:D:\Program Files\Apache2.2\conf\httpd.conf
在httpd.conf文件的末尾添加如下内容:
2 | Include conf/extra/mod_jk.conf |
mod_jk.conf
Apache2.2\conf\extra\目录下新建文件:mod_jk.conf,内容如下:
2 | LoadModule jk_module modules/mod_jk-1.2.31-httpd-2.2.3.so |
6 | JkWorkersFile conf/workers.properties |
8 | JkMountFile conf/uriworkermap.properties |
10 | JkLogFile logs/mod_jk.log |
workers.properties
Apache2.2\conf\目录下新建文件:workers.properties,内容如下:
2 | worker.list=LB_worker,jkstatus |
6 | worker.worker_1.host=localhost |
8 | worker.worker_1.port=8009 |
9 | worker.worker_1. type =ajp13 |
11 | worker.worker_1.lbfactor=1 |
14 | worker.worker_2.host=localhost |
16 | worker.worker_2.port=9009 |
17 | worker.worker_2. type =ajp13 |
18 | worker.worker_2.lbfactor=1 |
21 | worker.LB_worker. type =lb |
23 | worker.LB_worker.retries=3 |
25 | worker.LB_worker.balance_workers=worker_1,worker_2 |
28 | worker.LB_worker.sticky_session= true |
30 | worker.LB_worker.sticky_session_force= false |
32 | worker.jkstatus. type =status |
workers.properties相关参数说明:
http://tomcat.apache.org/connectors-doc/reference/workers.html
- worker.loadbalancer.sticky_session=true
-
该参数指定集群是否实现会话粘性,如果设为 true ,则表明为会话粘性,同一个session的请求会分发到同一台tomcat;当多个tomcat有一个session管理器时该值设为 false。
- worker.loadbalancer.sticky_session_force=false
-
该参数仅在上面sticky_session=true时才起作用。如果上面的 sticky_session 设为 true 时,建议此处也设为 false
uriworkermap.properties
Apache2.2\conf\目录下新建文件:uriworkermap.properties,内容如下:
uriworkermap.properties相关参数说明:
http://tomcat.apache.org/connectors-doc/reference/uriworkermap.html
【三】tomcat配置修改
有关JDK、tomcat的基本安装配置本文不再作详细介绍,重点介绍与当前负载均衡配置相关的内容,以tomcat7.0.2为例来配置。
[1#] 修改tomcat-7.0.2_1\conf\server.xml
配置文件片段如下:
2 | < Connector URIEncoding = "UTF-8" port = "8082" protocol = "HTTP/1.1" |
3 |
connectionTimeout = "20000" |
8 |
< Connector port = "8009" protocol = "AJP/1.3" redirectPort = "8443" /> |
11 | < Engine name = "Catalina" defaultHost = "localhost" jvmRoute = "worker_1" > |
[2#] 修改tomcat-7.0.2_2\conf\server.xml
配置文件片段如下:
1 | < Connector URIEncoding = "UTF-8" port = "9082" protocol = "HTTP/1.1" |
2 |
connectionTimeout = "20000" |
7 |
< Connector port = "9009" protocol = "AJP/1.3" redirectPort = "9443" /> |
10 | < Engine name = "Catalina" defaultHost = "localhost" jvmRoute = "worker_2" > |
【四】测试负载均衡
[1#] 新建测试页面test.jsp
新建页面tomcat-7.0.2_1\webapps\test\test.jsp内容如下:
2 |
System.out.println("this is tomcat server nod 1"); |
5 |
out.println(request.getLocalAddr() + " : " + request.getLocalPort()+"< br >this is tomcat server nod 1"); |
新建页面tomcat-7.0.2_2\webapps\test\test.jsp内容如下:
2 |
System.out.println( "this is tomcat server nod 2" ); |
5 |
out.println(request.getLocalAddr() + " : " + request.getLocalPort()+ "<br>this is tomcat server nod 2" ); |
[2#]测试负载均衡
启动好两个tomcat和apache,在浏览器里输入http://localhost/test/test.jsp回车,不断刷新页面会看到两个页面交替显示如下:
![apache_jk_06](http://www.micmiu.com/wp-content/uploads/2012/02/apache_jk_06.jpg)
![apache_jk_07](https://i-blog.csdnimg.cn/blog_migrate/cabff7a8f1b2ca1f02b1610d2b91930d.jpeg)
同时可以观察到两个tomcat的后台窗口会交替打印日志信息:
![apache_jk_08](https://i-blog.csdnimg.cn/blog_migrate/587e91d1ed5c9e992eb4f4976305e64b.jpeg)
![apache_jk_09](https://i-blog.csdnimg.cn/blog_migrate/a76e48079afe5a9b0b6d6217d9ae1d6e.jpeg)
到此基本实现了负载均衡的配置。
在浏览器里地址栏里输入http://localhost/jkstatus回车,可以看到当前的server的一些运行情况:
![apache_jk_10](https://i-blog.csdnimg.cn/blog_migrate/0e906e94a3effa20d624e326518a4c52.jpeg)
【四】配置apache处理静态文件
[1#] 修改页面tomcat-7.0.2_1\webapps\test\test.jsp内容如下:
2 |
System.out.println("this is tomcat server nod 1"); |
5 | < IMG height = 100 src = "images/inf.jpg" width = 100 border = 0 >< br > |
7 |
out.println(request.getLocalAddr() + " : " + request.getLocalPort()+"< br >this is tomcat server nod 1"); |
修改页面tomcat-7.0.2_2\webapps\test\test.jsp内容如下:
2 |
System.out.println("this is tomcat server nod 2"); |
5 | < IMG height = 100 src = "images/inf.jpg" width = 100 border = 0 >< br > |
7 |
out.println(request.getLocalAddr() + " : " + request.getLocalPort()+"< br >this is tomcat server nod 2"); |
[2#] 修改uriworkermap.properties配置文件
[3#] 静态文件存放位置
静态文件放在Apache2.2\htdocs\下,它的目录结构和webapps下的目录结构基本相同,新建好相应的目录结构:htdocs\test\images\,复制test.jsp页面用到的inf.jpg图片到images\下,此时基本完成了相应的配置。
此时在浏览器里在打开http://localhost/test/test.jsp,,刷新又将会看到如下两个页面交替出现:
![apache_jk_11](https://i-blog.csdnimg.cn/blog_migrate/7d1077c4d0ec7c6eac50e7aac5aaf8ff.jpeg)
![apache_jk_12](https://i-blog.csdnimg.cn/blog_migrate/e3795d83c21536d88f16146a3ef45098.jpeg)
此时静态图片,是由apache http server处理,就不再需要tomcat来处理。
原创文章,转载请注明: 转载自micmiu – 软件开发+生活点滴[ http://www.micmiu.com/ ]
本文链接地址: http://www.micmiu.com/architecture/cluster/apache-jk-tomcat/