配置 RAC 負載均衡與故障轉移

dawn009發表於2014-02-20
--轉載於:http://blog.csdn.net/leshami/article/details/6791667
Oracle負載均衡主要是指新會話連線到RAC資料庫時,如何判定這個新的連線要連到哪個節點進行工作?通常情況下,負載均衡分為客戶端負載

均衡與伺服器端負載均衡。客戶端負載均衡通常是在客戶端的tnsnames.ora中多新增一個連結地址以及LOAD_BALANCE與failover引數。而伺服器
端的負載均衡則相對複雜,下面具體描述伺服器端負載均衡。
    
一、負載均衡
    注意這裡的負載均衡指的是連線的負載均衡,即客戶可以隨機從不同的例項中連線到資料庫
    1.配置tnsnames.ora使得該檔案中包含如下全部內容:       
  1. # LISTENERS_DEVDB DEVDB是資料庫名,可以使用netmgr,netca編輯或直接使用Vim建立  
  2. LISTENERS_DEVDB =                                                             
  3.   (ADDRESS_LIST =                                                             
  4.     (ADDRESS = (PROTOCOL = TCP)(HOST = rac1-vip.robinson.com)(PORT = 1521))     
  5.     (ADDRESS = (PROTOCOL = TCP)(HOST = rac2-vip.robinson.com)(PORT = 1521))     
  6.   )                                                                           
    2.配置引數檔案remote_listener          
  1. SQL> alter system set remote_listener='LISTENERS_DEVDB' scope=both sid='*';   
    3.需要配置連線描述資訊的兩個IP地址、埠號、以及load_balance子項為yes (主要是load_balance子項)      
  1. DEVDB =                                                                         
  2.   (DESCRIPTION =                                                                
  3.     (ADDRESS = (PROTOCOL = TCP)(HOST = rac1-vip.robinson.com)(PORT = 1521))       
  4.     (ADDRESS = (PROTOCOL = TCP)(HOST = rac2-vip.robinson.com)(PORT = 1521))       
  5.     (LOAD_BALANCE = yes)                                                          
  6.     (CONNECT_DATA =                                                               
  7.       (SERVER = DEDICATED)                                                        
  8.       (SERVICE_NAME = devdb.robinson.com)                                         
  9.     )                                                                             
  10.   )                                                                             
    4.檢視偵聽器的狀態,從下面可以看到devdb.robinson.com服務中有兩個例項為其提供服務     
  1. [oracle@rac2 ~]$ lsnrctl status                                                  
  2.     Service "devdb.robinson.com" has 2 instance(s).                                
  3.       Instance "devdb1", status READY, has 1 handler(s) for this service...        
  4.       Instance "devdb2", status READY, has 2 handler(s) for this service...        
    5.測試負載均衡
    使用shell指令碼來進行測試負載均衡      
  1. --編輯TestLoadBalance.sh                                                                            
  2.     #!/bin/bash                                                                                       
  3.     #Usage: TestLoadBalance devdb 1000                                                                
  4.     count=0                                                                                           
  5.     while [ $count -lt $2 ]   # Set up a loop control                                                 
  6.     do                        # Begin the loop                                                        
  7.         count='expr $count + 1' # Increment the counter                                                 
  8.         sqlplus -s usr1/usr1pwd@$1 @TestLoadBalance.sql  # Connect instance and execute sql statement   
  9.         sleep 1                                                                                         
  10.     done                                                                                              
  11.                                                                                                     
  12. --TestLoadBalance.sql 指令碼                                                                          
  13.     col instance_name format a30                                                                      
  14.     select instance_name from v$instance;                                                             
  15.                                                                                                     
  16. --實施測試                                                                                          
  17.     ./TestLoadBalance.sh devdb 1000                                                                   
  18.                                                                                                       
  19. --檢視結果                                                                                          
  20.     SQL> select inst_id,count(1) from gv$instance group by inst_d;                                                    
  21.                                                                                                       
  22.     INST_ID       COUNT(1)                                                                            
  23.     ----------    ----------                                                                          
  24.     devdb1        446                                                                                 
  25.     devdb2        554                                                                                 
二、配置故障轉移
    負載均衡是用於實現基於連線的負載均衡,但不能解決節點是否可用,一旦一個節點損壞,已成功連線的客戶端並不能轉移到其他正常服務的
    例項中。而故障轉移功能則使得該功能得以實現。可以使用srvctl 和dbca來建立服務。下面使用dbca來建立一個新的服務,客戶端連線到實
    例後,對故障實現透明切換。
    
    1.配置故障轉移服務
        在節點rac1使用oracle帳戶啟動dbca工具,
            a.選擇 Oracle Real Application Clusters database
            b.選擇 Services Management
            c.叢集資料庫列表:單擊 Next。 
            d.資料庫服務:單擊 Add。 
                新增服務:輸入sales。 
                選擇 devdb1 作為首選例項。 
                選擇 devdb2 作為可用例項。 
                TAF 策略:選擇 Basic。 
                單擊 Finish。
            e.資料庫配置助手:單擊 No 退出。
        配置完畢後,在tnsnames.ora中多出了以下內容,注意是各個節點都會多出以下內容            
  1. SALES =                                                                      
  2.   (DESCRIPTION =                                                             
  3.     (ADDRESS = (PROTOCOL = TCP)(HOST = rac1-vip.robinson.com)(PORT = 1521))    
  4.     (ADDRESS = (PROTOCOL = TCP)(HOST = rac2-vip.robinson.com)(PORT = 1521))    
  5.     (LOAD_BALANCE = yes)                                                       
  6.     (CONNECT_DATA =                                                            
  7.       (SERVER = DEDICATED)                                                     
  8.       (SERVICE_NAME = sales.robinson.com)                                      
  9.       (FAILOVER =      --failover_mode是實現故障轉移的關鍵選項                 
  10.         (TYPE = SELECT)                                                          
  11.         (METHOD = BASIC)   --TAF 策略:此處當前為 Basic                          
  12.         (RETRIES = 180)                                                          
  13.         (DELAY = 5)                                                              
  14.       )                                                                        
  15.     )                                                                          
  16.   )                                                                          
        同時引數service_names會多出一個服務名,位於在配置資料庫服務時選擇的首選例項中      
  1. SQL> select instance_name from v$instance;                                          
  2.                                                                                     
  3. INSTANCE_NAME                                                                       
  4. ----------------                                                                    
  5. devdb1                                                                              
  6.                                                                                     
  7. SQL> show parameter service_names                                                   
  8.                                                                                     
  9. NAME                                 TYPE        VALUE                              
  10. ------------------------------------ ----------- ------------------------------     
  11. service_names                        string      devdb.robinson.com, sales          
  12.                                                                                     
  13. SQL> select instance_name from v$instance;                                          
  14.                                                                                     
  15. INSTANCE_NAME                                                                       
  16. ----------------                                                                    
  17. devdb2                                                                              
  18.                                                                                     
  19. SQL> show parameter service                                                         
  20.                                                                                     
  21. NAME                                 TYPE        VALUE                              
  22. ------------------------------------ ----------- ------------------------------     
  23. service_names                        string      devdb.robinson.com                 
        使用srvctl工具也可以看到該服務已經正常開始提供服務        
  1. SQL> ho srvctl status service -d devdb -s sales                              
  2. Service sales is running on instance(s) devdb1                               
  3.                                                                              
  4. SQL> ho lsnrctl status                                                       
  5.                                                                              
  6. Service "sales.robinson.com" has 1 instance(s).   --sales正常提供服務        
  7.   Instance "devdb1", status READY, has 2 handler(s) for this service...      
    2.實現故障轉移
        下面使用帳戶usr1,服務名sales從Windows客戶端來登陸,注意要配置好客戶端tnsnames,可以將伺服器sales項內容全部複製到客戶端tnsnames.ora中     
  1. C:\>sqlplus usr1/usr1@sales                                                                         
  2. SQL> col host_name format a20                                                                       
  3. SQL> select instance_number ins_no,instance_name,host_name,status from v$instance;                  
  4.                                                                                                     
  5.     INS_NO INSTANCE_NAME    HOST_NAME            STATUS                                               
  6. ---------- ---------------- -------------------- ------------                                       
  7.          1 devdb1           rac1.robinson.com    OPEN                                                   
  8. SQL> select failover_type,failover_method,failed_over from v$session                                
  9.   2  where username='USR1';                                                                         
  10.                                                                                                     
  11. FAILOVER_TYPE FAILOVER_M FAI                                                                        
  12. ------------- ---------- ---                                                                        
  13. SELECT        BASIC      NO                                                                         
  14.                                                                                                     
  15. --從其它會話使用sys帳戶登陸到crm 並關閉該例項                                                         
  16. SQL> show user;                                                                                     
  17. USER is "SYS"                                                                                       
  18. SQL> select instance_name from v$instance;                                                          
  19.                                                                                                     
  20. INSTANCE_NAME                                                                                       
  21. ----------------                                                                                    
  22. devdb1                                                                                              
  23.                                                                                                     
  24. SQL> shutdown abort                                                                                 
  25. ORACLE instance shut down.                                                                          
  26.                                                                                                     
  27. --從先前登陸到sales的會話中驗證會話故障切換功能                                                     
  28. SQL> select instance_number ins_no,instance_name,host_name,status from v$instance;                  
  29.                                                                                                     
  30.     INS_NO INSTANCE_NAME    HOST_NAME            STATUS                                               
  31. ---------- ---------------- -------------------- ------------                                       
  32.                  2 devdb2           rac2.robinson.com    OPEN                                               
  33.                                                                                                     
  34. SQL> select failover_type,failover_method,failed_over from v$session                                
  35.   2  where username='USR1';  --第3列顯示的為yes,也表明經過故障切換後提供的服務                     
  36.                                                                                                     
  37. FAILOVER_TYPE FAILOVER_M FAI                                                                        
  38. ------------- ---------- ---                                                                        
  39. SELECT        BASIC      YES                                                                            
  40.                                                                                                     
  41. --由下面的查詢中可以看到服務名sales被新增到可用例項devdb2,節點rac2 的service_names引數中           
  42. SQL> select instance_name from v$instance;                                                          
  43.                                                                                                     
  44. INSTANCE_NAME                                                                                       
  45. ----------------                                                                                    
  46. devdb2                                                                                              
  47.                                                                                                     
  48. SQL> show parameter service                                                                         
  49.                                                                                                     
  50. NAME                                 TYPE        VALUE                                              
  51. ------------------------------------ ----------- ------------------------------                     
  52. service_names                        string      devdb.robinson.com, sales                            
    3.重新定位故障轉移服務到首選例項
        對於首選例項從故障中恢復後,需要手動來重新定位到首選例項        
  1. SQL> startup  --啟動devdb1                                                           
  2. SQL> show parameter service_names                                                    
  3.                                                                                      
  4. NAME                                 TYPE        VALUE                               
  5. ------------------------------------ ----------- ------------------------------      
  6. service_names                        string      devdb.robinson.com                  
  7.                                                                                      
  8. SQL> ho srvctl relocate service -d devdb -s sales -i devdb2 -t devdb1                
  9.                                                                                      
  10. SQL> show parameter service_names                                                    
  11.                                                                                      
  12. NAME                                 TYPE        VALUE                               
  13. ------------------------------------ ----------- ------------------------------      
  14. service_names                        string      devdb.robinson.com, sales           
    4.DML故障轉移(不同於DQL,因此單獨列出)
        使用Windows客戶端透過sales服務名登陸        
  1. C:\>sqlplus usr1/usr1@sales                                                            
  2.                                                                                        
  3. SQL> show user;                                                                        
  4. USER is "USR1"                                                                         
  5. SQL> create table tb_temp (id int,ename varchar2(20)) tablespace tbs1;                 
  6.                                                                                          
  7. Table created.                                                                         
  8.                                                                                        
  9. SQL> insert into tb_temp                                                               
  10.   2  select 1,'Robinson' from dual                                                     
  11.   3  union all                                                                         
  12.   4  select 2,'Jackson'  from dual;                                                    
  13.                                                                                        
  14. rows created.                                                                        
  15.                                                                                        
  16. SQL> commit;                                                                           
  17.                                                                                        
  18. Commit complete.                                                                       
  19.                                                                                        
  20. SQL> select * from tb_temp;                                                            
  21.                                                                                        
  22.         ID ENAME                                                                           
  23. ---------- --------------------                                                        
  24.          1 Robinson                                                                        
  25.          2 Jackson                                                                         
  26.                                                                                        
  27. SQL> delete from tb_temp;                                                              
  28.                                                                                        
  29. rows deleted.                                                                        
  30.                                                                                        
  31. SQL> select * from tb_temp;                                                            
  32.                                                                                        
  33. no rows selected                                                                       
        從另一個會話中使用sysdba關閉devdb1(shutdown abort)
        再在剛剛執行表建立的會話中查詢記錄,收到提示,事務必須被回滾     
  1. SQL> select * from tb_temp;                                                  
  2. select * from tb_temp                                                        
  3. *                                                                            
  4. ERROR at line 1:                                                             
  5. ORA-25402: transaction must roll back                                        
  6.                                                                              
  7. SQL> rollback;                                                               
  8.                                                                              
  9. Rollback complete.                                                           
  10. SQL> select * from tb_temp;                                                  
  11.                                                                              
  12.         ID ENAME                                                                 
  13. ---------- --------------------                                              
  14.          1 Robinson                                                              
  15.          2 Jackson                                                               
  16.                                                                              
  17. SQL> select failover_type,failover_method,failed_over from v$session         
  18.   2  where username='USR1';                                                  
  19.                                                                              
  20. FAILOVER_TYPE FAILOVER_M FAI                                                 
  21. ------------- ---------- ---                                                 
  22. SELECT        BASIC      YES                                                 
        總結:對於DML 操作在實現故障轉移時,將嚴格按照ACID原則來執行,大部分情況需要回滾事務。

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

相關文章