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

首頁 > 數據庫 > MySQL > 正文

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

2024-07-24 12:35:07
字體:
來源:轉載
供稿:網友
  最近在一個群里老看到有一個朋友問游戲數據充值如何實現讀寫分享了,在此小編正好看到了一篇文章下面整理與各位看看,MySQL 數據庫的讀寫分離和負載均衡一般是通過第三方軟件來實現的,也可以通過mysql驅動程序來實現,如com.mysql.jdbc.ReplicationDriver.
 
  詳細文檔參見:http://dev.mysql.com/doc/refman/5.5/en/connector-j-info.html
 
  代碼如下:
 
  import java.sql.Connection;
  import java.sql.ResultSet;
  import java.util.Properties;
     
  import com.mysql.jdbc.ReplicationDriver;
     
  public class ReplicationDriverDemo {
    //開源軟件:phpfensi.com
    public static void main(String[] args) throws Exception {
      ReplicationDriver driver = new ReplicationDriver();
     
      Properties props = new Properties();
 
  // We want this for failover on the slaves
      props.put("autoReconnect", "true");

  // We want to load balance between the slaves
      props.put("roundRobinLoadBalance", "true");
     
      props.put("user", "foo");
      props.put("password", "bar");

  //
       
  // Looks like a normal MySQL JDBC url, with a
       
  // comma-separated list of hosts, the first
       
  // being the 'master', the rest being any number
       
  // of slaves that the driver will load balance against
       
  //
     
      Connection conn =
          driver.connect("jdbc:mysql:replication://master,slave1,slave2,slave3/test",
              props);

  //
       
  // Perform read/write work on the master
       
  // by setting the read-only flag to "false"
       
  //
     
      conn.setReadOnly(false);
      conn.setAutoCommit(false);
      conn.createStatement().executeUpdate("UPDATE some_table ....");
      conn.commit();
 
  //
       
  // Now, do a query from a slave, the driver automatically picks one
       
  // from the list
       
  //
      conn.setReadOnly(true);
     
      ResultSet rs =
        conn.createStatement().executeQuery("SELECT a,b FROM alt_table");
       .......
    }
  }
  讀寫分離,代碼如下:
 
  jdbc:mysql:replication:
  //master:3306,slave1:3306,slave2:3306/dbname
  When using the following connection string: jdbc:mysql:replication://dbmaster:3306,dbslave1:3306,dbslave2:3306/dbname
  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.
 
  負載均衡:
 
  jdbc:mysql:loadbalance:
  //master:3306,slave1:3306,slave2:3306/dbname
  When using the following connection string: jdbc:mysql:loadbalance://dbmaster:3306,dbslave1:3306,dbslave2:3306/dbname
  connections are load-balanced between all three servers for both read and write connections.
  問題:讀寫分離時可能會碰到剛寫完master,再馬上到slave進行查詢的情況,而主從復制的時候有延遲,這時怎么解決呢?有兩個辦法:
 
  1.比如增加頁面保存數據后馬上跳轉到列表頁面,這時可能出不來數據,因為復制還沒完成,這時可以在前臺添加一些成功的提示,成功頁面等進行一些頁面跳轉延遲處理,讓服務器有時間去復制,復制延遲一般在毫秒級,而這種提示處理在秒級,所以時間上一般是足夠的.
 
  2.第1種辦法可能部分場景是可行的,但是有些場景要求比較高,需要實時的,這時可以在讀取的時候進行處理,強制從master中讀取,可以通過注解,加參數/標識等來指定從master讀取數據.
 
  ps 這種做法小編覺得還不是最好的要實現負載均衡我們可以實現服務器集群來實現.
 
 

(編輯:武林網)

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 安泽县| 花莲市| 曲阜市| 新源县| 五家渠市| 彭山县| 连城县| 三穗县| 常熟市| 阳曲县| 阳信县| 南涧| 修水县| 仁化县| 大悟县| 沙洋县| 镇平县| 莱阳市| 个旧市| 交城县| 罗山县| 洱源县| 山丹县| 肥东县| 聂荣县| 林周县| 宜宾市| 英吉沙县| 凌海市| 禹城市| 凤台县| 南和县| 临洮县| 台北县| 绿春县| 庄河市| 崇义县| 上思县| 阜新| 河北省| 宜兰市|