配置手册

    配置示例

    配置项说明

    1. dataSource: 数据源配置
    2. <data_source_name> 可配置多个: !!数据库连接池实现类
    3. driverClassName: 数据库驱动类名
    4. url: 数据库url连接
    5. username: 数据库用户名
    6. password: 数据库密码
    7. ... 数据库连接池的其它属性
    8. defaultDataSourceName: 默认数据源,未配置分片规则的表将通过默认数据源定位
    9. tables: 分库分表配置,可配置多个logic_table_name
    10. dynamic: 是否为动态表
    11. actualTables: 真实表名,多个表以逗号分隔,支持inline表达式,指定数据源需要加前缀,不加前缀为默认数据源。不填写表示为只分库不分表或动态表(需要配置dynamic=true)。
    12. dataSourceNames: 数据源名称,多个数据源用逗号分隔,支持inline表达式。不填写表示使用全部数据源
    13. databaseStrategy: 分库策略
    14. shardingColumns: 分片列名,多个列以逗号分隔
    15. algorithmClassName: 分库算法全类名,该类需使用默认的构造器或者提供无参数的构造器,与algorithmExpression出现一个即可
    16. algorithmExpression: 分库算法表达式,与algorithmClassName出现一个即可
    17. tableStrategy: 分表策略
    18. shardingColumns: 分片列名,多个列以逗号分隔
    19. algorithmClassName: 分库算法全类名,该类需使用默认的构造器或者提供无参数的构造器,与algorithmExpression出现一个即可
    20. algorithmExpression: 分库算法表达式,与algorithmClassName出现一个即可
    21. bindingTables: 绑定表列表
    22. - tableNames: 逻辑表名列表,多个<logic_table_name>以逗号分隔
    23. defaultDatabaseStrategy: 默认数据库分片策略
    24. shardingColumns: 分片列名,多个列以逗号分隔
    25. algorithmExpression: 分库算法表达式,与algorithmClassName出现一个即可
    26. defaultTableStrategy: 默认数据表分片策略
    27. shardingColumns: 分片列名,多个列以逗号分隔
    28. algorithmClassName: 分表算法全类名,该类需使用默认的构造器或者提供无参数的构造器,与algorithmExpression出现一个即可
    29. algorithmExpression: 分表算法表达式,与algorithmClassName出现一个即可
    30. props: 属性配置(可选)
    31. sql.show: 是否开启SQL显示,默认值: false
    32. executor.size: 工作线程数量,默认值: CPU核数

    YAML格式特别说明

    & 表示变量定义

    • 表示变量引用

    • 表示多个

    配置示例

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xmlns:context="http://www.springframework.org/schema/context"
    5. xmlns:rdb="http://www.dangdang.com/schema/ddframe/rdb"
    6. xsi:schemaLocation="http://www.springframework.org/schema/beans
    7. http://www.springframework.org/schema/beans/spring-beans.xsd
    8. http://www.springframework.org/schema/context
    9. http://www.springframework.org/schema/context/spring-context.xsd
    10. http://www.dangdang.com/schema/ddframe/rdb/rdb.xsd
    11. ">
    12. <context:property-placeholder location="classpath:conf/rdb/conf.properties" ignore-unresolvable="true"/>
    13. <bean id="dbtbl_0" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    14. <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    15. <property name="url" value="jdbc:mysql://localhost:3306/dbtbl_0"/>
    16. <property name="username" value="root"/>
    17. </bean>
    18. <bean id="dbtbl_1" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    19. <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    20. <property name="url" value="jdbc:mysql://localhost:3306/dbtbl_1"/>
    21. <property name="username" value="root"/>
    22. <property name="password" value=""/>
    23. </bean>
    24. <rdb:strategy id="databaseStrategy" sharding-columns="user_id" algorithm-class="com.dangdang.ddframe.rdb.sharding.spring.algorithm.SingleKeyModuloDatabaseShardingAlgorithm"/>
    25. <rdb:strategy id="tableStrategy" sharding-columns="order_id" algorithm-class="com.dangdang.ddframe.rdb.sharding.spring.algorithm.SingleKeyModuloTableShardingAlgorithm"/>
    26. <rdb:data-source id="shardingDataSource">
    27. <rdb:sharding-rule data-sources="dbtbl_0,dbtbl_1" default-data-source="dbtbl_0">
    28. <rdb:table-rules>
    29. <rdb:table-rule logic-table="t_order" actual-tables="t_order_${0..3}" table-strategy="tableStrategy"/>
    30. <rdb:table-rule logic-table="t_order_item" actual-tables="t_order_item_${0..3}" database-strategy="databaseStrategy" table-strategy="tableStrategy"/>
    31. </rdb:table-rules>
    32. <rdb:binding-table-rules>
    33. <rdb:binding-table-rule logic-tables="t_order, t_order_item"/>
    34. </rdb:binding-table-rules>
    35. <rdb:default-database-strategy sharding-columns="none" algorithm-class="com.dangdang.ddframe.rdb.sharding.api.strategy.database.NoneDatabaseShardingAlgorithm"/>
    36. </rdb:sharding-rule>
    37. <rdb:props>
    38. <prop key="sql.show">true</prop>
    39. </rdb:props>
    40. </rdb:data-source>
    41. </beans>

    标签说明

    <rdb:data-source/>

    定义sharding-jdbc数据源

    <rdb:sharding-rule/>

    名称类型数据类型必填说明
    data-sources属性String数据源Bean列表,多个Bean以逗号分隔
    default-data-source属性String默认数据源名称,未配置分片规则的表将通过默认数据源定位
    table-rules标签-分片规则列表

    <rdb:table-rules/>

    名称类型数据类型必填说明
    table-rule+标签-分片规则

    <rdb:table-rule/>

    名称类型数据类型必填说明
    logic-table属性String逻辑表名
    dynamic属性boolean是否动态表
    actual-tables属性String真实表名,多个表以逗号分隔,支持inline表达式,指定数据源需要加前缀,不加前缀为默认数据源 指定数据源需要加前缀,不加前缀为默认数据源。不填写表示为只分库不分表或动态表(需要配置dynamic=true)
    data-source-names属性String数据源名称,多个数据源用逗号分隔,支持inline表达式。不填写表示使用全部数据源
    database-strategy属性String分库策略,对应<rdb:strategy>中分库策略id, 如果不填需配置<rdb:default-database-strategy/>
    table-strategy属性String分表策略,对应<rdb:strategy>中分表策略id, 如果不填需配置<rdb:default-table-strategy/>

    <rdb:binding-table-rules/>

    <rdb:binding-table-rule/>

    名称类型数据类型必填说明
    logic-tables属性String逻辑表名,多个表名以逗号分隔

    <rdb:default-database-strategy/>

    名称类型数据类型必填说明
    sharding-columns属性String分片列名,多个列以逗号分隔
    algorithm-class属性Class默认分库算法全类名,该类需使用默认的构造器或者提供无参数的构造器,与algorithm-expression有且仅有一个出现
    algorithm-expression属性String默认分库算法表达式,与algorithm-class有且仅有一个出现

    <rdb:default-table-strategy/>

    名称类型数据类型必填说明
    sharding-columns属性String分片列名,多个列以逗号分隔
    algorithm-class属性Class默认分表算法全类名,该类需使用默认的构造器或者提供无参数的构造器,与algorithm-expression有且仅有一个出现
    algorithm-expression属性String默认分表算法表达式,与algorithm-class有且仅有一个出现

    <rdb:strategy/>*

    定义数据分库或分表策略

    <rdb:props/>

    名称类型数据类型必填说明
    sql.show属性boolean是否开启SQL显示,默认为false不开启
    executor.size属性int最大工作线程数量

    <rdb:master-slave-data-source/>

    名称类型数据类型必填说明
    id属性StringSpring Bean ID
    master-data-source-ref标签-主库数据源Bean ID
    slave-data-sources-ref标签-从库数据源Bean列表,多个Bean以逗号分隔
    strategy-ref?标签-主从库复杂策略Bean ID,可以使用自定义复杂策略
    strategy-type?标签String主从库复杂策略类型可选值:ROUND_ROBIN, RANDOM默认值:ROUND_ROBIN

    Spring格式特别说明

    如需使用inline表达式,需配置ignore-unresolvable为true,否则placeholder会把inline表达式当成属性key值导致出错.

    ${begin..end} 表示范围区间

    ${[unit1, unit2, unitX]} 表示枚举值

    inline表达式中连续多个${…}表达式,整个inline最终的结果将会根据每个子表达式的结果进行笛卡尔组合,例如正式表inline表达式如下:

    最终会解析为dbtbl_online_1,dbtbl_online_2,dbtbl_online_3,dbtbl_offline_1,dbtbl_offline_2和dbtbl_offline_3这6张表。

    字符串内嵌groovy代码

    上面的表达式中datasource是字符串前缀,id.longValue() % 2 + 1是groovy代码。

    分区键值获取

    groovy代码中可以使用分区键的名字直接获取表达式的值对象。

    该对象是com.dangdang.ddframe.rdb.sharding.config.common.internal.algorithm.ShardingValueWrapper类型的对象。

    该类中提供了一些方法,方便数据类型的转换。包装的原始类型一般为Number,java.util.Date,String 三种类型。使用类中的方法可以将这三种类型转换为需要的其他类型。

    方法列表如下:

    方法名参数返回类型
    longValue()long
    doubleValue()double
    dateValue(String format)时间格式化表达式java.util.Date
    dateValue()java.util.Date
    toString(String format)时间格式化表达式String
    toString()String