Linux安全网 - Linux操作系统_Linux 命令_Linux教程_Linux黑客

会员投稿 投稿指南 本期推荐:
搜索:
您的位置: Linux安全网 > Linux集群 > 负载均衡 > » 正文

Apache+tomcat集群和负载均衡使用mod_proxy总结

来源: wsl3511 分享至:

 

  1. <VirtualHost *:80> 
  2.     ServerAdmin xy5300@qq.com 
  3.     ServerName localhost 
  4.     ServerAlias localhost   
  5.     ProxyPass / balancer://cluster/ stickysession=jsessionid nofailover=On 
  6.     ProxyPassReverse / balancer://cluster/ 
  7.     ErrorLog "logs/clustertest-error.log"     
  8.     CustomLog "logs/clustertest-access.log" common 
  9. </VirtualHost> 

0.集群,首先安装jdk,tomcat,apache。

安装apche的命令

[root@localhost httpd-2.2.18]#./configure --prefix=/usr/local/apache --enable-so --enable-mods-shared="proxy proxy_http proxy_ftp proxy_connect proxy_balancer headers" 

[root@localhost httpd-2.2.18]#make   

[root@localhost httpd-2.2.18]#make install 

之所以但说这个安装,是因为我们使用的是mod_proxy,安装后确定

[root@localhost ~]# ll /opt/apache2/modules/
总计 636
-rw-r--r-- 1 root root   9149 10-24 15:59 httpd.exp
-rwxr-xr-x 1 root root  42995 10-24 16:00 mod_headers.so
-rwxr-xr-x 1 root root 100940 10-24 16:00 mod_proxy_ajp.so
-rwxr-xr-x 1 root root  57525 10-24 16:00 mod_proxy_balancer.so
-rwxr-xr-x 1 root root  29815 10-24 16:00 mod_proxy_connect.so
-rwxr-xr-x 1 root root  72951 10-24 16:00 mod_proxy_ftp.so
-rwxr-xr-x 1 root root  75321 10-24 16:00 mod_proxy_http.so
-rwxr-xr-x 1 root root  41224 10-24 16:00 mod_proxy_scgi.so
-rwxr-xr-x 1 root root 178832 10-24 16:00 mod_proxy.so

下面要有这几个*.so的文件。

1.然后修改httpd.conf文件

 1.1:Include conf/extra/httpd-vhosts.conf前面的#去掉。(是为了连接httpd-vhosts这个文件,使用其配置)

 1.2:最下面添加:

  1. ProxyRequests Off 
  2. <proxy balancer://cluster> 
  3.   BalancerMember ajp://127.0.0.1:6045 loadfactor=1 route=tomcat_test1 
  4.   BalancerMember ajp://127.0.0.1:6055 loadfactor=1 route=tomcat_test2 
  5. </proxy> 

 注意:127.0.0.1是本机,可以换为其它IP。6045是端口,tomcat里server.xml的端口,这个端口是:<Connector port="6045" protocol="AJP/1.3" redirectPort="8443" />。

2.修改apache目录下conf/extra/httpd-vhosts.conf

把这个字段<VirtualHost *:80>前面都加上#,然后在最下面添加

 

其中的的virtualHost*:80为HttpServer监听端口。

 

ServerAdmin:管理员邮箱

 

ServerName: 服务器名称

 

ServerAlias: 配置服务器域名或IP,如果有多个域名或者域名和IP同时使用,则使用空格隔开。

 

ProxyPass / balancer:// 是告诉Apache需要进行负载均衡的代理,后面的cluster是集群名,可以随意取,转发时带上session的名称。

 

ProxyPassReverse 这行配置的含义是,使Apache调整HTTP重定向应答中Location, Content-Location, URI头里的URL。这样可以避免在Apache作为反向代理使用时,后端服务器的HTTP重定向造成的绕过反向代理的问题。

 

两个日志引擎日志名可以自己取

 

ErrorLog负责记录错误日志

 

CustomLog负责记录所有的http访问以及返回状态

3.修改tomcat。

  3.1如果同一台机器2个tomcat则需要修改3个端口,保持不一样,反之跳过3.1步骤。

  <Server port="6041" shutdown="SHUTDOWN"> (关闭端口)

   <Connector port="6043" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />  (连接端口)

<Connector port="6045" protocol="AJP/1.3" redirectPort="8443" />  (集群链连接端口)

  3.2 找到删除,并添加

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

tomcat_test1 是对应httpd.conf里的route=tomcat_test1参数

并在下面添加:

 

  1. <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"   
  2.                  channelSendOptions="8">   
  3.    
  4.           <Manager className="org.apache.catalina.ha.session.DeltaManager"   
  5.                    expireSessionsOnShutdown="false"   
  6.                    notifyListenersOnReplication="true"/>   
  7.    
  8.           <Channel className="org.apache.catalina.tribes.group.GroupChannel">   
  9.             <Membership className="org.apache.catalina.tribes.membership.McastService"   
  10.                         address="228.0.0.4"   
  11.                         port="45564"   
  12.                         frequency="500"   
  13.                         dropTime="3000"/>   
  14.             <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"   
  15.                       address="auto"   
  16.                       port="4000"   
  17.                       autoBind="100"   
  18.                       selectorTimeout="5000"   
  19.                       maxThreads="6"/>   
  20.    
  21.             <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">   
  22.               <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>   
  23.             </Sender>   
  24.             <Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>   
  25.             <Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>   
  26.           </Channel>   
  27.    
  28.           <Valve className="org.apache.catalina.ha.tcp.ReplicationValve"   
  29.                  filter=""/>   
  30.    
  31.           <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"   
  32.                     tempDir="/tmp/war-temp/"   
  33.                     deployDir="/tmp/war-deploy/"   
  34.                     watchDir="/tmp/war-listen/"   
  35.                     watchEnabled="false"/>   
  36.    
  37.           <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>   
  38.         </Cluster>   

两个tomcat都要做修改,其中注意上面代码里面的part="4000",两个tomcat在同一台服务器上,要修改为不同的,如4001.

3.3 添加<distributable/> 这个是最容易忽略的。

在所使用的工程应用下,web.xml里倒数第二行添加<distributable/>即可。

4.测试了

 4.1测试负载均衡

 4.2测试session复制

上面两个测试在上一篇文章有提到,可能比较乱。

 把下面的两个下载放到tomcat里面webapps里面然后重启进行测试。

 

 


Tags:
分享至:
最新图文资讯
1 2 3 4 5 6
验证码:点击我更换图片 理智评论文明上网,拒绝恶意谩骂 用户名:
关于我们 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 发展历史