官方文档:官方文档
类图:
所有的分片算法都有一个共同的方法(doSharding),因为参数不同,所以并没有在ShardingAlgorithm中体现。代码如下:
public interface ShardingAlgorithm {
}
public interface HintShardingAlgorithm extends ShardingAlgorithm {
Collection<String> doSharding(Collection<String> availableTargetNames, ShardingValue shardingValue);
}
public interface ComplexKeysShardingAlgorithm extends ShardingAlgorithm {
Collection<String> doSharding(Collection<String> availableTargetNames, Collection<ShardingValue> shardingValues);
}
public interface PreciseShardingAlgorithm<T extends Comparable<?>> extends ShardingAlgorithm {
String doSharding(Collection<String> availableTargetNames, PreciseShardingValue<T> shardingValue);
}
public interface RangeShardingAlgorithm<T extends Comparable<?>> extends ShardingAlgorithm {
Collection<String> doSharding(Collection<String> availableTargetNames, RangeShardingValue<T> shardingValue);
}
分片策略主要有两个方法,一个是获取分片项,一个是分片,分片主要是依赖于分片算法做分片,具体代码如下:
public interface ShardingStrategy {
//获取分片项
Collection<String> getShardingColumns();
//做分片
Collection<String> doSharding(Collection<String> availableTargetNames, Collection<ShardingValue> shardingValues);
}
复合分片算法实现如下:
public final class ComplexShardingStrategy implements ShardingStrategy {
//分片项
@Getter
private final Collection<String> shardingColumns;
//分片算法
private final ComplexKeysShardingAlgorithm shardingAlgorithm;
public ComplexShardingStrategy(final ComplexShardingStrategyConfiguration complexShardingStrategyConfig) {
shardingColumns = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
//分片配置中获取所有的分片项,保存在shardingColumns中。
shardingColumns.addAll(StringUtil.splitWithComma(complexShardingStrategyConfig.getShardingColumns()));
//初始化分片算法
shardingAlgorithm = complexShardingStrategyConfig.getShardingAlgorithm();
}
@Override
public Collection<String> doSharding(final Collection<String> availableTargetNames, final Collection<ShardingValue> shardingValues) {
//调用分片算法做分片
Collection<String> shardingResult = shardingAlgorithm.doSharding(availableTargetNames, shardingValues);
Collection<String> result = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
//组装分片结果并返回
result.addAll(shardingResult);
return result;
}
}
其他分片策略类似。
分片策略的初始化,是按照不同的分片策略配置,分片策略配置如下:
代码很简单,就不列出来了。
分片策略是如何被初始化的?
工厂模式,按照不同的策略配置类型初始化不同的策略。
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class ShardingStrategyFactory {
public static ShardingStrategy newInstance(final ShardingStrategyConfiguration shardingStrategyConfig) {
if (shardingStrategyConfig instanceof StandardShardingStrategyConfiguration) {
return new StandardShardingStrategy((StandardShardingStrategyConfiguration) shardingStrategyConfig);
}
if (shardingStrategyConfig instanceof InlineShardingStrategyConfiguration) {
return new InlineShardingStrategy((InlineShardingStrategyConfiguration) shardingStrategyConfig);
}
if (shardingStrategyConfig instanceof ComplexShardingStrategyConfiguration) {
return new ComplexShardingStrategy((ComplexShardingStrategyConfiguration) shardingStrategyConfig);
}
if (shardingStrategyConfig instanceof HintShardingStrategyConfiguration) {
return new HintShardingStrategy((HintShardingStrategyConfiguration) shardingStrategyConfig);
}
return new NoneShardingStrategy();
}
}
the end。