背景:公司的工单业务时分庞大,每个月大概至少会有500万工单,一年就至少会有6000万工单,如果接入的业务方继续增多,那么工单数量就会更加庞大,就需要对数据库进行分表。所以我们使用了按年份月份进行分表。
因为我们项目使用的springboot,所以引入的Maven依赖:
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>4.1.1</version>
</dependency>
配置中心使用的配置:
#数据库源名称,多数据源逗号分隔
spring.shardingsphere.datasource.names = ds
#数据库连接池的类名称
spring.shardingsphere.datasource.ds.type = com.alibaba.druid.pool.DruidDataSource
#数据库驱动类名
spring.shardingsphere.datasource.ds.driver-class-name = com.mysql.jdbc.Driver
#数据库url连接
spring.shardingsphere.datasource.ds.url = jdbc:mysql://xxxxxx/xxx?useUnicode=true&characterEncoding=UTF-8
#数据库用户名
spring.shardingsphere.datasource.ds.username = xxx
#数据库密码
spring.shardingsphere.datasource.ds.password = xxx
#由数据源+表名组成
spring.shardingsphere.sharding.tables.t_wom_work_order.actual-data-nodes = ds.t_wom_work_order_${2020..2021}${(1..12).collect{t ->t.toString().padLeft(2,'0')}}
#单分片键的标准分片场景
#分片列名称
spring.shardingsphere.sharding.tables.t_wom_work_order.table-strategy.standard.sharding-column = code
#精确分片算法类名称,用与=和IN,这个类需实现PreciseShardingAlgorithm接口并提供无参数的构造器
spring.shardingsphere.sharding.tables.t_wom_work_order.table-strategy.standard.precise-algorithm-class-name = com.example.demo.sharding.SingleKeyShardingAlgorithm
#是否开启SQL现实,默认是false
spring.shardingsphere.props.sql.show = false
#绑定表
spring.shardingsphere.sharding.binding-tables[0] = t_wom_work_order,t_wom_work_order_handler
自己需要开发的分片策略接口:因为我的分片列code是由雪花算法生成的,所以可以根据code来反解析出来是哪个年份哪个月生成的。然后根据固定的逻辑表名:t_wom_work_order 就可以找到改code在哪个表中。
public class SingleKeyShardingAlgorithm implements PreciseShardingAlgorithm<String> {
@Override
public String doSharding(Collection<String> availableTargetNames, PreciseShardingValue<String> shardingValue) {
StringBuilder tableName = new StringBuilder();
String code = shardingValue.getValue();
long milliSecond = (Long.parseLong(code) >>> (64 - 42)) + 1546272000000L;
String time = LocalDateTime.ofEpochSecond(milliSecond / 1000, 0, ZoneOffset.ofHours(8)).format(DateTimeFormatter.ofPattern("yyyyMM"));
return tableName.append(shardingValue.getLogicTableName()).append(time).toString();
}
}
在进行从3.0.0版本升级到4.1.1版本后,启动报错
Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2021-10-22 14:13:09.459 ERROR 21648 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Cannot determine embedded database driver class for database type NONE
Action:
If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (no profiles are currently active).
Process finished with exit code 1
原因:pom文件同时配置了 druid的spring-boot-starter 和 sharding-jdbc的spring-boot-starter,
因为 druid的spring-boot-starter 会先加载创建一个默认的数据源,这个会与 sharding-jdbc的spring-boot-starter创建的数据源发送冲突。
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.9</version>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>4.1.1</version>
</dependency>
解决办法: 只要去掉 druid的spring-boot-starter的pom依赖就可以。