使用apache和nginx代理实现tomcat负载均衡及集群配置详解

实验环境:



1、nginx的代理功能



nginx proxy:
eth0: 192.168.8.48
vmnet2 eth1: 192.168.10.10


tomcat server1:
vmnet2 eth0: 192.168.10.20


tomcat server2:
vmnet2 eth0: 192.168.10.30


使用apache和nginx代理实现tomcat负载均衡及集群配置详解_第1张图片

# yum install -y nginx-1.8.1-1.el6.ngx.x86_64.rpm
# vim /etc/nginx/conf.d/default.conf 


location / {
        root  /web/htdocs;
        index index.jsp index.html index.htm;
    }


    location ~* \.(jsp|do|action)$ {
        proxy_pass http://192.168.10.20;
    }


    location ~* \.(jpg|jpeg|gif|png|pdf|doc|rar|exe|zip|)$ {
        proxy_pass http://192.168.10.30;
    }


2、apache的代理功能

http方式的代理

# cd /etc/httpd/conf.d/
# vim mod_proxy.conf
加入如下内容:


ProxyRequests off
ProxyPreserveHost on


ProxyPass / http://192.168.10.20/
ProxyPassReverse / http://192.168.l0.20/



  Order Allow,Deny
  Allow from all


使用apache和nginx代理实现tomcat负载均衡及集群配置详解_第2张图片

②ajp方式的代理

ProxyVia on
ProxyRequests off
ProxyPreserveHost on


ProxyPass / ajp://192.168.10.20/
ProxyPassReverse / ajp://192.168.l0.20/



  Order Allow,Deny
  Allow from all


使用apache和nginx代理实现tomcat负载均衡及集群配置详解_第3张图片

②、配置apache基于mod_jk的负载均衡



a.安装apxs插件
# yum install -y httpd-devel
# tar xf tomcat-connectors-1.2.37-src.tar.gz 
# cd tomcat-connectors-1.2.37-src/native
# ./configure --with-apxs=/usr/sbin/apxs
# make && make install
使用apache和nginx代理实现tomcat负载均衡及集群配置详解_第4张图片

配置相关文件
# cd /etc/httpd/conf.d
# vim mod_jk.conf


LoadModule jk_module modules/mod_jk.so
JkWorkersFile /etc/httpd/conf.d/workers.properties
JkLogFile logs/mod_jk.log
JkLogLevel notice
JkMount /* TomcatA
JkMount /status/ statA
                                 
# vim workers.properties


worker.list=TomcatA,statA
worker.TomcatA.type=ajp13
worker.ToccatA.port=8009
worker.TomcatA.host=192.168.10.20
worker.TomcatA.lbfactor=1
worker.statA.type=status


访问如下地址,可以看到tomcat server1的状态
http://192.168.8.48/status/


③使用mod_jk配置负载均衡服务器



配置代理服务器
# cd /etc/httpd/conf.d
# cat mod_jk.conf 
LoadModule jk_module modules/mod_jk.so
JkWorkersFile /etc/httpd/conf.d/workers.properties
JkLogFile logs/mod_jk.log
JkLogLevel notice
JkMount /* lbcA
JkMount /status/ statA


# cat workers.properties 
worker.list=lbcA,statA
worker.TomcatA.type=ajp13
worker.TomcatA.port=8009
worker.TomcatA.host=192.168.10.20
worker.TomcatA.lbfactor=1
worker.TomcatB.type=ajp13
worker.TomcatB.port=8009
worker.TomcatB.host=192.168.10.30 
worker.TomcatB.lbfactor=1
worker.lbcA.type=lb
worker.lbcA.sticky_session=1
worker.lbcA.balance_workers=TomcatA,TomcatB
worker.statA.type=status


配置后端tomcat
# cd /usr/local/tomcat/webapps/
# mkdir testapp
# cd testapp/
# mkdir -pv WEB-INF/{classes,lib}
mkdir: created directory `WEB-INF'
mkdir: created directory `WEB-INF/classes'
mkdir: created directory `WEB-INF/lib'
# vim index.jsp


TOMCATA服务器
index.jsp:
<%@ page language="java" %>

  TomcatA
 
   

TomcatA.chinasoft.com


   
     
       
    <% session.setAttribute("chinasoft.com.com","chinasoft.com"); %>
       
     
     
       
       
     
   
Session ID <%= session.getId() %>
Created on <%= session.getCreationTime() %>

 



TOMCATB服务器
index.jsp:
<%@ page language="java" %>

  TomcatB
 
   

TomcatB.chinasoft.com


   
     
       
    <% session.setAttribute("chinasoft.com","chinasoft.com"); %>
       
     
     
       
       
     
   
Session ID <%= session.getId() %>
Created on <%= session.getCreationTime() %>

 



测试是否OK
# curl http://192.168.10.20:8080/testapp/index.jsp
# curl http://192.168.10.30:8080/testapp/index.jsp

使用apache和nginx代理实现tomcat负载均衡及集群配置详解_第5张图片


配置session会话绑定(会破坏负载均衡效果):
负载均衡,且实现会话绑定要注意给每个tomcat实例的egine容器一个jvmRoute属性!此名称要跟前端调度模块使用名称保持一致!
另外,在mod_proxy实现负载均衡的会话绑定时,还要使用sticksession=JSESSIONID(字符要大写)!
worker.properties:
worker.TomcatB.lbfactor=1
在后端tomcat服务器server.xml文件中定义jvmRoute:


使用apache和nginx代理实现tomcat负载均衡及集群配置详解_第6张图片

使用apache和nginx代理实现tomcat负载均衡及集群配置详解_第7张图片
④.apache mod_proxy实现基于http的负载均衡配置:



# mv mod_jk.conf mod_jk.conf.bak
# mv mod_proxy.conf.bak mod_proxy.conf
# vim mod_proxy.conf 


ProxyVia on
ProxyRequests off
ProxyPreserveHost on



  BalancerMember http://192.168.10.20:8080 loadfactor=1 route=TomcatA
  BalancerMember http://192.168.10.30:8080 loadfactor=1 route=TomcatB



ProxyPass / balancer://lb/
ProxyPassReverse / balancer://lb/



  Order Allow,Deny
  Allow from all



⑤.配置session会话的持久性



ProxyVia on
ProxyRequests off
ProxyPreserveHost on



  BalancerMember http://192.168.10.20:8080 loadfactor=1 route=TomcatA
  BalancerMember http://192.168.10.30:8080 loadfactor=1 route=TomcatB



ProxyPass / balancer://lb/ stickysession=JSESSIONID
ProxyPassReverse / balancer://lb/



  Order Allow,Deny
  Allow from all


使用apache和nginx代理实现tomcat负载均衡及集群配置详解_第8张图片

⑥.配置管理接口

ProxyVia on
ProxyRequests off
ProxyPreserveHost on



  BalancerMember http://192.168.10.20:8080 loadfactor=1 route=TomcatA
  BalancerMember http://192.168.10.30:8080 loadfactor=1 route=TomcatB




  SetHandler balancer-manager



ProxyPass /lbmanager !
ProxyPass / balancer://lb/
ProxyPassReverse / balancer://lb/



  Order Allow,Deny
  Allow from all

使用apache和nginx代理实现tomcat负载均衡及集群配置详解_第9张图片

访问:http://192.168.8.48/lbmanager,可以看到负载均衡的状态界面



集群配置:

负载均衡器:

192.168.10.10:
# vim mod_proxy.conf


ProxyVia on
ProxyRequests off
ProxyPreserveHost on



  BalancerMember http://192.168.10.20:8080 loadfactor=1 route=TomcatA
  BalancerMember http://192.168.10.30:8080 loadfactor=1 route=TomcatB




  SetHandler balancer-manager



ProxyPass /lbmanager !
ProxyPass / balancer://lb/
ProxyPassReverse / balancer://lb/



  Order Allow,Deny
  Allow from all


192.168.10.20:






  


  
    
    


  
      
    
    
    
  


  
  


  


  
  



192.168.10.30:






  


  
    
    
	



















并在各集群节点上添加路由信息:
route add -net 228.1.0.4 netmask 255.255.255.255 dev eth0


并在:

web.xml节点上添加:


使用apache和nginx代理实现tomcat负载均衡及集群配置详解_第10张图片

可以看到session id没有变化

使用apache和nginx代理实现tomcat负载均衡及集群配置详解_第11张图片

转载于:https://www.cnblogs.com/reblue520/p/6239794.html

你可能感兴趣的:(使用apache和nginx代理实现tomcat负载均衡及集群配置详解)