apache+tomcat负载均衡3种实现方式

1.首先安装apache,编译完成后,通过IP:端口就行访问,如果返回“it workers”证明Apache启动成功(注意apache的工程路径要正确)

2.下载JK,下载地址为http://mirror.bjtu.edu.cn/apache/tomcat/tomcat-connectors/jk/binaries/win32/jk-1.2.31/,果http服务器是Apache2.0.X版本,则必须选择mod_jk-1.2.31-httpd-2.0.52.so,页面下方有英文的说明,大家可以看下,我这里宣传的mod_jk-1.2.23-apache-2.2.x-linux-x86_64.so,跟自己操作系统版本吻合,然后重命名为mod_jk.so,并且赋值777权限

3.将mod_jk.so拷贝到apache/modules

4.在httpd.conf里增加如下内容

   include "/usr/local/apache/conf/mod_jk.conf"

5.在conf路径下新建mod_jk.conf文件,并添加如下内容  

#加载mod_jk Module  

LoadModule jk_module modules/mod_jk.so

#指定 workers.properties文件路径  

JkWorkersFile conf/workers.properties

#指定哪些请求交给tomcat处理,"loadbalancer"为在workers.propertise里指定的负载分配控制器名

JkLogFile logs/mod_jk.log

#JkLogLevel debug

JkMount  /*.do loadbalancer

JkMount  /*.jsp loadbalancer

6.在conf下新建workers.properties文件,并添加如下内容

worker.list=loadbalancer  

#此处与mod_jk.conf中保持一致

worker.worker2.port=8009  

#tomcat2中 ajp13 端口号,对应tomcat配置文件server.xml中Connector port="8009",默认8009

worker.worker2.host=127.0.0.1

#worker2的地址,本机loaclhost或者127.0.0.1,远程直接写IP

worker.worker2.type=ajp13

#与worker2中的server.xml的protocol保持一致

worker.worker2.lbfactor=1

#负载的权重值,越高表示负载越大

worker.worker3.port=9009

worker.worker3.host=127.0.0.1

worker.worker3.type=ajp13

worker.worker3.lbfactor=1

worker.loadbalancer.type=lb

worker.loadbalancer.balance_workers=worker2,worker3

#指定负载的worker列表,用逗号分隔

worker.loadbalancer..sticky_session=false

#配置session会话是否为粘性    

#这样负载均衡器lb就会尽量保持一个session,也就是使用户在一次会话中跟同一个Tomcat进行交互

worker.loadbalancer..sticky_session_force=false

worker.loadbalancer.sticky_session=1

#是否启用session共享

7.下载tomcat并解压,复制2份tomcat,保持成为worker2,worker3

 修改2个tomcat的server.xml,保证2个tomcat的端口号不一致,具体配置如下,需要修改黑体加粗标示

worker2如下:

[html] view plain copy
  1. <?xmlversion='1.0'encoding='utf-8'?>

  2. <Serverport="8005"shutdown="SHUTDOWN">

  3. <ListenerclassName="org.apache.catalina.core.AprLifecycleListener"SSLEngine="on"/>

  4. <ListenerclassName="org.apache.catalina.core.JasperListener"/>

  5. <ListenerclassName="org.apache.catalina.mbeans.ServerLifecycleListener"/>

  6. <ListenerclassName="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>

  7. <GlobalNamingResources>

  8. <Resourcename="UserDatabase"auth="Container"

  9. type="org.apache.catalina.UserDatabase"

  10. description="User database that can be updated and saved"

  11. factory="org.apache.catalina.users.MemoryUserDatabaseFactory"

  12. pathname="conf/tomcat-users.xml"/>

  13. </GlobalNamingResources>

  14. <Servicename="Catalina">

  15. <Connectorport="8080"protocol="HTTP/1.1"

  16. connectionTimeout="20000"

  17. redirectPort="8443"/>

  18. <Connectorport="8009"protocol="AJP/1.3"redirectPort="8443"/>

  19. <!--<Engine name="Catalina" defaultHost="localhost">-->

  20. <Enginename="Catalina"defaultHost="localhost"jvmRoute="worker2">

  21. <RealmclassName="org.apache.catalina.realm.UserDatabaseRealm"

  22. resourceName="UserDatabase"/>

  23. <Hostname="localhost"appBase="webapps"

  24. unpackWARs="true"autoDeploy="true"

  25. xmlValidation="false"xmlNamespaceAware="false">

  26. </Host>

  27. </Engine>

  28. </Service>

  29. </Server>


worker3如下:

[html] view plain copy
  1. <?xmlversion='1.0'encoding='utf-8'?>

  2. <Serverport="8006"shutdown="SHUTDOWN">

  3. <ListenerclassName="org.apache.catalina.core.AprLifecycleListener"SSLEngine="on"/>

  4. <ListenerclassName="org.apache.catalina.core.JasperListener"/>

  5. <ListenerclassName="org.apache.catalina.mbeans.ServerLifecycleListener"/>

  6. <ListenerclassName="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>

  7. <GlobalNamingResources>

  8. <Resourcename="UserDatabase"auth="Container"

  9. type="org.apache.catalina.UserDatabase"

  10. description="User database that can be updated and saved"

  11. factory="org.apache.catalina.users.MemoryUserDatabaseFactory"

  12. pathname="conf/tomcat-users.xml"/>

  13. </GlobalNamingResources>

  14. <Servicename="Catalina">

  15. <Connectorport="8081"protocol="HTTP/1.1"

  16. connectionTimeout="20000"

  17. redirectPort="8443"/>

  18. <Connectorport="9009"protocol="AJP/1.3"redirectPort="8443"/>

  19. <!--<Engine name="Catalina" defaultHost="localhost">-->

  20. <Enginename="Catalina"defaultHost="localhost"jvmRoute="worker3">

  21. <RealmclassName="org.apache.catalina.realm.UserDatabaseRealm"

  22. resourceName="UserDatabase"/>

  23. <Hostname="localhost"appBase="webapps"

  24. unpackWARs="true"autoDeploy="true"

  25. xmlValidation="false"xmlNamespaceAware="false">

  26. </Host>

  27. </Engine>

  28. </Service>

  29. </Server>


8.分别在2个tomcat下建立项目test1,并新增andashu.jsp,内容如下:

<%


System.out.println("===========");


%>


9.分别访问2个tomcat,http://IP:8081/test1/andashu.jsp 以及http://10.14.133.204:8080/test1/andashu.jsp,如果2个tomcat的日志catalina.out里 都打印出===========,证明2个tomcat访问都ok


10.分别启动apache worker2,worker3,用不同的浏览器或者不同的机器分别访问http://IP/test1/andashu.jsp,如果如果2个tomcat的日志catalina.out里 都打印出===========,证明负载均衡搭建成功(这里要用不同浏览器或者不同机器访问)


11.试节点故障,随便停掉其中的一台tomcat,继续访问http://IP/test1/andashu.jsp,能正常访问




其他2种方式也可以实现负载均衡

2、ajp_proxy
去掉httpd.conf文件中下面内容的注释(删掉#号),开启下边的配置

Include conf/extra/httpd-vhosts.conf
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule proxy_connect_module modules/mod_proxy_connect.so
LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
LoadModule proxy_http_module modules/mod_proxy_http.so
注意:
除了mod_proxy.so,mod_proxy_balancer.so,mod_proxy_connect.so
如果是采用ajp_proxy,需要加载mod_proxy_ajp.so这个模块;
如果是采用http_proxy,需要加载mod_proxy_http.so这个模块;

httpd.conf,删除刚才jk方式的配置内容,增加下边的配置
ProxyRequests Off
< proxy balancer://loadbalancer >
BalancerMember ajp://127.0.0.1:9009 loadfactor=1 route=worker3
BalancerMember ajp://127.0.0.1:8009loadfactor=1 route=worker2
</ proxy >
conf/extra/httpd-vhosts.conf增加配置
< VirtualHost *:8081 >
        ServerAdmin [email protected]
        ServerName localhost
        ServerAlias localhost
        ProxyPass / balancer://loadbalancer/ stickysession=jsessionid nofailover=On
        ProxyPassReverse / balancer://loadbalancer/
        ErrorLog "logs/loadbalancer-error.log"
        CustomLog "logs/loadbalancer-access.log" common
</ VirtualHost >
配置完成任务,访问http://IP:8081/test1/andashu.jsp,检查是否能正常访问。

3、http_proxy
http_proxy的配置与ajp_proxy类似,修改方法2的httpd.conf配置如下,其它不变
ProxyRequests Off
< proxy balancer://loadbalancer >
BalancerMember http://127.0.0.1:9009 loadfactor=1 route=worker3
BalancerMember http://127.0.0.1:8009 loadfactor=1 route=worker2
</ proxy >
配置完成任务,访问http://IP:8081/test/test.jsp,检查是否能正常访问

你可能感兴趣的:(集群,负载均衡,apache+tomcat)