sharding-sphere分片算法和分片策略

官方文档:官方文档

sharding-sphere分片算法和分片策略_第1张图片
分片算法

类图:
sharding-sphere分片算法和分片策略_第2张图片
分片算法.png

所有的分片算法都有一个共同的方法( doSharding),因为参数不同,所以并没有在 ShardingAlgorithm中体现。代码如下:

public interface ShardingAlgorithm {
}

public interface HintShardingAlgorithm extends ShardingAlgorithm {
    Collection doSharding(Collection availableTargetNames, ShardingValue shardingValue);
}

public interface ComplexKeysShardingAlgorithm extends ShardingAlgorithm {
    Collection doSharding(Collection availableTargetNames, Collection shardingValues);
}

public interface PreciseShardingAlgorithm> extends ShardingAlgorithm {
    String doSharding(Collection availableTargetNames, PreciseShardingValue shardingValue);
}
public interface RangeShardingAlgorithm> extends ShardingAlgorithm {
    Collection doSharding(Collection availableTargetNames, RangeShardingValue shardingValue);
}
sharding-sphere分片算法和分片策略_第3张图片
分片策略

分片策略.png

分片策略主要有两个方法,一个是获取分片项,一个是分片,分片主要是依赖于分片算法做分片,具体代码如下:

public interface ShardingStrategy {
    //获取分片项
    Collection getShardingColumns();  
    //做分片
    Collection doSharding(Collection availableTargetNames, Collection shardingValues);
}

复合分片算法实现如下:

public final class ComplexShardingStrategy implements ShardingStrategy {
     //分片项
    @Getter
    private final Collection 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 doSharding(final Collection availableTargetNames, final Collection shardingValues) {
        //调用分片算法做分片
        Collection shardingResult = shardingAlgorithm.doSharding(availableTargetNames, shardingValues);
        Collection result = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
        //组装分片结果并返回
        result.addAll(shardingResult);
        return result;
    }
}

其他分片策略类似。
分片策略的初始化,是按照不同的分片策略配置,分片策略配置如下:


分片配置.png

代码很简单,就不列出来了。

分片策略是如何被初始化的?

工厂模式,按照不同的策略配置类型初始化不同的策略。

@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。

你可能感兴趣的:(sharding-sphere分片算法和分片策略)