sharding jdbc初探
2018-11-21 本文已影响38人
zlmind
与其他中间件对比
imageSharding-JDBC的整体架构图
imageimage.png
SQL支持详细列表
http://shardingsphere.io/document/legacy/2.x/cn/01-start/sql-supported/
JDBC未支持列表
http://shardingsphere.io/document/legacy/2.x/cn/01-start/limitations/
简单分表实例:
maven 引用
<dependency>
<groupId>io.shardingjdbc</groupId>
<artifactId>sharding-jdbc-core</artifactId>
<version>2.0.0.M3</version>
</dependency>
<dependency>
<groupId>io.shardingjdbc</groupId>
<artifactId>sharding-jdbc-spring-namespace</artifactId>
<version>2.0.0.M3</version>
分库分表见:
http://shardingsphere.io/document/legacy/2.x/cn/02-guide/sharding/
实现了按某个字段所带的日期进行分表,供参考,后台的表半年一个,例如XXX_2018_1_6,XXX_2018_7_12
public final class PreciseModuloTableShardingAlgorithm implements PreciseShardingAlgorithm<String> {
@Override
public String doSharding(final Collection<String> availableTargetNames, final PreciseShardingValue<String> shardingValue) {
int year, month;
if (null != shardingValue && null != shardingValue.getValue()) {
String value = shardingValue.getValue();
year = Integer.valueOf(value.substring(0, 4));
month = Integer.valueOf(value.substring(4, 6));
} else {
LocalDate today = LocalDate.now();
year = today.getYear();
month = today.getMonthValue();
}
for (String each : availableTargetNames) {
//获取后缀
String[] split1 = each.split("_");
if (Integer.valueOf(split1[split1.length - 3]).equals(year)) {
if (Integer.valueOf(split1[split1.length - 2]) <= month && month <= Integer.valueOf(split1[split1.length - 1])) {
return each;
}
}
}
throw new UnsupportedOperationException();
}
}
源码分析参考: