文章目录
- 业务场景
- 依赖
- 配置
- 特别注意
- 优劣
- 参考资料
业务场景
在 报表
等 大数据量
且需要 按照日期显示
的业务场景下,按照 日期水平分表
是一个不错的选择
依赖
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.7.17.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent>
<dependency><groupId>org.apache.shardingsphere</groupId><artifactId>shardingsphere-jdbc-core-spring-boot-starter</artifactId><version>5.2.1</version></dependency><dependency><groupId>org.yaml</groupId><artifactId>snakeyaml</artifactId><version>1.33</version></dependency>
配置
bootstrap.yml
spring:shardingsphere:mode: # 模式配置type: Standalone # 单机模式repository:type: JDBC # 存储类型props:sql-show: true # 是否打印 SQLdataSources: # 数据源配置names: ds0ds0: # 数据源名称type: com.zaxxer.hikari.HikariDataSourcedriverClassName: com.mysql.jdbc.DriverjdbcUrl: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=falseusername: rootpassword: rootpasswordrules: # 规则配置sharding: # 分片标签tables: # 表规则DASHBOARD_CUS_DATE: # 逻辑表名称actualDataNodes: ds0.DASHBOARD_CUS_DATE_$->{2024..2099}$->{(1..12).collect{m -> sprintf('%02d', m)}}$->{(1..31).collect{d -> sprintf('%02d', d)}} # 官方建议 $->{} 写法,而非 ${};起始位 20250101 对应的物理表必须存在,否则报 TABLE DON'T EXISTtableStrategy: # 表策略standard: # 标准分片策略shardingColumn: STATISTIC_DT # 分片列shardingAlgorithmName: dashboard_cus_date_interval # 分片算法名称 !!!禁止大写shardingAlgorithms: # 分片算法dashboard_cus_date_interval: # 分片算法名称 !!!禁止大写type: INTERVAL # 时间分片算法props: # 分片算法属性datetime-pattern: 'yyyy-MM-dd' # 时间格式datetime-lower: '2025-01-01' # 起始时间datetime-upper: '2099-12-31' # 结束时间datetime-interval-amount: 1 # 时间间隔长度datetime-interval-unit: 'DAYS' # 时间间隔单位datetime-suffix-pattern: 'yyyyMMdd' # 时间后缀格式
特别注意
- 水平分表时,如果未配置自动分表(
autoTables
标签,从5.3.0
开始支持),最好手动将涉及的物理表都创建好,否则很可能遇到Table doesn't exist
错误 - 分片算法名称注意大小写,
shardingsphere-jdbc-core-spring-boot-starter 大小写敏感
,算法名称大写会导致props
属性内容加载失败
优劣
- 优:远程配置方便,与常规项目一般无二
- 劣:大杂烩,都放在一起
参考资料
- ShardingSphere 中文官网