配置手册

1.JAVA配置

引入maven依赖

  1. <dependency>
  2. <groupId>io.shardingjdbc</groupId>
  3. <artifactId>sharding-jdbc-core</artifactId>
  4. <version>${latest.release.version}</version>
  5. </dependency>

配置示例

分库分表

  1. DataSource getShardingDataSource() throws SQLException {
  2. ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
  3. shardingRuleConfig.getTableRuleConfigs().add(getOrderTableRuleConfiguration());
  4. shardingRuleConfig.getTableRuleConfigs().add(getOrderItemTableRuleConfiguration());
  5. shardingRuleConfig.getBindingTableGroups().add("t_order, t_order_item");
  6. shardingRuleConfig.setDefaultDatabaseShardingStrategyConfig(new InlineShardingStrategyConfiguration("user_id", "demo_ds_${user_id % 2}"));
  7. shardingRuleConfig.setDefaultTableShardingStrategyConfig(new StandardShardingStrategyConfiguration("order_id", ModuloShardingTableAlgorithm.class.getName()));
  8. return ShardingDataSourceFactory.createDataSource(createDataSourceMap(), shardingRuleConfig);
  9. }
  10. TableRuleConfiguration getOrderTableRuleConfiguration() {
  11. TableRuleConfiguration orderTableRuleConfig = new TableRuleConfiguration();
  12. orderTableRuleConfig.setLogicTable("t_order");
  13. orderTableRuleConfig.setActualDataNodes("demo_ds_${0..1}.t_order_${0..1}");
  14. orderTableRuleConfig.setKeyGeneratorColumnName("order_id");
  15. return orderTableRuleConfig;
  16. }
  17. TableRuleConfiguration getOrderItemTableRuleConfiguration() {
  18. TableRuleConfiguration orderItemTableRuleConfig = new TableRuleConfiguration();
  19. orderItemTableRuleConfig.setLogicTable("t_order_item");
  20. orderItemTableRuleConfig.setActualDataNodes("demo_ds_${0..1}.t_order_item_${0..1}");
  21. return orderItemTableRuleConfig;
  22. }
  23. Map<String, DataSource> createDataSourceMap() {
  24. Map<String, DataSource> result = new HashMap<>(2, 1);
  25. result.put("demo_ds_0", DataSourceUtil.createDataSource("demo_ds_0"));
  26. result.put("demo_ds_1", DataSourceUtil.createDataSource("demo_ds_1"));
  27. return result;
  28. }

读写分离

  1. DataSource getMasterSlaveDataSource() throws SQLException {
  2. MasterSlaveRuleConfiguration masterSlaveRuleConfig = new MasterSlaveRuleConfiguration();
  3. masterSlaveRuleConfig.setName("demo_ds_master_slave");
  4. masterSlaveRuleConfig.setMasterDataSourceName("demo_ds_master");
  5. masterSlaveRuleConfig.setSlaveDataSourceNames(Arrays.asList("demo_ds_slave_0", "demo_ds_slave_1"));
  6. return MasterSlaveDataSourceFactory.createDataSource(createDataSourceMap(), masterSlaveRuleConfig);
  7. }
  8. Map<String, DataSource> createDataSourceMap() {
  9. final Map<String, DataSource> result = new HashMap<>(3, 1);
  10. result.put("demo_ds_master", DataSourceUtil.createDataSource("demo_ds_master"));
  11. result.put("demo_ds_slave_0", DataSourceUtil.createDataSource("demo_ds_slave_0"));
  12. result.put("demo_ds_slave_1", DataSourceUtil.createDataSource("demo_ds_slave_1"));
  13. return result;
  14. }

2.YAML配置

引入maven依赖

  1. <dependency>
  2. <groupId>io.shardingjdbc</groupId>
  3. <artifactId>sharding-jdbc-core</artifactId>
  4. <version>${latest.release.version}</version>
  5. </dependency>

配置示例

分库分表

  1. dataSources:
  2. db0: !!org.apache.commons.dbcp.BasicDataSource
  3. driverClassName: org.h2.Driver
  4. url: jdbc:h2:mem:db0;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL
  5. username: sa
  6. password:
  7. maxActive: 100
  8. db1: !!org.apache.commons.dbcp.BasicDataSource
  9. driverClassName: org.h2.Driver
  10. url: jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL
  11. username: sa
  12. password:
  13. maxActive: 100
  14. shardingRule:
  15. tables:
  16. config:
  17. actualDataNodes: db${0..1}.t_config
  18. t_order:
  19. actualDataNodes: db${0..1}.t_order_${0..1}
  20. databaseStrategy:
  21. standard:
  22. shardingColumn: user_id
  23. preciseAlgorithmClassName: io.shardingjdbc.core.yaml.fixture.SingleAlgorithm
  24. tableStrategy:
  25. inline:
  26. shardingColumn: order_id
  27. algorithmInlineExpression: t_order_${order_id % 2}
  28. keyGeneratorColumnName: order_id
  29. keyGeneratorClass: io.shardingjdbc.core.yaml.fixture.IncrementKeyGenerator
  30. t_order_item:
  31. actualDataNodes: db${0..1}.t_order_item_${0..1}
  32. #绑定表中其余的表的策略与第一张表的策略相同
  33. databaseStrategy:
  34. standard:
  35. shardingColumn: user_id
  36. preciseAlgorithmClassName: io.shardingjdbc.core.yaml.fixture.SingleAlgorithm
  37. tableStrategy:
  38. inline:
  39. shardingColumn: order_id
  40. algorithmInlineExpression: t_order_item_${order_id % 2}
  41. bindingTables:
  42. - t_order,t_order_item
  43. #默认数据库分片策略
  44. defaultDatabaseStrategy:
  45. none:
  46. defaultTableStrategy:
  47. complex:
  48. shardingColumns: id, order_id
  49. algorithmClassName: io.shardingjdbc.core.yaml.fixture.MultiAlgorithm
  50. props:
  51. sql.show: true

分库分表配置项说明

  1. dataSources: 数据源配置
  2. <data_source_name> 可配置多个: !!数据库连接池实现类
  3. driverClassName: 数据库驱动类名
  4. url: 数据库url连接
  5. username: 数据库用户名
  6. password: 数据库密码
  7. ... 数据库连接池的其它属性
  8. defaultDataSourceName: 默认数据源,未配置分片规则的表将通过默认数据源定位
  9. tables: 分库分表配置,可配置多个logic_table_name
  10. <logic_table_name>: 逻辑表名
  11. actualDataNodes: 真实数据节点,由数据源名 + 表名组成,以小数点分隔。多个表以逗号分隔,支持inline表达式。不填写表示将为现有已知的数据源 + 逻辑表名称生成真实数据节点。用于广播表(即每个库中都需要一个同样的表用于关联查询,多为字典表)或只分库不分表且所有库的表结构完全一致的情况。
  12. databaseStrategy: 分库策略,以下的分片策略只能任选其一
  13. standard: 标准分片策略,用于单分片键的场景
  14. shardingColumn: 分片列名
  15. preciseAlgorithmClassName: 精确的分片算法类名称,用于=和IN。该类需使用默认的构造器或者提供无参数的构造器
  16. rangeAlgorithmClassName: 范围的分片算法类名称,用于BETWEEN,可以不配置。该类需使用默认的构造器或者提供无参数的构造器
  17. complex: 复合分片策略,用于多分片键的场景
  18. shardingColumns : 分片列名,多个列以逗号分隔
  19. algorithmClassName: 分片算法类名称。该类需使用默认的构造器或者提供无参数的构造器
  20. inline: inline表达式分片策略
  21. shardingColumn : 分片列名
  22. algorithmInlineExpression: 分库算法Inline表达式,需要符合groovy动态语法
  23. hint: Hint分片策略
  24. algorithmClassName: 分片算法类名称。该类需使用默认的构造器或者提供无参数的构造器
  25. none: 不分片
  26. tableStrategy: 分表策略,同分库策略
  27. bindingTables: 绑定表列表
  28. - 逻辑表名列表,多个<logic_table_name>以逗号分隔
  29. defaultDatabaseStrategy: 默认数据库分片策略,同分库策略
  30. defaultTableStrategy: 默认数据表分片策略,同分库策略
  31. props: 属性配置(可选)
  32. sql.show: 是否开启SQL显示,默认值: false
  33. executor.size: 工作线程数量,默认值: CPU核数

分库分表数据源构建方式

  1. DataSource dataSource = ShardingDataSourceFactory.createDataSource(yamlFile);

读写分离

  1. dataSources:
  2. db_master: !!org.apache.commons.dbcp.BasicDataSource
  3. driverClassName: org.h2.Driver
  4. url: jdbc:h2:mem:db_master;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL
  5. username: sa
  6. password:
  7. maxActive: 100
  8. db_slave_0: !!org.apache.commons.dbcp.BasicDataSource
  9. driverClassName: org.h2.Driver
  10. url: jdbc:h2:mem:db_slave_0;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL
  11. username: sa
  12. password:
  13. maxActive: 100
  14. db_slave_1: !!org.apache.commons.dbcp.BasicDataSource
  15. driverClassName: org.h2.Driver
  16. url: jdbc:h2:mem:db_slave_1;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL
  17. username: sa
  18. password:
  19. maxActive: 100
  20. masterSlaveRule:
  21. name: db_ms
  22. masterDataSourceName: db_master
  23. slaveDataSourceNames: [db_slave_0, db_slave_1]

读写分离配置项说明

  1. dataSource: 数据源配置,同分库分表
  2. name: 分库分表数据源名称
  3. masterDataSourceName: master数据源名称
  4. slaveDataSourceNamesslave数据源名称,用数组表示多个

读写分离数据源构建方式

  1. DataSource dataSource = MasterSlaveDataSourceFactory.createDataSource(yamlFile);

YAML格式特别说明

!! 表示实现类

[] 表示多个

3.Spring命名空间配置

引入maven依赖

  1. <dependency>
  2. <groupId>io.shardingjdbc</groupId>
  3. <artifactId>sharding-jdbc-core-spring-namespace</artifactId>
  4. <version>${latest.release.version}</version>
  5. </dependency>

配置示例

  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:sharding="http://shardingsphere.io/schema/shardingjdbc/sharding"
  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://shardingsphere.io/schema/shardingjdbc/sharding
  11. http://shardingsphere.io/schema/shardingjdbc/sharding/sharding.xsd
  12. ">
  13. <context:property-placeholder location="classpath:conf/rdb/conf.properties" ignore-unresolvable="true" />
  14. <bean id="dbtbl_0" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  15. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  16. <property name="url" value="jdbc:mysql://localhost:3306/dbtbl_0" />
  17. <property name="username" value="root" />
  18. <property name="password" value="" />
  19. </bean>
  20. <bean id="dbtbl_1" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  21. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  22. <property name="url" value="jdbc:mysql://localhost:3306/dbtbl_1" />
  23. <property name="username" value="root" />
  24. <property name="password" value="" />
  25. </bean>
  26. <sharding:standard-strategy id="databaseStrategy" sharding-column="user_id" precise-algorithm-class="io.shardingjdbc.spring.algorithm.PreciseModuloDatabaseShardingAlgorithm" />
  27. <sharding:standard-strategy id="tableStrategy" sharding-column="order_id" precise-algorithm-class="io.shardingjdbc.spring.algorithm.PreciseModuloTableShardingAlgorithm" />
  28. <sharding:data-source id="shardingDataSource">
  29. <sharding:sharding-rule data-source-names="dbtbl_0,dbtbl_1" default-data-source-name="dbtbl_0">
  30. <sharding:table-rules>
  31. <sharding:table-rule logic-table="t_order" actual-data-nodes="dbtbl_${0..1}.t_order_${0..3}" database-strategy-ref="databaseStrategy" table-strategy-ref="tableStrategy" />
  32. <sharding:table-rule logic-table="t_order_item" actual-data-nodes="dbtbl_${0..1}.t_order_item_${0..3}" database-strategy-ref="databaseStrategy" table-strategy-ref="tableStrategy" />
  33. </sharding:table-rules>
  34. <sharding:binding-table-rules>
  35. <sharding:binding-table-rule logic-tables="t_order, t_order_item" />
  36. </sharding:binding-table-rules>
  37. </sharding:sharding-rule>
  38. <sharding:props>
  39. <prop key="sql.show">true</prop>
  40. </sharding:props>
  41. </sharding:data-source>
  42. </beans>

标签说明

<sharding:data-source/>

定义sharding-jdbc数据源

名称类型数据类型必填说明
id属性StringSpring Bean ID
sharding-rule标签-分片规则
binding-table-rules?标签-绑定表规则
props?标签-相关属性配置

<sharding:sharding-rule/>

名称类型数据类型必填说明
data-source-names属性String数据源Bean列表,需要配置所有需要被Sharding-JDBC管理的数据源BEAN ID(包括默认数据源),多个Bean以逗号分隔
default-data-source-name属性String默认数据源名称,未配置分片规则的表将通过默认数据源定位
default-database-strategy-ref属性String默认分库策略,对应<sharding:xxx-strategy>中的策略id,不填则使用不分库的策略
default-table-strategy-ref属性String默认分表策略,对应<sharding:xxx-strategy>中的策略id,不填则使用不分表的策略
table-rules标签-分片规则列表

<sharding:table-rules/>

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

<sharding:table-rule/>

名称类型数据类型必填说明
logic-table属性String逻辑表名
actual-data-nodes属性String真实数据节点,由数据源名(读写分离引用master-slave:data-source中的id属性) + 表名组成,以小数点分隔。多个表以逗号分隔,支持inline表达式。不填写表示将为现有已知的数据源 + 逻辑表名称生成真实数据节点。用于广播表(即每个库中都需要一个同样的表用于关联查询,多为字典表)或只分库不分表且所有库的表结构完全一致的情况。
database-strategy-ref属性String分库策略,对应<sharding:xxx-strategy>中的策略id,不填则使用<sharding:sharding-rule/>配置的default-database-strategy-ref
table-strategy-ref属性String分表策略,对应<sharding:xxx-strategy>中的略id,不填则使用<sharding:sharding-rule/>配置的default-table-strategy-ref
logic-index属性String逻辑索引名称,对于分表的Oracle/PostgreSQL数据库中DROP INDEX XXX语句,需要通过配置逻辑索引名称定位所执行SQL的真实分表

<sharding:binding-table-rules/>

名称类型数据类型必填说明
binding-table-rule标签-绑定规则

<sharding:binding-table-rule/>

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

<sharding:standard-strategy/>

标准分片策略,用于单分片键的场景

名称类型数据类型必填说明
sharding-column属性String分片列名
precise-algorithm-class属性String精确的分片算法类名称,用于=和IN。该类需使用默认的构造器或者提供无参数的构造器
range-algorithm-class属性String范围的分片算法类名称,用于BETWEEN。该类需使用默认的构造器或者提供无参数的构造器

<sharding:complex-strategy/>

复合分片策略,用于多分片键的场景

名称类型数据类型必填说明
sharding-columns属性String分片列名,多个列以逗号分隔
algorithm-class属性String分片算法全类名,该类需使用默认的构造器或者提供无参数的构造器

<sharding:inline-strategy/>

inline表达式分片策略

名称类型数据类型必填说明
sharding-column属性String分片列名
algorithm-expression属性String分片算法表达式

<sharding:hint-database-strategy/>

Hint方式分片策略

名称类型数据类型必填说明
algorithm-class属性String分片算法全类名,该类需使用默认的构造器或者提供无参数的构造器

<sharding:none-strategy/>

不分片的策略

<sharding:props/>

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

<master-slave:data-source/>

定义sharding-jdbc读写分离的数据源

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

Spring格式特别说明

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

分片算法表达式语法说明

inline表达式特别说明

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

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

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

  1. dbtbl_${['online', 'offline']}_${1..3}

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

字符串内嵌groovy代码

表达式本质上是一段字符串,字符串中使用${}来嵌入groovy代码。

  1. data_source_${id % 2 + 1}

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

4.Spring Boot配置

引入maven依赖

  1. <dependency>
  2. <groupId>io.shardingjdbc</groupId>
  3. <artifactId>sharding-jdbc-core-spring-boot-starter</artifactId>
  4. <version>${latest.release.version}</version>
  5. </dependency>

配置示例

分库分表配置

  1. sharding.jdbc.datasource.names=ds,ds_0,ds_1
  2. sharding.jdbc.datasource.ds.type=org.apache.commons.dbcp.BasicDataSource
  3. sharding.jdbc.datasource.ds.driverClassName=org.h2.Driver
  4. sharding.jdbc.datasource.ds.url=jdbc:mysql://localhost:3306/ds
  5. sharding.jdbc.datasource.ds.username=root
  6. sharding.jdbc.datasource.ds.password=
  7. sharding.jdbc.datasource.ds_0.type=org.apache.commons.dbcp.BasicDataSource
  8. sharding.jdbc.datasource.ds_0.driverClassName=com.mysql.jdbc.Driver
  9. sharding.jdbc.datasource.ds_0.url=jdbc:mysql://localhost:3306/ds_0
  10. sharding.jdbc.datasource.ds_0.username=root
  11. sharding.jdbc.datasource.ds_0.password=
  12. sharding.jdbc.datasource.ds_1.type=org.apache.commons.dbcp.BasicDataSource
  13. sharding.jdbc.datasource.ds_1.driverClassName=com.mysql.jdbc.Driver
  14. sharding.jdbc.datasource.ds_1.url=jdbc:mysql://localhost:3306/ds_1
  15. sharding.jdbc.datasource.ds_1.username=root
  16. sharding.jdbc.datasource.ds_1.password=
  17. sharding.jdbc.config.sharding.default-data-source-name=ds
  18. sharding.jdbc.config.sharding.default-database-strategy.inline.sharding-column=user_id
  19. sharding.jdbc.config.sharding.default-database-strategy.inline.algorithm-expression=ds_${user_id % 2}
  20. sharding.jdbc.config.sharding.tables.t_order.actualDataNodes=ds_${0..1}.t_order_${0..1}
  21. sharding.jdbc.config.sharding.tables.t_order.tableStrategy.inline.shardingColumn=order_id
  22. sharding.jdbc.config.sharding.tables.t_order.tableStrategy.inline.algorithmInlineExpression=t_order_${order_id % 2}
  23. sharding.jdbc.config.sharding.tables.t_order.keyGeneratorColumnName=order_id
  24. sharding.jdbc.config.sharding.tables.t_order_item.actualDataNodes=ds_${0..1}.t_order_item_${0..1}
  25. sharding.jdbc.config.sharding.tables.t_order_item.tableStrategy.inline.shardingColumn=order_id
  26. sharding.jdbc.config.sharding.tables.t_order_item.tableStrategy.inline.algorithmInlineExpression=t_order_item_${order_id % 2}
  27. sharding.jdbc.config.sharding.tables.t_order_item.keyGeneratorColumnName=order_item_id

分库分表配置项说明

分库分表Yaml配置

读写分离配置

  1. sharding.jdbc.datasource.names=ds_master,ds_slave_0,ds_slave_1
  2. sharding.jdbc.datasource.ds_master.type=org.apache.commons.dbcp.BasicDataSource
  3. sharding.jdbc.datasource.ds_master.driverClassName=com.mysql.jdbc.Driver
  4. sharding.jdbc.datasource.ds_master.url=jdbc:mysql://localhost:3306/demo_ds_master
  5. sharding.jdbc.datasource.ds_master.username=root
  6. sharding.jdbc.datasource.ds_master.password=
  7. sharding.jdbc.datasource.ds_slave_0.type=org.apache.commons.dbcp.BasicDataSource
  8. sharding.jdbc.datasource.ds_slave_0.driverClassName=com.mysql.jdbc.Driver
  9. sharding.jdbc.datasource.ds_slave_0.url=jdbc:mysql://localhost:3306/demo_ds_slave_0
  10. sharding.jdbc.datasource.ds_slave_0.username=root
  11. sharding.jdbc.datasource.ds_slave_0.password=
  12. sharding.jdbc.datasource.ds_slave_1.type=org.apache.commons.dbcp.BasicDataSource
  13. sharding.jdbc.datasource.ds_slave_1.driverClassName=com.mysql.jdbc.Driver
  14. sharding.jdbc.datasource.ds_slave_1.url=jdbc:mysql://localhost:3306/demo_ds_slave_1
  15. sharding.jdbc.datasource.ds_slave_1.username=root
  16. sharding.jdbc.datasource.ds_slave_1.password=
  17. sharding.jdbc.config.masterslave.load-balance-algorithm-type=round_robin
  18. sharding.jdbc.config.masterslave.name=ds_ms
  19. sharding.jdbc.config.masterslave.master-data-source-name=ds_master
  20. sharding.jdbc.config.masterslave.slave-data-source-names=ds_slave_0,ds_slave_1

读写分离配置项说明

读写分离Yaml配置