點選上方“Java技術驛站”,選擇“置頂公眾號”。
有內涵、有價值的文章第一時間送達!
位於 sharding-jdbc-core
模組下的包 com.dangdang.ddframe.rdb.sharding.hint
中,核心類HintManagerHolder的部分原始碼如下:
/**
* Hint manager holder.
*
Use thread-local to manage hint.
* @author zhangliang
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class HintManagerHolder {
// hint特性儲存資料的核心變數,即儲存一個HintManager型別物件到ThreadLocal中
private static final ThreadLocal<HintManager> HINT_MANAGER_HOLDER = new ThreadLocal<>();
/**
* Set hint manager.
* @param hintManager hint manager instance
*/
public static void setHintManager(final HintManager hintManager) {
Preconditions.checkState(null == HINT_MANAGER_HOLDER.get(), "HintManagerHolder has previous value, please clear first.");
HINT_MANAGER_HOLDER.set(hintManager);
}
public static boolean isUseShardingHint() {
// 判斷當前執行緒中是否使用了sharding hint--即HintManager中的shardingHint為true
return null != HINT_MANAGER_HOLDER.get() && HINT_MANAGER_HOLDER.get().isShardingHint();
}
public static Optional<ShardingValue>> getDatabaseShardingValue(final ShardingKey shardingKey) {
// 如果使用了sharding hint,那麼從ThreadLocal中取資料庫的sharding值
return isUseShardingHint() ? Optional.<ShardingValue>>fromNullable(HINT_MANAGER_HOLDER.get().getDatabaseShardingValue(shardingKey)) : Optional.<ShardingValue>>absent();
}
public static Optional<ShardingValue>> getTableShardingValue(final ShardingKey shardingKey) {
// 如果使用了sharding hint,那麼從ThreadLocal中取表的sharding值
return isUseShardingHint() ? Optional.<ShardingValue>>fromNullable(HINT_MANAGER_HOLDER.get().getTableShardingValue(shardingKey)) : Optional.<ShardingValue>>absent();
}
public static boolean isMasterRouteOnly() {
// 是否強制路由主庫--sharding-jdbc的特性之一:強制路由
return null != HINT_MANAGER_HOLDER.get() && HINT_MANAGER_HOLDER.get().isMasterRouteOnly();
}
public static boolean isDatabaseShardingOnly() {
// 是否只是資料庫sharding
return null != HINT_MANAGER_HOLDER.get() && HINT_MANAGER_HOLDER.get().isDatabaseShardingOnly();
}
/**
* Clear hint manager for current thread-local.
*/
public static void clear() {
// ThreadLocal用完需要清理
HINT_MANAGER_HOLDER.remove();
}
}
ThreadLocal中管理的HintManager定義如下:
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class HintManager implements AutoCloseable {
// 資料庫強制路由的值
private final Map<ShardingKey, ShardingValue>> databaseShardingValues = new HashMap<>();
// 表強制路由的值
private final Map<ShardingKey, ShardingValue>> tableShardingValues = new HashMap<>();
// 即是否使用了強制路由特性
@Getter
private boolean shardingHint;
// 是否強制路由到主資料庫
@Getter
private boolean masterRouteOnly;
@Getter
private boolean databaseShardingOnly;
... ...
@Override
public void close() {
HintManagerHolder.clear();
}
}
sharding值儲存在ThreadLocal中,所以需要在操作結束時呼叫HintManager.close()來清除ThreadLocal中的內容。HintManager實現了AutoCloseable介面,推薦使用
trywithresource
(JDK7新特性,參考Java 7中的Try-with-resources)自動關閉清理ThreadLocl執行緒中的資料。
如何使用
分析了sharding-jdbc的強制路由實現的原始碼,接下來說說如何使用這一niubility特性,假定資料源定義如下:
private static ShardingDataSource getShardingDataSource() throws SQLException {
DataSourceRule dataSourceRule = new DataSourceRule(createDataSourceMap());
TableRule orderTableRule = TableRule
.builder("t_order")
.actualTables(Arrays.asList("t_order_0", "t_order_1"))
.dataSourceRule(dataSourceRule)
.build();
TableRule orderItemTableRule = TableRule
.builder("t_order_item")
.actualTables(Arrays.asList("t_order_item_0", "t_order_item_1"))
.dataSourceRule(dataSourceRule)
.build();
ShardingRule shardingRule = ShardingRule.builder()
.dataSourceRule(dataSourceRule)
.tableRules(Arrays.asList(orderTableRule, orderItemTableRule))
.bindingTableRules(Collections.singletonList(new BindingTableRule(Arrays.asList(orderTableRule, orderItemTableRule))))
.databaseShardingStrategy(new DatabaseShardingStrategy("user_id", new ModuloDatabaseShardingAlgorithm()))
.tableShardingStrategy(new TableShardingStrategy("order_id", new ModuloTableShardingAlgorithm())).build();
return new ShardingDataSource(shardingRule);
}
根據資料源定義可知,資料庫的sharding column為userid,表的sharding column為orderid;
1、強制路由資料庫
-
如何使用
private static void printHintSimpleSelect(final DataSource dataSource) throws SQLException {
// SQL陳述句並不涉及任何資料庫路由和表路由資訊(即where陳述句中沒有user_id條件和order_id條件)
String sql = "SELECT i.* FROM t_order o JOIN t_order_item i ON o.order_id=i.order_id";
try (
HintManager hintManager = HintManager.getInstance();
Connection conn = dataSource.getConnection();
PreparedStatement preparedStatement = conn.prepareStatement(sql)) {
// 強制路由:資料庫路由sharding column即user_id的值為10
hintManager.addDatabaseShardingValue("t_order", "user_id", 10);
try (ResultSet rs = preparedStatement.executeQuery()) {
//todo do something
}
}
}
由於指定了強制路由資料庫的值user_id=10,所以只會輸出
ds_jdbc_0
這個庫中符合條件的資料。而ds_jdbc_1
會被過濾;
-
實現原理
private Collection<String> routeDataSources(final TableRule tableRule) {
// 首先得到資料庫sharding策略,例如:資料庫按照列user_id進行sharding
DatabaseShardingStrategy strategy = shardingRule.getDatabaseShardingStrategy(tableRule);
// 然後從ThreadLocal中取出sharding的值
List<ShardingValue>> shardingValues = HintManagerHolder.isUseShardingHint() ? getDatabaseShardingValuesFromHint(strategy.getShardingColumns())
: getShardingValues(strategy.getShardingColumns());
Collection<String> result = strategy.doStaticSharding(tableRule.getActualDatasourceNames(), shardingValues);
Preconditions.checkState(!result.isEmpty(), "no database route info");
return result;
}
2、強制路由表
-
如何使用
private static void printHintSimpleSelect(final DataSource dataSource) throws SQLException {
// SQL陳述句並不涉及任何資料庫路由和表路由資訊(即where陳述句中沒有user_id條件和order_id條件)
String sql = "SELECT i.* FROM t_order o JOIN t_order_item i ON o.order_id=i.order_id";
try (
HintManager hintManager = HintManager.getInstance();
Connection conn = dataSource.getConnection();
PreparedStatement preparedStatement = conn.prepareStatement(sql)) {
// 強制路由:表路由sharding column即order_id的值為1000
hintManager.addTableShardingValue("t_order", "order_id", 1000);
try (ResultSet rs = preparedStatement.executeQuery()) {
//todo do something
}
}
}
由於指定了強制路由表的值order_id=1000,所以只會輸出所有庫中與
t_order_0
匹配的資料。而與t_order_1
匹配的資料會被過濾;
-
實現原理
private Collection<String> routeTables(final TableRule tableRule, final String routedDataSource) {
// 首先得到表的sharding策略,例如:表按照列order_id進行sharding
TableShardingStrategy strategy = shardingRule.getTableShardingStrategy(tableRule);
// 然後從ThreadLocal中取出sharding的值
List<ShardingValue>> shardingValues = HintManagerHolder.isUseShardingHint() ? getTableShardingValuesFromHint(strategy.getShardingColumns())
: getShardingValues(strategy.getShardingColumns());
Collection<String> result = tableRule.isDynamic() ? strategy.doDynamicSharding(shardingValues) : strategy.doStaticSharding(tableRule.getActualTableNames(routedDataSource), shardingValues);
Preconditions.checkState(!result.isEmpty(), "no table route info");
return result;
}
3、強制路由主庫
-
如何使用
HintManager hintManager = HintManager.getInstance();
hintManager.setMasterRouteOnly();
END