Apache+Tomcat是如何实现负载均衡的

手册/FAQ (522) 2015-12-04 11:35:04

一、规划

前端Apache:192.168.10.135

后端tomcat:192.168.10.132

                   192.168.10.133

 

二、配置Tomcat

①配置server.xml

1

2

3

4

5

# vi /usr/local/tomcat/conf/server.xml               ##位于103行

TomcatA:

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

TomcatB:

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

 

②配置测试页

1

2

3

# cd /usr/local/tomcat/webapps/ROOT

# mv index.jsp index.jsp.bak

# vi index.jsp

TomcatA:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

<%@ page language="java" %>

<html>

  <head><title>TomcatA</title></head>

  <body>

    <h1><font color="green">TomcatA </font></h1>

    <table align="centre" border="1">

      <tr>

        <td>Session ID</td>

    <% session.setAttribute("abc","abc"); %>

        <td><%= session.getId() %></td>

      </tr>

      <tr>

        <td>Created on</td>

        <td><%= session.getCreationTime() %></td>

     </tr>

    </table>

  </body>

</html>

TomcatB:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

<%@ page language="java" %>

<html>

  <head><title>TomcatB</title></head>

  <body>

    <h1><font color="yellow">TomcatB </font></h1>

    <table align="centre" border="1">

      <tr>

        <td>Session ID</td>

    <% session.setAttribute("abc","abc"); %>

        <td><%= session.getId() %></td>

      </tr>

      <tr>

        <td>Created on</td>

        <td><%= session.getCreationTime() %></td>

     </tr>

    </table>

  </body>

</html>

 

三、基于mod_jk实现

①配置mod_jk配置文件

1

2

3

4

5

6

7

8

9

# vi /etc/httpd/conf/mod_jk.conf

JkWorkersFile  /etc/httpd/conf/workers.properties   ##指出mod_jk工作所需文件位置

JkLogFile  logs/mod_jk.log                   ##指定日志输出文件位置

JkLogLevel  debug                         ##指定日志级别

JkLogStampFormat "[%a %b %d %H:%M:%S %Y]"

JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories

JkRequestLogFormat "%w %V %T"

JkMount  /*  lbcluster1                   ##指定负载均衡器名称

JkMount  /status/  stat1                 ##指定状态信息

 

②配置worker.properties

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

# vi /etc/httpd/conf/workers.properties

# workers 列表

worker.list = lbcluster1,stat1

#-------------------------------------------------

# tomcat实例1

#-------------------------------------------------

worker.TomcatA.type = ajp13              ##定向包协议

worker.TomcatA.host = 192.168.10.132     ##tomcat主机地址

worker.TomcatA.port = 8009            ##TomcatA实例端口

worker.TomcatA.lbfactor = 1             ##负载均衡权重为1

#-------------------------------------------------

# tomcat实例2

#-------------------------------------------------

worker.TomcatB.type = ajp13

worker.TomcatB.host = 192.168.10.133

worker.TomcatB.port = 8009

worker.TomcatB.lbfactor = 1

#-------------------------------------------------

# load balancer worker --负载均衡控制器

# ------------------------------------------------

worker.lbcluster1.type = lb             ##负载均衡work,lb内置类

worker.lbcluster1.sticky_session = 0       ##会话是否绑定

worker.lbcluster1.balance_workers = TomcatA, TomcatB     ##指定集群拥有实例

worker.stat1.type = status                    ##状态信息

 

③配置httpd

1

2

3

# vi /etc/httpd/conf/httpd.conf

LoadModule jk_module modules/mod_jk.so           ##加载mod_jk模块

Include /etc/httpd/conf/mod_jk.conf             ##指定mod_jk配置文件

 

THE END