其他分享
首页 > 其他分享> > 基于Apache和tomcat实现负载均衡

基于Apache和tomcat实现负载均衡

作者:互联网

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 plaincopy  
  1. <?xmlversion='1.0'encoding='utf-8'?>

  2.  

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

  4.  

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

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

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

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

  9.  

  10. <GlobalNamingResources>

  11.  

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

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

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

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

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

  17. </GlobalNamingResources>

  18.  

  19. <Servicename="Catalina">

  20.  

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

  22. connectionTimeout="20000"

  23. redirectPort="8443"/>

  24.  

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

  26.  

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

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

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

  30. resourceName="UserDatabase"/>

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

  32. unpackWARs="true"autoDeploy="true"

  33. xmlValidation="false"xmlNamespaceAware="false">

  34. </Host>

  35. </Engine>

  36. </Service>

  37. </Server>

 

worker3如下:

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

  2.  

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

  4.  

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

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

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

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

  9.  

  10. <GlobalNamingResources>

  11.  

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

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

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

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

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

  17. </GlobalNamingResources>

  18.  

  19. <Servicename="Catalina">

  20.  

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

  22. connectionTimeout="20000"

  23. redirectPort="8443"/>

  24.  

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

  26.  

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

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

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

  30. resourceName="UserDatabase"/>

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

  32. unpackWARs="true"autoDeploy="true"

  33. xmlValidation="false"xmlNamespaceAware="false">

  34. </Host>

  35. </Engine>

  36. </Service>

  37. </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,能正常访问

 

标签:负载,tomcat,worker2,worker,apache,jk,Apache,mod
来源: https://www.cnblogs.com/2567xl/p/11537140.html