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 { 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 這種做法小編覺得還不是最好的要實現負載平衡我們可以實現伺服器叢集來實現。