目的是将 http://127.0.0.1/demo 负载均衡转发到 http://127.0.0.1/demo1和http://127.0.0.1/demo2
第一步,开启项目模块:
1
2
3
4
5
|
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule lbmethod_byrequests_module modules/mod_lbmethod_byrequests.so
LoadModule slotmem_shm_module modules/mod_slotmem_shm.so
|
第二步,负载均衡代理配置,写在httpd.conf文件下面:
可配置一个或者多个负载代理,democluster 为该代理的命名,后面需要通过引用该名字调用负载均衡转发
1
2
3
4
5
|
ProxyRequests Off
<Proxy balancer: BalancerMember http: BalancerMember http: </Proxy>
|
开启负载均衡管理界面 http://127.0.0.1/balancer-manager,生产环境慎重开启:
1
2
3
4
|
<Location /balancer-manager>
SetHandler balancer-manager
Require all granted
</Location>
|
(Apache2.4:Require all granted,Apache2.2:Allow from all)
第三步,应用负载均衡,在虚拟服务器中使用,也可以直接根配置(httpd.conf)中使用:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<VirtualHost 127.0.0.1:80>
ServerAdmin webmaster@dummy-host.example.com
ServerName 127.0.0.1
ProxyRequests Off
ProxyPreserveHost On
<Proxy *>
Require all granted
</Proxy>
ProxyPass /demo balancer: ProxyPassReverse /demo balancer: ErrorLog "logs/test.localhost-error_log"
CustomLog "logs/test.localhost-access_log" common
</VirtualHost>
|
其他,也可以将第二步中的负载均衡代理配置,直接配置到虚拟主机中,如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<VirtualHost 127.0.0.1:80>
ServerAdmin webmaster@dummy-host.example.com
ServerName 127.0.0.1
ProxyRequests Off
ProxyPreserveHost On
<Proxy balancer: BalancerMember http: BalancerMember http: </Proxy>
ProxyPass /demo balancer: ProxyPassReverse /demo balancer: ErrorLog "logs/test.localhost-error_log"
CustomLog "logs/test.localhost-access_log" common
</VirtualHost>
|
最后 :重启即可
-------------------------------------------------------------------------------------
相关配置和策略说明
负载权重设置,按照比例设置分发权重,在分发链接后面加上loadfactor:
1
2
3
4
|
<Proxy balancer: BalancerMember http: BalancerMember http: </Proxy>
|
分发策略设置,通过设置lbmethod设置分发策略:
lbmethod:表示负载载均衡策略。
lbmethod=byrequests :按照请求次数均衡(默认)
lbmethod=bytraffic: 按照流量均衡
lbmethod=bybusyness :按照繁忙程度均衡(总是分配给活跃请求数最少的服务器)
1
2
3
4
5
|
<Proxy balancer: BalancerMember http: BalancerMember http: ProxySet lbmethod=byrequests
</Proxy>
|
1
|
ProxyPass /demo balancer:
|
热备份配置,热备份就是所有请求都发到一台服务器上,当这台服务器宕机之后,请求会被发到另一台热备份服务器上,使用status=+H :
1
2
3
4
|
<Proxy balancer: BalancerMember http: BalancerMember http: </Proxy>
|
会话保持,就是将同一个用户(同一个session)的请求总是转发到同一台服务器上去,这样就可以不用做session同步,主要配置方法可参考 Apache + Tomcat 的实现方式,自行百度即可。或者参考文章:http://www.blogjava.net/gentoo1439/archive/2007/07/11/129527.html
(责任编辑:IT) |