Apache Tomcat 集群配置 (超简单 详细)

Apache Tomcat集群方式主要有两类,一类是JK方式,不过这个感觉是过时了,而且配置繁琐,网上有很多JK配置的例子,还有一类就是Proxy方式,新版本的Apache都集成了集群配置,下面讲解的是Proxy方式


下载配置Tomcat

将下载好的tomcat复制两份,http://mirror.bit.edu.cn/apache/tomcat/tomcat-7/v7.0.77/bin/apache-tomcat-7.0.77-windows-x64.zip

Apache Tomcat 集群配置 (超简单 详细)_第1张图片


因为是windows下面的本地测试,所以要改端口号,实际不同的主机不需要改

1、分别找到conf下面的server.xml文件,打开,修改如下端口配置,AJP分别与下面的ajp端口对应


<Connector port="8090" protocol="HTTP/1.1"
               connectionTimeout="20000"

<Connector port="8881" protocol="AJP/1.3" redirectPort="8443" />



<Connector port="8091" protocol="HTTP/1.1"
               connectionTimeout="20000"

<Connector port="8882" protocol="AJP/1.3" redirectPort="8443" />

2、添加jvmRoute,分别找到Engine标签,修改如下,jvmRoute的tomcat1和tomcat2分别与下面的route对应

    <Engine name="Catalina" defaultHost="localhost"  jvmRoute="tomcat1">
    <Engine name="Catalina" defaultHost="localhost"  jvmRoute="tomcat2">

3、分别在Tomcat下面的webapps下面新建test文件夹,在test下面新建index.jsp,内容如下

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title heretitle>
head>
<body>
<%
System.out.println("hello");
%>
it work
body>
html>

4、开启Tomcat,打开网页,分别输入路径 http://localhost:8090/test/index.jsp http://localhost:8091/test/index.jsp,控制台打印hello,并出现 it work,说明Tomcat配置成功

Apache Tomcat 集群配置 (超简单 详细)_第2张图片


这里写图片描述

下载配置Apache http://blog.csdn.net/qq_28883885/article/details/70210990 将端口改成9991


配置集群

1、打开Apache conf下面的httpd.conf,找到下面这些,去掉文本前的注释符(#)

LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
LoadModule rewrite_module modules/mod_rewrite.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
LoadModule slotmem_shm_module modules/mod_slotmem_shm.so
LoadModule speling_module modules/mod_speling.so
LoadModule ssl_module modules/mod_ssl.so

2、找到“DirectoryIndex index.html”字样,在其后追加index.jsp

<IfModule dir_module>
    DirectoryIndex index.html index.jsp
IfModule>

3、找到“Include conf/extra/httpd-vhosts.conf”字样,去掉前面的注释符

# Virtual hosts
Include conf/extra/httpd-vhosts.conf

4、在文件末尾加反向代理,loadfactor为权限,这里设置都为1,权限一样,ajp://127.0.0.1:8881 loadfactor=1 route=tomcat1与Tomcat里面server.xml 配置对应

ProxyRequests Off 
//cluster> 
     BalancerMember ajp://127.0.0.1:8881 loadfactor=1 route=tomcat1 
     BalancerMember ajp://127.0.0.1:8882 loadfactor=1 route=tomcat2
 

5、找到conf下面的httpd-vhosts.conf,打开修改 VirtualHost,如下

 
         ServerAdmin [email protected] 
         ServerName localhost 
         ServerAlias localhost 
         ProxyPass / balancer://cluster/ stickysession=jsessionid nofailover=On 
         ProxyPassReverse / balancer://cluster/ 
         ErrorLog "logs/lbtest-error.log" 
         CustomLog "logs/lbtest-access.log" common 

测试

分别打开Apache,Tomcat1和Tomcat2,连续刷新http://localhost:9991/test/index.jsp,可以看到Tomcat1和Tomcat2依次打印Hello,说明配置集群成功

Apache Tomcat 集群配置 (超简单 详细)_第3张图片

你可能感兴趣的:(apache,集群)