1.先上原理图2.说明两个mysql采用主主同步的方式进行部署。在安装mysql的服务器上安装客户端(目前是这么做,以后想在zookeeper扩展集成),客户端实时监控mysql应用的可用性,可用时想zookeepercreateNode,当网络不可用或者mysql应用不可用时, ...
1.先上原理图
2.说明
两个mysql采用主主同步的方式进行部署。
在安装mysql的服务器上安装客户端(目前是这么做,以后想在zookeeper扩展集成),客户端实时监控mysql应用的可用性,可用时想zookeepercreateNode,当网络不可用或者mysql应用不可用时,建立的znode消失。
在客户端,通过改造proxool数据库连接池的方式,在建立连接之前,从zookeeper中去取真实的数据库URL,如果有多个URL,即有多个服务时,采用随机算法去拿连接(以后准备扩展权重)。当连接不可用时,数据库连接池将重建连接,这时候又回去zookeeper拿连接,因为agent建立的临时znode消失了,就不能拿到已经失效的url了。
这个方案只是初步的实验和实现了,还有很多后续的问题,主要为了解决lvs+keepalived只能在同一个区域内的问题。
3.部分实现
1).agent
/** * 数据库可用性检测 * @author tomsnail * @date 2015年4月3日 上午10:11:51 */public class TestMySQL { public static boolean test(String url){ Connection conn = null; Statement stmt = null; ResultSet rs = null; String sql = ConfigHelp.getLocalConifg("jdbc_inventory.house-keeping-test-sql", "select 0"); try { Class.forName(ConfigHelp.getLocalConifg("jdbc_inventory.driver-class", "com.mysql.jdbc.Driver"));// 动态加载mysql驱动 conn = DriverManager.getConnection(url); stmt = conn.createStatement(); rs = stmt.executeQuery(sql); while (rs.next()) { } return true; } catch (SQLException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { try { if(rs!=null){ rs.close(); } if(stmt!=null){ stmt.close(); } if(conn!=null) conn.close(); } catch (SQLException e) { e.printStackTrace(); } } return false; }}
原标题:基于zookeeper的MySQL主主负载均衡的简单实现
关键词:MYSQL
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。