正文  软件开发 > html5 >

mycat分库规则sharding-by-murmur

mycat分库规则sharding-by-murmurmurmur算法是将字段进行hash后分发到不同的数据库,字段类型支持int和varchar.1.rule.xml<tableRule nam...

mycat分库规则sharding-by-murmur

murmur算法是将字段进行hash后分发到不同的数据库,字段类型支持int和varchar.
1.rule.xml

  1. <tableRule name="sharding-by-murmur-userid">
  2. <rule>
  3. <columns>userid</columns>
  4. <algorithm>murmur</algorithm>
  5. </rule>
  6. </tableRule>

  7. <function name="murmur"
  8. class="io.mycat.route.function.PartitionByMurmurHash">
  9. <property name="seed">0</property><!-- 默认是0 -->
  10. <property name="count">6</property><!-- 要分片的数据库节点数量,必须指定,否则没法分片 -->
  11. <property name="virtualBucketTimes">160</property><!-- 一个实际的数据库节点被映射为这么多虚拟节点,默认是160倍,也就
  12. 是虚拟节点数是物理节点数的160倍 -->
  13. <!-- <property name="weightMapFile">weightMapFile</property> 节点的权重,没有指定权重的节点默认是1。以properties文件
  14. 的格式填写,以从0开始到count-1的整数值也就是节点索引为key,以节点权重值为值。所有权重值必须是正整数,否则以1代替 -->
  15. <!-- <property name="bucketMapPath">/etc/mycat/bucketMapPath</property>
  16. 用于测试时观察各物理节点与虚拟节点的分布情况,如果指定了这个属性,会把虚拟节点的murmur hash值与物理节点的映
  17. 射按行输出到这个文件,没有默认值,如果不指定,就不会输出任何东西 -->
  18. </function>
name="count" 根据分库的个数设置,我这里6个分库


2.schema.xml配置文件

  1. <?xml version="1.0"?>
  2. <!DOCTYPE mycat:schema SYSTEM "schema.dtd">
  3. <mycat:schema xmlns:mycat="http://io.mycat/">

  4. <schema name="logdb" checkSQLschema="false" sqlMaxLimit="100">
  5. <table name="tb_log_t" primaryKey="ID" autoIncrement="true" dataNode="dn1,dn2,dn3,dn4,dn5,dn6" rule="mod-long" />
  6. <table name="tb_user_t" dataNode="dn1,dn2,dn3,dn4,dn5,dn6" rule="sharding-by-intfile-provcode" />
  7. <table name="tb_user_detail_t" dataNode="dn1,dn2,dn3,dn4,dn5,dn6" rule="auto-sharding-long-userid" />
  8. <table name="tb_user_murmur_t" dataNode="dn1,dn2,dn3,dn4,dn5,dn6" rule="sharding-by-murmur" />
  9. <table name="tb_user_murmur_string_t" dataNode="dn1,dn2,dn3,dn4,dn5,dn6" rule="sharding-by-murmur-userid" />
  10. </schema>
  11. <schema name="coss03" checkSQLschema="false" sqlMaxLimit="100" dataNode="dnoss03"/>
  12. <dataNode name="dn1" dataHost="localhost1" database="log01" />
  13. <dataNode name="dn2" dataHost="localhost1" database="log02" />
  14. <dataNode name="dn3" dataHost="localhost1" database="log03" />
  15. <dataNode name="dn4" dataHost="localhost1" database="log04" />
  16. <dataNode name="dn5" dataHost="localhost1" database="log05" />
  17. <dataNode name="dn6" dataHost="localhost1" database="log06" />
  18. <dataNode name="dnoss03" dataHost="localhost1" database="oss03" />
  19. <dataHost name="localhost1" maxCon="1000" minCon="10" balance="0"
  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.56.141:3306" user="root"
  24. password="mysql">
  25. <!-- can have multi read hosts -->
  26. <readHost host="hostS2" url="192.168.56.142:3306" user="root" password="mysql" />
  27. </writeHost>
  28. </dataHost>
  29. </mycat:schema>


3.创建表


CREATE TABLE `tb_user_murmur_string_t` (
`userid` varchar(32) NOT NULL,
`name` varchar(64) DEFAULT NULL,
`createtime` datetime DEFAULT CURRENT_TIMESTAMP,
`moditytime` datetime DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`userid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


写入数据
insert into tb_user_murmur_string_t(userid,name) values(‘user002‘,‘name002‘);
insert into tb_user_murmur_string_t(userid,name) values(‘user003‘,‘name003‘);
insert into tb_user_murmur_string_t(userid,name) values(‘user004‘,‘name004‘);
insert into tb_user_murmur_string_t(userid,name) values(‘user005‘,‘name005‘);
insert into tb_user_murmur_string_t(userid,name) values(‘user006‘,‘name006‘);
insert into tb_user_murmur_string_t(userid,name) values(‘user007‘,‘name007‘);
insert into tb_user_murmur_string_t(userid,name) values(‘user008‘,‘name008‘);
insert into tb_user_murmur_string_t(userid,name) values(‘user009‘,‘name009‘);
insert into tb_user_murmur_string_t(userid,name) values(‘user010‘,‘name010‘);

-- The End --

http://www.bkjia.com/HTML5/1222875.htmlwww.bkjia.comtruehttp://www.bkjia.com/HTML5/1222875.htmlTechArticlemycat分库规则sharding-by-murmur murmur算法是将字段进行hash后分发到不同的数据库,字段类型支持int和varchar. 1.rule.xml tableRule name="sharding-by-murmur-...