四时宝库

程序员的知识宝库

springboot整合mycat- -后续(二)(springboot整合mybatis步骤)

3、完整代码如下:

/**

* 自定义多字段算法计算

*/

public class HeiMaBurstRuleAlgorithm extends AbstractPartitionAlgorithm implements RuleAlgorithm {

// 单组数据容量

Long volume;

// 单组DN节点数量

Integer step;

// 分片模

Integer mod;

public void init(){}

/**

*

* @param columnValue 数据ID-桶ID

* @return

*/

public Integer calculate(String columnValue){

if(columnValue!=null){

String[] temp = columnValue.split("-");

if(temp.length==2){

try {

Long dataId = Long.valueOf(temp[0]);

Long burstId = Long.valueOf(temp[1]);

int group = (int)(dataId/volume)*step;

int pos = group + (int)(burstId%mod);

System.out.println("HEIMA RULE INFO ["+columnValue+"]-[{"+pos+"}]");

return pos;

}catch (Exception e){

System.out.println("HEIMA RULE INFO ["+columnValue+"]-[{"+e.getMessage()+"}]");

}

}

}

return 0;

}

/**

* 范围计算

* @param beginValue

* @param endValue

* @return

*/

public Integer[] calculateRange(String beginValue, String endValue){

if(beginValue!=null&&endValue!=null){

Integer begin = calculate(beginValue);

Integer end = calculate(endValue);

if(begin == null || end == null){

return new Integer[0];

}

if (end >= begin) {

int len = end - begin + 1;

Integer[] re = new Integer[len];

for (int i = 0; i < len; i++) {

re[i] = begin + i;

}

return re;

}

}

return new Integer[0];

}

public void setVolume(Long volume) {

this.volume = volume;

}

public void setStep(Integer step) {

this.step = step;

}

public void setMod(Integer mod) {

this.mod = mod;

}

}

算法的应用

  • service-mycat/config/rule.xml

Burst5050单组有50个DN,单组容量为5亿,分50张表,每个DN上存储一张对应表;其定义在service-mycat/config/rule.xml中,定义代码为:

<tableRule name="burst5050">

<rule>

<columns>burst</columns>

<algorithm>burst5050</algorithm>

</rule>

</tableRule>


<functionname="burst5050" class="com.heima.HeiMaBurstRuleAlgorithm">

<property name="volume">500000000</property><!-- 单组容量 -->

<property name="step">50</property><!-- 单组节点量 -->

<property name="mod">50</property><!-- 单组数据mod -->

</function>

  • service-mycat/config/schema.xml
  • 在service-mycat/config/schema.xml中,可使用以上定义的规则,示例如下:

<tablename="ap_behavior_entry" dataNode="DNBE_$0-49" rule="burst5050"/>

service

mycat/config/sequence_db_conf.properties

在service-

mycat/config/sequence_db_conf.properties中去掉对应表的sequence配置:

AP_BEHAVIOR_ENTRY=...

AP_COLLECTION=...

算法部署

进入到项目service-mycat项目根路径,运行mvn clean package打包命令打包项目

拷贝service-mycat/config下的文件到mycat安装目录下的config文件夹下

拷贝service-mycat/target/ service-mycat-1.0-SNAPSHOT.jar文件到mycat安装目录lib文件夹下

重启mycat服务

四、mycat的sql语句

五、springboot整合mycat实现读写分离

六、springboot实现分库分表

发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言
    友情链接