国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > MySQL > 正文

淺析MySQL讀寫分離與負載均衡實例教程

2024-07-24 12:39:46
字體:
來源:轉載
供稿:網友

MySQL 數據庫的讀寫分離和負載均衡一般是通過第三方軟件來實現的,也可以通過mysql驅動程序來實現,如com.mysql.jdbc.ReplicationDriver.

詳細文檔參見:http://dev.mysql.com/doc/refman/5.5/en/connector-j-info.html,代碼如下:

  1. import java.sql.Connection
  2. import java.sql.ResultSet; 
  3. import java.util.Properties; 
  4.  
  5. import com.mysql.jdbc.ReplicationDriver; 
  6.  
  7. public class ReplicationDriverDemo { 
  8.  
  9.   public static void main(String[] args) throws Exception { 
  10.     ReplicationDriver driver = new ReplicationDriver(); 
  11.  
  12.     Properties props = new Properties(); 
  13.  
  14.     // We want this for failover on the slaves 
  15.     props.put("autoReconnect""true"); 
  16.  
  17.     // We want to load balance between the slaves 
  18.     props.put("roundRobinLoadBalance""true"); 
  19.  
  20.     props.put("user""foo"); 
  21.     props.put("password""bar"); 
  22.  
  23.     // 
  24.     // Looks like a normal MySQL JDBC url, with a 
  25.     // comma-separated list of hosts, the first 
  26.     // being the 'master', the rest being any number 
  27.     // of slaves that the driver will load balance against 
  28.     // 
  29.  
  30.     Connection conn = 
  31.         driver.connect("jdbc:mysql:replication://master,slave1,slave2,slave3/test"
  32.             props); 
  33.  
  34.     // 
  35.     // Perform read/write work on the master 
  36.     // by setting the read-only flag to "false" 
  37.     // 
  38.  
  39.     conn.setReadOnly(false); 
  40.     conn.setAutoCommit(false); 
  41.     conn.createStatement().executeUpdate("UPDATE some_table ...."); 
  42.     conn.commit(); 
  43.  
  44.     // 
  45.     // Now, do a query from a slave, the driver automatically picks one 
  46.     // from the list  --Vevb.com 
  47.     // 
  48.  
  49.     conn.setReadOnly(true); 
  50.  
  51.     ResultSet rs = 
  52.       conn.createStatement().executeQuery("SELECT a,b FROM alt_table"); 
  53.  
  54.      ....... 
  55.   } 

讀寫分離:

  1. jdbc:mysql:replication://master:3306,slave1:3306,slave2:3306/dbname 
  2.  
  3. When using the following connection string: jdbc:mysql:replication://dbmaster:3306,dbslave1:3306,dbslave2:3306/dbname 
  4.  
  5. dbmaster is used for all write connections as expected and dbslave1 is used for all read connections, but dbslave2 is never used. I would have expected distributed reads between dbslave1 and dbslave2. 

原理是:ReplicationDriver生成代理的connection對象,當設置這個connection.readOnly=true時,連接slave,當connection.readOnly=false時,連接master

負載均衡:

  1. jdbc:mysql:loadbalance://master:3306,slave1:3306,slave2:3306/dbname 
  2.  
  3. When using the following connection string: jdbc:mysql:loadbalance://dbmaster:3306,dbslave1:3306,dbslave2:3306/dbname 
  4. connections are load-balanced between all three servers for both read and write connections. 

問題:讀寫分離時可能會碰到剛寫完master,再馬上到slave進行查詢的情況,而主從復制的時候有延遲,這時怎么解決呢?有兩個辦法.

1.比如增加頁面保存數據后馬上跳轉到列表頁面,這時可能出不來數據,因為復制還沒完成,這時可以在前臺添加一些成功的提示,成功頁面等進行一些頁面跳轉延遲處理,讓服務器有時間去復制,復制延遲一般在毫秒級,而這種提示處理在秒級,所以時間上一般是足夠的.

2.第1種辦法可能部分場景是可行的,但是有些場景要求比較高,需要實時的,這時可以在讀取的時候進行處理,強制從master中讀取,可以通過注解,加參數/標識等來指定從master讀取數據.

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 苍南县| 江陵县| 九台市| 苍溪县| 拉萨市| 永春县| 晋州市| 神农架林区| 丽水市| 泌阳县| 砚山县| 百色市| 屯昌县| 商城县| 正镶白旗| 佳木斯市| 呼和浩特市| 尼玛县| 松阳县| 舞阳县| 外汇| 菏泽市| 凤庆县| 万盛区| 都兰县| 东辽县| 长泰县| 青阳县| 凤台县| 台江县| 北海市| 红河县| 隆化县| 德令哈市| 义马市| 达州市| 南投县| 沧州市| 通州区| 安远县| 象州县|