Apache2+Resin3集群实现负载均衡

Apache2(前端)+ Resin(后端集群)可以很好的实现负载均衡。Apache用来服务静态页面,Resin用来服务动态页面。

实验环境

10.130.41.233: 前端服务器,Apache2
10.130.41.233: 后端服务器b,resin-3.0.25
10.130.41.232: 后端服务器a,resin-3.0.25

编译mod_caucho.so

  • 在10.130.41.233机器下载resin-3.0.25.tar.gz,解压:tar -zxf resin-3.0.25.tar.gz
  • 进入resin-3.0.25目录:cd /home/portal/resin-3.0.25
  • 编译mod_caucho.so:
./configure --with-apxs=/usr/local/apache2/bin/apxs
make
make install
注意:apache2以root安装的,所以make install时需要root权限
  • 编译完后,会在/usr/local/apache2/modules/目录下生成mod_caucho.so文件。并且自动更新apache2的配置文件httpd.conf,添加下面的内容
LoadModule caucho_module /usr/local/apache2/modules/mod_caucho.so
ResinConfigServer localhost 6802
CauchoConfigCacheDirectory /tmp
CauchoStatus yes

配置后端resin

需要修改2台后端resin服务器的配置文件resin.conf,如果有多个resin,均按此方式配置

找到配置文件中的 <!— The local cluster, used for load balancing and distributed backup.–>,修改负载均衡配置,需要设置服务器的ID、IP和port,例如:

10.130.41.233的设置如下:

<cluster>
<srun server-id="b" host="10.130.41.233" port="6802"/>
</cluster>

10.130.41.232的设置如下:

<cluster>
<srun server-id="a" host="10.130.41.232" port="6802"/>
</cluster>

配置前端的Apache

修改httpd.conf内容,增加ResinConfigServer。如下:

LoadModule caucho_module /usr/local/apache2/modules/mod_caucho.so
ResinConfigServer 10.130.41.232 6802
ResinConfigServer 10.130.41.233 6802
CauchoConfigCacheDirectory /tmp
CauchoStatus yes

ResinConfigServer配置的是调用resin负载均衡器的IP地址和端口号,前端服务器apache,分别调用了10.130.41.233和10.130.41.232两个Resin后端服务器

测试

5.1 编写测试页面
为了检查负载均衡是否生效,需要编写测试页面test.jsp,通过在web页面和各Resin的日志中显示的内容进行辨别,下面10.130.41.232的其中一个Resin为例说明:

<%System.out.println("server 10.130.41.232");%> //在Resin日志中显示

server 10.130.41.232 //在web页面显示

其他Resin的test.jsp参照上例,只需要将其中的内容替换掉,例如10.130.41.233的test.jsp文件可以写成

<%System.out.println("server 10.130.41.233");%> //在Resin日志中显示

server 10.130.41.233 //在web页面显示

5.2 发布项目

分别将来两个test.jsp文件放到10.130.41.232和10.130.41.233设置的resin的webapps/ROOT目录下

5.3 启动服务

#启动Apache2

#/usr/local/apache2/bin/apachectl -k start

#分别启动调用的resin

启动10.130.41.232的resin中id为"a"的服务,需要在10.130.41.232服务器执行如下命令

$ /home/portal/resin-3.0.25/bin/httpd.sh -server a start

启动10.130.41.233的resin中id为"b"的服务,需要在10.130.41.233服务器执行如下命令

$ /home/portal/resin-3.0.25/bin/httpd.sh -server b start

5.4 验证test.jsp

使用IE多次访问web发布页面,通过页面显示的内容和resin日志打印的内容,检查是否随机调用各后端服务器1&2的resin的test.jsp文件。

Web页面的访问地址如下

http://10.130.41.233/test.jsp

多次访问http://10.130.41.233/test.jsp。如果每次会随机显示其中一个resin的a.jsp文件的内容,之后关闭其中个几个resin(不是全部关闭),http://10.130.41.233/test.jsp 仍能正常访问,至此就完成了负载均衡的测试,证明配置成功。

参考

1. Resin with Apache

Advertisement
This entry was posted in 计算机与 Internet. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s