【Mycat】Mycat讀寫分離與負載均衡2

小亮520cl發表於2017-04-17
上篇文章我們學習了schemal配置檔案的基本資訊,這節我們來學習讀寫分離與負載均衡
先看一下
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",沒實現。





所以我們根據第二種方式做負載均衡
  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 -h127.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臺機器全部都有讀到,讀負載均衡成功

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29096438/viewspace-2137405/,如需轉載,請註明出處,否則將追究法律責任。

相關文章