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

首頁 > 數據庫 > MySQL > 正文

MySQL數據庫實現讀寫分離與負載均衡

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

最近在一個群里老看到有一個朋友問游戲數據充值如何實現讀寫分享了,在此小編正好看到了一篇文章下面整理與各位看看,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.   //開源軟件:Vevb.com 
  9.   public static void main(String[] args) throws Exception { 
  10.     ReplicationDriver driver = new ReplicationDriver(); 
  11.    
  12.     Properties props = new Properties(); 
  13.    
  14.      
  15. // We want this for failover on the slaves 
  16.     props.put("autoReconnect""true"); 
  17.    
  18.      
  19. // We want to load balance between the slaves 
  20.     props.put("roundRobinLoadBalance""true"); 
  21.    
  22.     props.put("user""foo"); 
  23.     props.put("password""bar"); 
  24.    
  25.      
  26. // 
  27.      
  28. // Looks like a normal MySQL JDBC url, with a 
  29.      
  30. // comma-separated list of hosts, the first 
  31.      
  32. // being the 'master', the rest being any number 
  33.      
  34. // of slaves that the driver will load balance against 
  35.      
  36. // 
  37.    
  38.     Connection conn = 
  39.         driver.connect("jdbc:mysql:replication://master,slave1,slave2,slave3/test"
  40.             props); 
  41.    
  42.      
  43. // 
  44.      
  45. // Perform read/write work on the master 
  46.      
  47. // by setting the read-only flag to "false" 
  48.      
  49. // 
  50.    
  51.     conn.setReadOnly(false); 
  52.     conn.setAutoCommit(false); 
  53.     conn.createStatement().executeUpdate("UPDATE some_table ...."); 
  54.     conn.commit(); 
  55.    
  56.      
  57. // 
  58.      
  59. // Now, do a query from a slave, the driver automatically picks one 
  60.      
  61. // from the list 
  62.      
  63. // 
  64.    
  65.     conn.setReadOnly(true); 
  66.    
  67.     ResultSet rs = 
  68.       conn.createStatement().executeQuery("SELECT a,b FROM alt_table"); 
  69.    
  70.      ....... 
  71.   } 

讀寫分離,代碼如下:

  1. jdbc:mysql:replication: 
  2. //master:3306,slave1:3306,slave2:3306/dbname 
  3. When using the following connection string: jdbc:mysql:replication://dbmaster:3306,dbslave1:3306,dbslave2:3306/dbname 
  4. 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: 
  2. //master:3306,slave1:3306,slave2:3306/dbname 
  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讀取數據.

ps 這種做法小編覺得還不是最好的要實現負載均衡我們可以實現服務器集群來實現.

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 虞城县| 二手房| 洛南县| 武定县| 广饶县| 西乡县| 裕民县| 郸城县| 岢岚县| 宁化县| 介休市| 洪江市| 静安区| 莱州市| 祁东县| 北京市| 台中县| 瑞金市| 陆川县| 方山县| 甘肃省| 靖边县| 太原市| 孟连| 昌图县| 泾川县| 沽源县| 杭州市| 宜宾市| 桂平市| 五河县| 东宁县| 沾化县| 额尔古纳市| 临邑县| 股票| 永川市| 康马县| 南召县| 聂荣县| 铅山县|