怎样实现Mycat读写分离与负载均衡

怎样实现Mycat读写分离与负载均衡,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

我们来学习读写分离与负载均衡
先看一下balance 属性


  1. 负载均衡类型,目前的取值有3种:
    1. balance="0", 
    不开启读写分离机制,所有读操作都发送到当前可用的writeHost上。


    2. balance="1",全部的readHoststand by writeHost参与select语句的负载均衡,简单的说,当双主双从模式(M1->S1M2->S2,并且M1 M2互为主备),正常情况下,M2,S1,S2都参与select语句的负载均衡。


    3. balance="2",所有读操作都随机的在writeHostreadhost上分发。


    4. balance="3",所有读请求随机的分发到wiriterHost对应的readhost执行,writerHost不负担读压力,注意balance=3只在1.4及其以后版本有,1.3没有。



writeType属性

  1. 负载均衡类型,目前的取值有3种:

  2. 1. writeType="0", 所有写操作发送到配置的第一个writeHost,第一个挂了切到还生存的第二个writeHost,重新启动后已切换后的为准,切换记录在配置文件中:dnindex.properties .

  3. 2. writeType="1",所有写操作都随机的发送到配置的writeHost。 

  4. 3. writeType="2",没实现。

怎样实现Mycat读写分离与负载均衡
怎样实现Mycat读写分离与负载均衡

所以我们根据第二种方式做负载均衡

  1. <dataNode name="dn1" dataHost="localhost1" database="examdb" />

  2.         <dataNode name="dn2" dataHost="localhost2" database="examdb" />

  3.         <dataNode name="dn3" dataHost="localhost3" database="examdb" />

  4.         <dataHost name="localhost1" maxCon="1000" minCon="10" balance="2"

  5.                           writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">

  6.                 <heartbeat>select user()</heartbeat>

  7.                 <!– can have multi write hosts –>

  8.                 <writeHost host="hostM1" url="192.168.6.121:3306" user="root"

  9.                                    password="ESBecs00">

  10.                         <!– can have multi read hosts –>

  11.                 </writeHost>

  12.                 

  13.                 <writeHost host="hostS1" url="192.168.6.121:3307" user="root"

  14.                                    password="ESBecs00">

  15.                         <!– can have multi read hosts –>

  16.                 </writeHost>

  17.         </dataHost>

  18.         

  19.         <dataHost name="localhost2" maxCon="1000" minCon="10" balance="2"

  20.                           writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">

  21.                 <heartbeat>select user()</heartbeat>

  22.                 <!– can have multi write hosts –>

  23.                 <writeHost host="hostM1" url="192.168.6.120:3306" user="root"

  24.                                    password="ESBecs00">

  25.                         <!– can have multi read hosts –>

  26.                 </writeHost>

  27.                 

  28.                 <writeHost host="hostS1" url="192.168.6.120:3307" user="root"

  29.                                    password="ESBecs00">

  30.                         <!– can have multi read hosts –>

  31.                 </writeHost>

  32.         </dataHost>

  33.         <dataHost name="localhost3" maxCon="1000" minCon="10" balance="2"

  34.                           writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">

  35.                 <heartbeat>select user()</heartbeat>

  36.                 <!– can have multi write hosts –>

  37.                 <writeHost host="hostM1" url="192.168.6.119:3306" user="root"

  38.                                    password="ESBecs00">

  39.                         <!– can have multi read hosts –>

  40.                 </writeHost>

  41.                 

  42.                 <writeHost host="hostM1" url="192.168.6.119:3307" user="root"

  43.                                    password="ESBecs00">

  44.                         <!– can have multi read hosts –>

  45.                 </writeHost>

  46.         </dataHost>

测试

  1.   #!/usr/bin/bash
    for((i=1;i<=1000;i++))
    do
    mysql -uroot -p123456 -P8066 -h227.0.0.1  -e "select name from company" TESTDB;
    done
       

  2. root@localhost logs]# sort test-blance.log |more |uniq -c 

  3.      30 this is 119
         17 this is 119:3307
         28 this is 120
         23 this is 120:3307
         36 this is 121
         26 this is 121:3307

如上:6台机器全部都有读到,读负载均衡成功

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/204339.html

(0)
上一篇 2021年11月29日
下一篇 2021年11月29日

相关推荐

发表回复

登录后才能评论