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");
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进行查询的情况,而主从复制的时候有延迟,这时怎么解决呢?有两个办法: