Oracle的负载均衡技术是Oracle rac提供的主要技术之一,目的是通过算法把大量的连接分到不同的节点上去,从而实现负载的均衡。不过这种算法计算的是连接数,而不是节点上的真实负载,所以有些时候我们会发现,应用分布并不是十分的理想。对于oracle10g来说,也可以通过service来限制连接在某个或某些实例上,从而达到负载均衡的效果。
负载均衡分为3种
1.服务器端的负载均衡
2.客户端的负载均衡
3.服务器端和客户端混合的负载均衡
一、客户端LB(load banlance)测试
修改参数remote_listener,取消服务器端load banlance功能
[oracle@rac1 workshop]$ sqlplus / as sysdba
SQL*Plus: Release 10.2.0.4.0 - Production on Thu Aug 9 19:59:48 2012
Copyright (c) 1982, 2007, Oracle. All Rights Reserved.
Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bit Production
With the Partitioning, Real Application Clusters, OLAP, Data Mining
and Real Application Testing options
SQL> alter system set remote_listener='' scope=spfile sid='*';
System altered.
SQL> exit
Disconnected from Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bit Production
With the Partitioning, Real Application Clusters, OLAP, Data Mining
and Real Application Testing options
重启数据库和两个节点上的监听程序
[oracle@rac1 workshop]$ srvctl stop database -d rac
[oracle@rac1 workshop]$ srvctl stop listener -n rac1
[oracle@rac1 workshop]$ srvctl stop listener -n rac2
[oracle@rac1 workshop]$ srvctl start listener -n rac1
[oracle@rac1 workshop]$ srvctl start listener -n rac2
[oracle@rac1 workshop]$ srvctl start database -d rac
清空两个节点(rac1,rac2)的监听器log日志
[oracle@rac1 log]$ cat /dev/null>listener_rac1.log
[oracle@rac1 log]$ more listener_rac1.log
[oracle@rac2 log]$ cat /dev/null>listener_rac2.log
[oracle@rac2 log]$ more listener_rac2.log
在客户端机器,执行测试shell脚本,统计通过客户端负载均衡到每个节点的的连接数
[oracle@boss workshop]$ more test.sh
#!/bin/bash
i=1
while [ $i -le $2 ]; do
i=$((i+1))
sqlplus -s system/oracle@$1 @test.sql
done
以上脚本中的$1表示客户端连接字符串,本例中是RAC,$2是连接数
[oracle@boss workshop]$ more test.sql
select instance_name from v$instance
/
exit
[oracle@boss workshop]$ more $ORACLE_HOME/network/admin/tnsnames.ora
RAC =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = rac1-vip)(PORT = 1521))
(ADDRESS = (PROTOCOL = TCP)(HOST = rac2-vip)(PORT = 1521))
(LOAD_BALANCE = yes)
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = rac)
)
)
[oracle@boss workshop]$ ./test.sh rac 1000 > test.log
[oracle@boss workshop]$ grep rac1 test.log |wc -l
527
[oracle@boss workshop]$ grep rac2 test.log |wc -l
473
通过分析test.log我们发现,oracle基本做到了负载均衡
二、服务器端LB(load banlance)测试
在服务器端修改参数remote_listener,配置oracle数据库使用服务器端的load balance
[oracle@rac1 workshop]$ sqlplus / as sysdba
SQL*Plus: Release 10.2.0.4.0 - Production on Thu Aug 9 20:30:47 2012
Copyright (c) 1982, 2007, Oracle. All Rights Reserved.
Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bit Production
With the Partitioning, Real Application Clusters, OLAP, Data Mining
and Real Application Testing options
SQL> alter system set remote_listener=listeners_rac scope=spfile sid='*';
System altered.
SQL> exit
重启数据库和两个节点上的监听程序
[oracle@rac1 log]$ srvctl stop database -d rac
[oracle@rac1 log]$ srvctl stop listener -n rac1
[oracle@rac1 log]$ srvctl stop listener -n rac2
[oracle@rac1 log]$
[oracle@rac1 log]$ srvctl start listener -n rac1
[oracle@rac1 log]$ srvctl start listener -n rac2
[oracle@rac1 log]$ srvctl start database -d rac
清空两个节点(rac1,rac2)的监听器log日志
[oracle@rac1 log]$ cat /dev/null>listener_rac1.log
[oracle@rac1 log]$ more listener_rac1.log
[oracle@rac2 log]$ cat /dev/null>listener_rac2.log
[oracle@rac2 log]$ more listener_rac2.log
在客户端机器,执行测试shell脚本,统计通过客户端负载均衡到每个节点的的连接数
[oracle@boss workshop]$ more test.sh
#!/bin/bash
i=1
while [ $i -le $2 ]; do
i=$((i+1))
sqlplus -s system/oracle@$1 @test.sql
done
以上脚本中的$1表示客户端连接字符串,本例中是RAC,$2是连接数
[oracle@boss workshop]$ more test.sql
select instance_name from v$instance
/
exit
[oracle@boss workshop]$ more $ORACLE_HOME/network/admin/tnsnames.ora
RAC =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = rac1-vip)(PORT = 1521))
(ADDRESS = (PROTOCOL = TCP)(HOST = rac2-vip)(PORT = 1521))
(LOAD_BALANCE = off)
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = rac)
)
)
[oracle@boss workshop]$ grep rac1 test.log |wc -l
402
[oracle@boss workshop]$ grep rac2 test.log |wc -l
398
通过分析test.log我们发现,oracle基本做到了负载均衡
在节点1(rac1)上执行下面语句,确认直接到节点1的连接数,和由节点2通过负载均衡算法路由过来的连接数
[oracle@rac1 log]$ grep establish listener_rac1.log |wc -l
800
[oracle@rac1 log]$ grep INSTANCE_NAME=rac listener_rac1.log | wc -l
0
通过以上的显示结果我们可以分析出直接连接到节点1的连接数是800,其中800-402=398由节点1负载均衡到节点2上,从节点2通过负载均衡算法路由过来的连接数0.
在节点2(rac2)上执行下面语句,确认连接到节点2的连接数,和从节点1通过负载均衡算法路由过来的连接数
[oracle@rac2 log]$ grep establish listener_rac2.log | wc -l
398
[oracle@rac2 log]$ grep INSTANCE_NAME=rac listener_rac2.log | wc -l
398
通过以上的显示结果我们可以分析出连接到节点2的连接数是398,这些连接都是由节点1通过负载均衡算法路由过来的连接数.
以上两个节点的信息可以互相验证。