apache整合tomcat实现负载均衡

这种整合方式能够使apache成为反向代理,将请求均匀地转发到多台tomcat之一,从而达到负载均衡的目的。
负载均衡的方式我知道有三种:
1.mod_proxy方式。
2.mod_proxy_blancer方式
3.ajp方式。
本文只介绍mod_proxy_blancer方式。

系统版本:
Ubuntu 14.04.1 LTS

内核版本:
3.13.0-32-generic

一、apache的安装
1.准备以下资材
apr-1.5.2.tar.gz
apr-util-1.5.4.tar.gz
pcre-8.37.tar.gz(注意:不要使用pcre2,之前用了以后,configure httpd时报错了)
httpd-2.4.18.tar.gz

2.安装gcc,g++和make
#apt-get install gcc
#apt-get install make
#apt-get install g++

2.安装apr
#tar xzvf apr-1.5.2.tar.gz
#cd apr-1.5.2
#./configure --prefix=/usr/local/apr-1.5.2
#make
#make install

3.安装apr-util
#tar xzvf apr-util-1.5.4.tar.gz
#cd apr-util-1.5.4
#./configure --prefix=/usr/local/apr-util-1.5.4 --with-apr=/usr/local/apr-1.5.2
#make
#make install

4.安装pcre
#tar xzvf pcre-8.37.tar.gz
#cd pcre-8.37/
#./configure --prefix=/usr/local/pcre-8.37
#make
#make install

5.安装httpd
#tar xzvf httpd-2.4.18.tar.gz
#cd httpd-2.4.18/
#./configure --with-apr=/usr/local/apr-1.5.2 --with-apr-util=/usr/local/apr-util-1.5.4 --with-pcre=/usr/local/pcre-8.37 --prefix=/usr/local/httpd-2.4.18 --enable-so
#make
#make install

6.启动httpd
#/usr/local/httpd-2.4.18/bin/apachectl start
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message
提示找不到服务器的域名,进行如下修改:
# vi /usr/local/httpd-2.4.18/conf/httpd.conf
......
#ServerName www.example.com:80
ServerName localhost:80  <= 增加该行
......
重启服务器
# /usr/local/httpd-2.4.18/bin/apachectl restart

浏览器输入以下url,出现it works!,代表安装成功。
http://<http server ip>

二、tomcat安装
1.准备以下资材
jdk-7u79-linux-x64.tar.gz
apache-tomcat-8.0.30.tar.gz

2.安装jdk
#cd /usr/local
#tar xzvf /root/jdk-7u79-linux-x64.tar.gz
#vi /etc/profile
#文件最下方添加如下环境变量:
export JAVA_HOME=/usr/local/jdk1.7.0_79
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
立即使环境变量生效。
#source /etc/profile
确认安装
#java -version
java version "1.7.0_79"
Java(TM) SE Runtime Environment (build 1.7.0_79-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.79-b02, mixed mode)

3.安装tomcat1
#cd /usr/local
#tar xzvf /root/apache-tomcat-8.0.30.tar.gz
启动tomcat1
#/usr/local/apache-tomcat-8.0.30/bin/startup.sh
Using CATALINA_BASE:   /usr/local/apache-tomcat-8.0.30
Using CATALINA_HOME:   /usr/local/apache-tomcat-8.0.30
Using CATALINA_TMPDIR: /usr/local/apache-tomcat-8.0.30/temp
Using JRE_HOME:        /usr/local/jdk1.7.0_79
Using CLASSPATH:       /usr/local/apache-tomcat-8.0.30/bin/bootstrap.jar:/usr/local/apache-tomcat-8.0.30/bin/tomcat-juli.jar
Tomcat started.
通过浏览器输入以下url进入tomcat的管理页面:
http://<tomcat server ip>:8080/

4.安装tomcat2
#cd /opt
#tar xzvf /root/apache-tomcat-8.0.30.tar.gz
修改tomcat2的端口为9080
#vi /opt/apache-tomcat-8.0.30/conf/server.xml
    <Connector port="9080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />

修改AJP的端口为9009
<Connector port="9009" protocol="AJP/1.3" redirectPort="8443" />

启动tomcat2
# /opt/apache-tomcat-8.0.30/bin/startup.sh
Using CATALINA_BASE:   /opt/apache-tomcat-8.0.30
Using CATALINA_HOME:   /opt/apache-tomcat-8.0.30
Using CATALINA_TMPDIR: /opt/apache-tomcat-8.0.30/temp
Using JRE_HOME:        /usr/local/jdk1.7.0_79
Using CLASSPATH:       /opt/apache-tomcat-8.0.30/bin/bootstrap.jar:/opt/apache-tomcat-8.0.30/bin/tomcat-juli.jar
Tomcat started.
通过浏览器输入以下url进入tomcat的管理页面:
http://<tomcat server ip>:9080/

三、整合apache和tomcat
1.修改/usr/local/httpd-2.4.18/conf/httpd.conf,为以下内容解除注释
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule slotmem_shm_module modules/mod_slotmem_shm.so
LoadModule lbmethod_byrequests_module modules/mod_lbmethod_byrequests.so

并在文件的最下方添加如下内容:
ProxyPass / balancer://proxy/
<Proxy balancer://proxy>
  BalancerMember http://127.0.0.1:8080/ loadfactor=1
  BalancerMember http://127.0.0.1:9080/ loadfactor=2
</Proxy>

如果遇到为/的请求,就让balancer://proxy来处理,http server会分发到tomcat1和tomcat2,
loadfactor值越大,被分发到请求的概率也越大。

2.分别在tomcat1和tomcat2的webapps目录下部署firstweb.war,
其中tomcat1中的firstweb.war的index.jsp内容为:
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
   you are in tomcat1!
</body>
</html>

tomcat2中的firstweb.war的index.jsp内容为:
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
   you are in tomcat2!
</body>
</html>

3.重启http、tomcat1和tomcat2。在浏览器中执行以下url 5 次:
http://<http server ip>/firstweb/

分别显示如下内容:
you are in tomcat1!
you are in tomcat2!
you are in tomcat2!
you are in tomcat1!
you are in tomcat2!
you are in tomcat2!
可以看到tomcat1中被firstweb被执行到概率为tomcat2的1/2.

写在最后:
到这边虽然loadbalancer的功能基本实现了,但是还有很多需要完善的地方,比如tomcat session的同步等。
等有时间再进行完善。

你可能感兴趣的:(apache整合tomcat实现负载均衡)