Mycat 读写分离详解

Mycat 的读写分离是依赖数据库级别的数据主从同步的基础上来实现的(Mysql 的主从配置链接),Mycat 的读写分离是在 schema.xml 配置的 dataHost 节点的 balance 属性来完成,该属性的取值有4种,详细说明如下:

  • balance="0", 不开启读写分离机制,所有读操作都发送到当前可用的 writeHost上,示例如下:

    该配置虽然配置了 readHost 节点,但是由于 balance = 0 则表示所有的操作语句都发送给当前可用的 writeHost 配置的数据库

    我们执行查询语句如下:

    mysql> select * from tb_sharding_murmur where id= 22;

    通过查询日志如下,发现 select read source hostM1 for dataHost:vmDB1,表示操作的数据库 host 名称为 hostM1

    2018-02-11 21:49:14.957 DEBUG [$_NIOREACTOR-0-RW] (io.mycat.server.NonBlockingSession.execute(NonBlockingSession.java:110)) - ServerConnection [id=1, schema=TEMPDB, host=192.168.2.109, user=mycat,txIsolation=3, autocommit=true, schema=TEMPDB]select * from tb_sharding_murmur where id=22, route={

    1 -> vmDB1-tempdb{select * from tb_sharding_murmur where id=22}

    } rrs

    2018-02-11 21:49:14.958 DEBUG [$_NIOREACTOR-0-RW] (io.mycat.backend.datasource.PhysicalDBPool.getRWBanlanceCon(PhysicalDBPool.java:456)) - select read source hostM1 for dataHost:vmDB1

  • balance="1",全部的readHost与 stand by writeHost 参与 select语句的负载均衡,简单的说,当双主双从模式(M1->S1,M2->S2,并且 M1与 M2互为主备),正常情况下,M2,S1,S2都参与 select语句的负载均衡,配置示例如下:

    我们多次执行查询语句如下:

    mysql> select * from tb_sharding_murmur where id= 32;

    通过查询日志如下,发现 select read source hostR1 for dataHost:vmDB1,表示操作的数据库 host 名称为 hostR1,由于我们只有一个M1 主,所以所有的查询操作由读取主机来负责处理

    2018-02-12 02:44:35.480 DEBUG [$_NIOREACTOR-0-RW] (io.mycat.backend.datasource.PhysicalDBPool.getRWBanlanceCon(PhysicalDBPool.java:456)) - select read source hostR1 for dataHost:vmDB2

    2018-02-12 02:44:35.509 DEBUG [$_NIOREACTOR-0-RW] (io.mycat.server.NonBlockingSession.execute(NonBlockingSession.java:110)) - ServerConnection [id=1, schema=TEMPDB, host=192.168.2.109, user=mycat,txIsolation=3, autocommit=true, schema=TEMPDB]SELECT * FROM tb_sharding_murmur where id=32, route={

    1 -> vmDB2-tempdb{SELECT * FROM tb_sharding_murmur where id=32}

    } rrs

       

  • balance="2",所有读操作都随机的在writeHost、readhost上分发,配置示例如下:

    我们多次执行查询语句如下:

    mysql> select * from tb_sharding_murmur where id= 76;

    通过查询日志可以发现,发现 select read source hostM1 for dataHost:vmDB1,表示从主库处理的操作

    2018-02-12 02:49:59.279 DEBUG [$_NIOREACTOR-0-RW] (io.mycat.backend.datasource.PhysicalDBPool.getRWBanlanceCon(PhysicalDBPool.java:456)) - select read source hostM1 for dataHost:vmDB1

    2018-02-12 02:49:59.411 DEBUG [$_NIOREACTOR-0-RW] (io.mycat.server.NonBlockingSession.execute(NonBlockingSession.java:110)) - ServerConnection [id=1, schema=TEMPDB, host=192.168.2.109, user=mycat,txIsolation=3, autocommit=true, schema=TEMPDB]SELECT * FROM tb_sharding_murmur where id=76, route={

    1 -> vmDB2-tempdb{SELECT * FROM tb_sharding_murmur where id=76}

    也有日志如下,发现 select read source hostR1 for dataHost:vmDB1,表示从主库处理的操作

    2018-02-12 02:49:59.411 DEBUG [$_NIOREACTOR-0-RW] (io.mycat.backend.datasource.PhysicalDBPool.getRWBanlanceCon(PhysicalDBPool.java:456)) - select read source hostR1 for dataHost:vmDB2

    2018-02-12 02:50:01.459 DEBUG [$_NIOREACTOR-0-RW] (io.mycat.server.NonBlockingSession.execute(NonBlockingSession.java:110)) - ServerConnection [id=1, schema=TEMPDB, host=192.168.2.109, user=mycat,txIsolation=3, autocommit=true, schema=TEMPDB]SELECT * FROM tb_sharding_murmur where id=76, route={

    1 -> vmDB2-tempdb{SELECT * FROM tb_sharding_murmur where id=76}

    } rrs

       

  • balance="3",所有读请求随机的分发到wiriterHost对应的readhost执行,writerHost 不负担读压力,配置示例如下:

    我们执行查询语句如下:

    mysql> select * from tb_sharding_murmur where id= 17;

    通过查询日志如下,发现 select read source hostR1 for dataHost:vmDB1 ,表示操作的数据库 host 名称为 hostR1

    2018-02-12 02:41:25.934 DEBUG [$_NIOREACTOR-0-RW] (io.mycat.backend.datasource.PhysicalDBPool.getRWBanlanceCon(PhysicalDBPool.java:456)) - select read source hostR1 for dataHost:vmDB1

    2018-02-12 02:41:26.072 DEBUG [$_NIOREACTOR-0-RW] (io.mycat.server.NonBlockingSession.execute(NonBlockingSession.java:110)) - ServerConnection [id=1, schema=TEMPDB, host=192.168.2.109, user=mycat,txIsolation=3, autocommit=true, schema=TEMPDB]SELECT * FROM tb_sharding_murmur where id=17, route={

    1 -> vmDB1-tempdb{SELECT * FROM tb_sharding_murmur where id=17}

       

posted @ 2018-02-23 14:34  立3807  阅读(623)  评论(0编辑  收藏  举报