配置解读

分库分表5种分片策略
第一种:none
对应NoneShardingStragey,不分片策略,SQL会被发给所有节点去执行,这个规则没有子项目可以配置。
第二种:inline 行表达时分片策略(核心,必须要掌握)
对应InlineShardingStragey。使用Groovy的表达时,提供对SQL语句种的=和in的分片操作支持,只支持单分片键。对于简单的分片算法,可以通过简单的配置使用,从而避免繁琐的Java代码开放,如:ksd_user${分片键(数据表字段)userid % 5} 表示ksd_user表根据某字段(userid)模 5.从而分为5张表,表名称为:ksd_user0到ksd_user4 。如果库也是如此。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| server: port: 8085 spring: main: allow-bean-definition-overriding: true shardingsphere: props: sql: show: true datasource: names: ds0,ds1 ds0: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://47.115.94.78:3306/ksd-sharding-db?useUnicode=true&characterEncoding=utf8&tinyInt1isBit=false&useSSL=false&serverTimezone=GMT username: root password: mkxiaoer1986. maxPoolSize: 100 minPoolSize: 5 ds1: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://114.215.145.201:3306/ksd-sharding-db?useUnicode=true&characterEncoding=utf8&tinyInt1isBit=false&useSSL=false&serverTimezone=GMT username: root password: mkxiaoer1986. maxPoolSize: 100 minPoolSize: 5 sharding: default-data-source-name: ds0 tables: ksd_user: actual-data-nodes: ds$->{0..1}.ksd_user$->{0..1} database-strategy: inline: sharding-column: sex algorithm-expression: ds$->{sex % 2} table-strategy: inline: sharding-column: age algorithm-expression: ksd_user$->{age % 2}
mybatis: mapper-locations: classpath:mapper/*.xml type-aliases-package: com.xuexiangban.shardingjdbc.entity
|
algorithm-expression行表达式:
- ${begin…end} 表示区间范围。
- ${[unit1,unit2,….,unitn]} 表示枚举值。
- 行表达式种如果出现连续多个${expresssion}或 $->{expression} 表达式,整个表达时最终的结果将会根据每个子表达式的结果进行笛卡尔组合。
第三种:根据实时间日期 - 按照标准规则分库分表
- 对应StrandardShardingStrategy.提供对SQL语句中的=,in和恶between and 的分片操作支持。
- StrandardShardingStrategy只支持但分片键。提供PreciseShardingAlgorithm和RangeShardingAlgorithm两个分片算法。
- PreciseShardingAlgorithm是必选的呃,用于处理=和IN的分片
- 和RangeShardingAlgorithm是可选的,是用于处理Betwwen and分片,如果不配置和RangeShardingAlgorithm,SQL的Between AND 将按照全库路由处理。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| server: port: 8085 spring: main: allow-bean-definition-overriding: true shardingsphere: props: sql: show: true datasource: names: ds0,ds1 ds0: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://47.115.94.78:3306/ksd-sharding-db?useUnicode=true&characterEncoding=utf8&tinyInt1isBit=false&useSSL=false&serverTimezone=GMT%2b8 username: root password: mkxiaoer1986. maxPoolSize: 100 minPoolSize: 5 ds1: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://114.215.145.201:3306/ksd-sharding-db?useUnicode=true&characterEncoding=utf8&tinyInt1isBit=false&useSSL=false&serverTimezone=GMT%2b8 username: root password: mkxiaoer1986. maxPoolSize: 100 minPoolSize: 5 sharding: default-data-source-name: ds0 tables: ksd_user: actual-data-nodes: ds$->{0..1}.ksd_user$->{0..1} database-strategy: standard: shardingColumn: birthday preciseAlgorithmClassName: com.xuexiangban.shardingjdbc.algorithm.BirthdayAlgorithm table-strategy: inline: sharding-column: age algorithm-expression: ksd_user$->{age % 2}
mybatis: mapper-locations: classpath:mapper/*.xml type-aliases-package: com.xuexiangban.shardingjdbc.entity
|
第四种:ShardingSphere - 符合分片策略(了解)
- 对应接口:HintShardingStrategy。通过Hint而非SQL解析的方式分片的策略。
- 对于分片字段非SQL决定,而是由其他外置条件决定的场景,克使用SQL hint灵活的注入分片字段。例如:按照用户登录的时间,主键等进行分库,而数据库中并无此字段。SQL hint支持通过Java API和SQL注解两种方式使用。让后分库分表更加灵活。

第五种:ShardingSphere - hint分片策略(了解)
- 对应ComplexShardingStrategy。符合分片策略提供对SQL语句中的-,in和between and的分片操作支持。
- ComplexShardingStrategy支持多分片键,由于多分片键之间的关系复杂,因此并未进行过多的封装,而是直接将分片键组合以及分片操作符透传至分片算法,完全由开发者自己实现,提供最大的灵活度。
