官方網(wǎng)址:http://shardingsphere.io
與其他中間件對比
Sharding-JDBC的整體架構(gòu)圖
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/
實現(xiàn)了按某個字段所帶的日期進行分表,供參考,后臺的表半年一個束倍,例如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();
}
}
源碼分析參考: