cobar文档 - 分库规则配置

1.场景

本文将给出一个典型的应用场景,并做具体配置说明,假设场景如下:
 - 通过Cobar提供一个名为test的数据库,其中包含t1,t2两张表。

 - 后台有3个MySQL实例(ip:port)为其提供服务,分别为:A,B,C。


 - 期望t1表的数据放置在实例A中,t2表的数据水平拆成四份并在实例B和C中各自放两份。
 - t2表的数据要具备HA功能,即B或者C实例其中一个出现故障,不影响使用且可提供完整的数据服务。


2.图示

3.规则描述

  • 3.1 针对test定义了两条访问规则:
    第一条,表示t2表按照rule1规则访问,访问的数据节点范围是dnTest[0-3]。
    第二条,test中除定义的t2表,其他表直接访问dnTest数据节点。
    SchemaTableRuleDataNode
    testt2rule1=func1(id)dnTest[0],dnTest[1],dnTest[2],dnTest[3]
    testNULLNULLdnTest
    • rule1的具体含义是,将t2的id字段值(假设是数字型,如果不是则做一下hash)按1024取模,然后把得到
      的值分配在如下区段:[0-255],[256-511],[512-767],[768-1023]该四个区段分别对应dnTest[0-3]四个节点。
  • 3.2 数据节点(DataNode),由主备数据源(DataSource)、心跳语句以及连接池等信息组成。
    DataNodeDataSource
    dnTestdsTest[0]
    dnTest[0]dsTest[1](master), dsTest[5](slave)
    dnTest[1]dsTest[2](master), dsTest[6](slave)
    dnTest[2]dsTest[3](master), dsTest[7](slave)
    dnTest[3]dsTest[4](master), dsTest[8](slave)
  • 3.3 数据源(DataSource),描述了与数据库实例(DBInstance)及实例中schema之间的映射关系。
    DataSourceDBInstance
    dsTest[0]A/test1
    dsTest[1]B/test2 (master)
    dsTest[2]B/test3 (master)
    dsTest[3]C/test4 (master)
    dsTest[4]C/test5 (master)
    dsTest[5]C/test2 (slave)
    dsTest[6]C/test3 (slave)
    dsTest[7]B/test4 (slave)
    dsTest[8]B/test5 (slave) 


4.配置文件

注:xxx$0-4 等价于 xxx[0],xxx[1],xxx[2],xxx[3],xxx[4] 是配置的一种缩写方式。

schema.xml 配置

<? xml version = "1.0" encoding = "UTF-8" ?>
<!DOCTYPE cobar:schema SYSTEM "schema.dtd">
< cobar:schema xmlns:cobar = "http://cobar.alibaba.com/" >
<!-- schema定义 -->
< schema name = "test" dataNode = "dnTest" >
< table name = "t2" dataNode = "dnTest$0-3" rule = "rule1" />
</ schema >
<!-- 数据节点定义,数据节点由数据源和其他一些参数组织而成。-->
< dataNode name = "dnTest" >
< property name = "dataSource" >
< dataSourceRef >dsTest$0</ dataSourceRef >
</ property >
</ dataNode >
< dataNode name = "dnTest" >
< property name = "dataSource" >
< dataSourceRef >dsTest$1-4</ dataSourceRef >
< dataSourceRef >dsTest$5-8</ dataSourceRef >
</ property >
< heartbeat >select 1</ heartbeat >
</ dataNode >
<!-- 数据源定义,数据源是一个具体的后端数据连接的表示。-->
< dataSource name = "dsTest" type = "mysql" >
< property name = "location" >
< location >192.168.0.1:3306/test1</ location > <!--注意:替换为您的MySQL IP和Port-->
< location >192.168.0.1:3307/test$2-3</ location > <!--注意:替换为您的MySQL IP和Port-->
< location >192.168.0.1:3308/test$4-5</ location > <!--注意:替换为您的MySQL IP和Port-->
< location >192.168.0.1:3308/test$2-3</ location > <!--注意:替换为您的MySQL IP和Port-->
< location >192.168.0.1:3307/test$4-5</ location > <!--注意:替换为您的MySQL IP和Port-->
</ property >
< property name = "user" >test</ property > <!--注意:替换为您的MySQL用户名-->
< property name = "password" ></ property > <!--注意:替换为您的MySQL密码-->
< property name = "sqlMode" >STRICT_TRANS_TABLES</ property >
</ dataSource >
</ cobar:schema >

rule.xml 配置

<? xml version = "1.0" encoding = "UTF-8" ?>
<!DOCTYPE cobar:rule SYSTEM "rule.dtd">
< cobar:rule xmlns:cobar = "http://cobar.alibaba.com/" >
<!-- 路由规则定义,定义什么表,什么字段,采用什么路由算法。-->
< tableRule name = "rule1" >
< rule >
< columns >id</ columns >
< algorithm > <![CDATA[ func1(${id})]]> </ algorithm >
</ rule >
</ tableRule >
<!-- 路由函数定义,在路由规则的算法定义中使用(路由函数可以自定义扩展)。-->
< function name = "func1" class = "com.alibaba.cobar.route.function.PartitionByLong" >
< property name = "partitionCount" >4</ property >
< property name = "partitionLength" >256</ property >
</ function >
</ cobar:rule >

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值