[WK-T]ORACLE 10G 配置負載均衡(LoadBalance)

dayong2015發表於2014-08-22
文章參考:http://www.cnblogs.com/millen/archive/2009/05/06/1450844.html 
ORACLE負載均衡主要是指新會話連線到RAC資料庫時,如何判定這個新的連線要連到哪個節點進行工作。通常情況下,負載均衡分為客戶端的負載均衡和伺服器端的負載均衡。客戶端負載均衡通常是在tnsnames.ora中多新增一個連結地址以及load_balance和failover引數。而伺服器端的負載均衡相對複雜。

一.客戶端的負載均衡(Client-Side LB)
工作原理:當客戶端發起連線時,會從地址列表中隨機選取一個,再使用隨機演算法把連線請求分散到各個例項。
1)分配連線時沒有考慮每個節點的真實負載,最後分配不過不一定是平衡
2)隨機演算法需要長時間片,如果在短時間內同時發起多個連線,這些連線有可能被分配到一個節點上
3)有些情況下,連線可能被分配到故障節點上
配置方法:在tns中新增LOAD_BALANCE = YES條目
1.查詢當前伺服器中資料庫的版本
SQL> select * from v$version;

BANNER
--------------------------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - 64bi
PL/SQL Release 10.2.0.1.0 - Production
CORE    10.2.0.1.0      Production
TNS for Linux: Version 10.2.0.1.0 - Production
NLSRTL Version 10.2.0.1.0 - Production
2.在客戶端的tnsnames.ora的配置中,只要連線的是整個資料庫的服務名,不是例項名。
在伺服器端檢視RAC資料庫的service_names:
SQL> show parameter service_names;

NAME                                 TYPE       VALUE
------------------------------------ ---------- ------------------------------
service_names                        string     orcl
3.在客戶端配置TNS:
客戶端的負載均衡配置相對簡單,只需要在tnsnames.ora中新增 LOAD_BALANCE = yes這麼一個選項即可。

配置TNS中的HOST值是伺服器端RAC配置中的虛擬IP即VIP,如下:
[oracle@rac1 ~]$ cat /etc/hosts
# Do not remove the following line, or various programs
# that require network functionality will fail.
#127.0.0.1              rac1 localhost.localdomain localhost
#::1            localhost6.localdomain6 localhost6
127.0.0.1       localhost
#public ip
192.168.1.200   rac1
192.168.1.201   rac2
#private ip
10.0.0.1        rac1-priv
10.0.0.2        rac2-priv
#virtual ip
192.168.1.210   rac1-vip
192.168.1.211   rac2-vip
4.在客戶端測試


總結:這樣當客戶端連線RAC資料庫時,會隨機在TNS裡面挑個監聽地址進行連線。在Oracle 10g以前,假如有節點當機或者類似事故時,客戶端可能還是選擇連線到這個節點,這樣會發生較長時間的TCP等待超時。而在10g以後,由於VIP和FAN的引入,這樣的情況可以得到很大程度的改善。客戶端的負載均衡在通常情況下能夠較好地工作,但是由於連線是在客戶端隨機發起的,這樣客戶端並不知道RAC各節點的負荷及連線數情況,有可能負荷大的節點還會源源不斷地增加新的連線,導致RAC節點無法均衡工作。

二.伺服器端的負載均衡(Server-Side LB)
工作原理:
1)該均衡實現是依賴於Listener收集的負載資訊。在資料庫執行過程中,PMON後臺程式會每3秒會將各自節點的負荷(包括LOAD、最大LOAD、CPU使用率)及連線數更新到service_register裡面,然後假如節點的負荷有發生變化,將會通知到監聽程式,由監聽程式再決定新的客戶端連線分配至哪個節點。假如RAC中一個節點的監聽失敗了,PMON每一分鐘會去檢查一次是否已經恢復正常。
2)PMON程式不僅會向本地的Listener註冊,也會想其他節點上的Listener註冊,但到底向何處註冊,是由Remote_Listeners和Local_Listener這兩個引數決定。Local_Listener不用設定,而Remote_Listeners需要設定,引數值有一個tnsnames項。
3)當收到客戶端連線請求時,就會把連線轉給負載最小的節點,這個節點可能是自己,也可能是其他節點,也就是Listener會轉發客戶端的連線請求。
配置方法:
伺服器端的監聽配置是在各節點的tnsnames.ora裡面新增一個連線到各個節點監聽的條目,然後再在初始化引數裡面設定remote_listeners這個引數。
開始配置:
1.測試客戶端的TNS是否能夠正常連線資料庫
首先檢視客戶端tnsnames.ora的配置,如下:

測試連線:

2.配置伺服器端TNS
伺服器端的監聽配置是在各節點的tnsnames.ora裡面新增一個連線到各個節點監聽的條目,在伺服器端每個節點的tnsnames.ora裡面的內容如下:
[oracle@rac1 ~]$ cd /u01/app/oracle/db_1/network/admin/
[oracle@rac1 admin]$ ls
listener.ora  samples  shrept.lst  sqlnet.log  tnsnames.ora
[oracle@rac1 admin]$ cat tnsnames.ora
# tnsnames.ora.rac1 Network Configuration File: /u01/app/oracle/db_1/network/admin/tnsnames.ora.rac1
# Generated by Oracle configuration tools.

LISTENERS_ORCL =
  (ADDRESS_LIST =
    (ADDRESS = (PROTOCOL = TCP)(HOST = rac1-vip)(PORT = 1521))
    (ADDRESS = (PROTOCOL = TCP)(HOST = rac2-vip)(PORT = 1521))
  )

ORCL2 =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = rac2-vip)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = orcl)
      (INSTANCE_NAME = orcl2)
    )
  )

ORCL1 =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = rac1-vip)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = orcl)
      (INSTANCE_NAME = orcl1)
    )
  )

ORCL =
  (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 = orcl)
    )
  )

EXTPROC_CONNECTION_DATA =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC0))
    )
    (CONNECT_DATA =
      (SID = PLSExtProc)
      (PRESENTATION = RO)
    )
  )
3.在初始化引數中設定引數remote_listeners
[oracle@rac1 admin]$ export ORACLE_SID=orcl1
[oracle@rac1 admin]$ sqlplus / as sysdba

SQL*Plus: Release 10.2.0.1.0 - Production on Fri Aug 22 16:23:48 2014

Copyright (c) 1982, 2005, Oracle.  All rights reserved.

Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - 64bit Production
With the Partitioning, Real Application Clusters, OLAP and Data Mining options

SQL> show parameter remote_listener

NAME                                 TYPE       VALUE
------------------------------------ ---------- ------------------------------
remote_listener                      string     LISTENERS_ORCL
正確配置引數後,透過lsnrctl status命令看到在監聽啟動以後,可以看到監聽器上有2個instance。
[oracle@rac1 admin]$ lsnrctl status

LSNRCTL for Linux: Version 10.2.0.1.0 - Production on 22-AUG-2014 16:26:09

Copyright (c) 1991, 2005, Oracle.  All rights reserved.

Connecting to (ADDRESS=(PROTOCOL=tcp)(HOST=)(PORT=1521))
STATUS of the LISTENER
------------------------
Alias                     LISTENER_RAC1
Version                   TNSLSNR for Linux: Version 10.2.0.1.0 - Production
Start Date                22-AUG-2014 13:22:34
Uptime                    0 days 3 hr. 3 min. 35 sec
Trace Level               off
Security                  ON: Local OS Authentication
SNMP                      OFF
Listener Parameter File   /u01/app/oracle/db_1/network/admin/listener.ora
Listener Log File         /u01/app/oracle/db_1/network/log/listener_rac1.log
Listening Endpoints Summary...
  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=192.168.1.210)(PORT=1521)))
  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=192.168.1.200)(PORT=1521)))
Services Summary...
Service "+ASM" has 1 instance(s).
  Instance "+ASM1", status BLOCKED, has 1 handler(s) for this service...
Service "+ASM_XPT" has 1 instance(s).
  Instance "+ASM1", status BLOCKED, has 1 handler(s) for this service...
Service "PLSExtProc" has 1 instance(s).
  Instance "PLSExtProc", status UNKNOWN, has 1 handler(s) for this service...
Service "orcl" has 2 instance(s).
  Instance "orcl1", status READY, has 2 handler(s) for this service...
  Instance "orcl2", status READY, has 1 handler(s) for this service...
Service "orclXDB" has 2 instance(s).
  Instance "orcl1", status READY, has 1 handler(s) for this service...
  Instance "orcl2", status READY, has 1 handler(s) for this service...
Service "orcl_XPT" has 2 instance(s).
  Instance "orcl1", status READY, has 2 handler(s) for this service...
  Instance "orcl2", status READY, has 1 handler(s) for this service...
The command completed successfully
4.透過客戶端測試負載均衡

經過多次的客戶端連線資料庫操作,當例項orcl1的負載較大時,會將客戶端的請求轉發給例項orcl2

補充:
在使用客戶端sqlplus連線伺服器資料庫的時候出現ORA-12545錯誤

解決辦法:
方法1:
配置客戶端的Hosts檔案

方法2:配置引數local_listener
首先註釋掉客戶端配置的hosts檔案:

1)登入資料庫檢視local_listener的設定狀況
SQL> show parameter listener;

NAME                                 TYPE       VALUE
------------------------------------ ---------- ------------------------------
local_listener                       string
remote_listener                      string     LISTENERS_ORCL
在伺服器端rac1,rac2上分別編輯tnsnames.ora和listener.ora檔案,將兩個檔案中的host主機名字均修改為具體的VIP地址,如將下面部分:
tnsnames.ora檔案中:
LISTENERS_ORCL =
  (ADDRESS_LIST =
    (ADDRESS = (PROTOCOL = TCP)(HOST = rac1-vip)(PORT = 1521))
    (ADDRESS = (PROTOCOL = TCP)(HOST = rac2-vip)(PORT = 1521))
  )
修改為:
LISTENERS_ORCL =
  (ADDRESS_LIST =
    (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.210)(PORT = 1521))
    (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.211)(PORT = 1521))
  )
listener.ora檔案中:
LISTENER_RAC1 =
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = TCP)(HOST = rac1-vip)(PORT = 1521)(IP = FIRST))
      (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.200)(PORT = 1521)(IP = FIRST))
    )
  )
修改為:
LISTENER_RAC1 =
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.210)(PORT = 1521)(IP = FIRST))
      (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.200)(PORT = 1521)(IP = FIRST))
    )
  )
2)在rac1節點的tnsnames.ora增加如下內容
LOCAL_LISTENER_RAC =
  (ADDRESS_LIST =
    (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.210)(PORT = 1521))
  )
3)在rac2節點的tnsnames.ora增加如下內容
LOCAL_LISTENER_RAC =
  (ADDRESS_LIST =
    (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.211)(PORT = 1521))
  )
4)登陸資料庫修改local_listener引數
SQL> alter system set local_listener='LOCAL_LISTENER_RAC' scope=both;
System altered.
SQL> col name for a10;
SQL> col type for a10;
SQL> col value for a10;
SQL> show parameter listener;

NAME                                 TYPE       VALUE
------------------------------------ ---------- ------------------------------
local_listener                       string     LOCAL_LISTENER_RAC
remote_listener                      string     LISTENERS_ORCL

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29634949/viewspace-1255740/,如需轉載,請註明出處,否則將追究法律責任。

相關文章