SpringCloud下结合shardingSphere进行分库分表(实现ShardingAlgorithm)

通过ShardingAlgorithm的实现,可以进一步发现分片策略的灵活和强大;可以实现一致性hash算法、按时间分片算法、以及mod算法等;

更进一步,可以对同一个表按业务需求实现不同的分片算法,比如原来按年分片的业务表,比如随着业务量的扩展,需要提高分片频率,

可是又不想进行大量历史数据迁移,可以在某一时刻开始按月或者按日分片;当然前提是要维护一个相对复杂的分片算法;

下面展示一个自定义分片算法原型,留作业务扩展;

 

业务模型和上一篇的inline表达式一样,下面进行核心代码说明:

1)核心pom文件内容

SpringCloud下结合shardingSphere进行分库分表(实现ShardingAlgorithm)
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>
        <dependency>
            <groupId>io.shardingsphere</groupId>
            <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
SpringCloud下结合shardingSphere进行分库分表(实现ShardingAlgorithm)

 

2)核心yml内容:

SpringCloud下结合shardingSphere进行分库分表(实现ShardingAlgorithm)
sharding:
  jdbc:
    datasource:
      names: master0,master0salve0,master0slave1,master1,master1slave0,master1slave1
      master0:
        type: com.alibaba.druid.pool.DruidDataSource
        url: jdbc:mysql://localhost:3306/mcspcsharding0?useUnicode=true&character_set_server=utf8mb4&useSSL=false&serverTimezone=GMT%2B8
        username: root
        password: root
      master0salve0:
        type: com.alibaba.druid.pool.DruidDataSource
        url: jdbc:mysql://localhost:3306/mcspcsharding0s0?useUnicode=true&character_set_server=utf8mb4&useSSL=false&serverTimezone=GMT%2B8
        username: root
        password: root
      master0slave1:
        type: com.alibaba.druid.pool.DruidDataSource
        url: jdbc:mysql://localhost:3306/mcspcsharding0s1?useUnicode=true&character_set_server=utf8mb4&useSSL=false&serverTimezone=GMT%2B8
        username: root
        password: root
      master1:
        type: com.alibaba.druid.pool.DruidDataSource
        url: jdbc:mysql://localhost:3306/mcspcsharding1?useUnicode=true&character_set_server=utf8mb4&useSSL=false&serverTimezone=GMT%2B8
        username: root
        password: root
      master1slave0:
        type: com.alibaba.druid.pool.DruidDataSource
        url: jdbc:mysql://localhost:3306/mcspcsharding1s0?useUnicode=true&character_set_server=utf8mb4&useSSL=false&serverTimezone=GMT%2B8
        username: root
        password: root
      master1slave1:
        type: com.alibaba.druid.pool.DruidDataSource
        url: jdbc:mysql://localhost:3306/mcspcsharding1s1?useUnicode=true&character_set_server=utf8mb4&useSSL=false&serverTimezone=GMT%2B8
        username: root
        password: root
    config:
      sharding:
        tables:
          mc_member:
            actual-nodes: mcspcsharding$->{0..1}.mc_member$->{0..1}
            database-strategy:
              standard:
                sharding-column: gender
                precise-algorithm-class-name: com.chong.mcspcshardingdbtable.sharding.DbShardingAlgorithm
            table-strategy:
              complex:
                sharding-columns: id
                algorithm-class-name: com.chong.mcspcshardingdbtable.sharding.MemberTblComplexKeySharding
        binding-tables: mc_member  # 多个时逗号隔开
        broadcast-tables: mc_master
        master-slave-rules:
          ms0:
            master-data-source-name: master0
            slave-data-source-names: master0salve0,master0slave1
          ms1:
            master-data-source-name: master1
            slave-data-source-names: master1slave0,master1slave1
      props:
        sql:
          show: true
SpringCloud下结合shardingSphere进行分库分表(实现ShardingAlgorithm)

 

3)database数据源的sharding算法,实现了PreciseShardingAlgorithm

 

SpringCloud下结合shardingSphere进行分库分表(实现ShardingAlgorithm)
package com.chong.mcspcshardingdbtable.sharding;

import io.shardingsphere.api.algorithm.sharding.PreciseShardingValue;
import io.shardingsphere.api.algorithm.sharding.standard.PreciseShardingAlgorithm;
import org.springframework.stereotype.Component;

import java.util.Collection;

@Component
public class DbShardingAlgorithm implements PreciseShardingAlgorithm<Integer> {

    @Override
    public String doSharding(Collection<String> collection, PreciseShardingValue<Integer> preciseShardingValue) {

        Integer index = preciseShardingValue.getValue() % 2;
        for (String dataSourceName : collection) {
            if (dataSourceName.endsWith(index + "")) {
                return dataSourceName;
            }
        }
        throw new UnsupportedOperationException();
    }
}
SpringCloud下结合shardingSphere进行分库分表(实现ShardingAlgorithm)

 

 

4)table的sharding算法,实现了ComplexKeysShardingAlgorithm

SpringCloud下结合shardingSphere进行分库分表(实现ShardingAlgorithm)
package com.chong.mcspcshardingdbtable.sharding;

import com.google.common.collect.Range;
import io.shardingsphere.api.algorithm.sharding.ListShardingValue;
import io.shardingsphere.api.algorithm.sharding.PreciseShardingValue;
import io.shardingsphere.api.algorithm.sharding.RangeShardingValue;
import io.shardingsphere.api.algorithm.sharding.ShardingValue;
import io.shardingsphere.api.algorithm.sharding.complex.ComplexKeysShardingAlgorithm;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 通过复合分片键进行演示,覆盖Precise,Range,List三种类型的ShardingValue。
 * 项目中应根据实际情况实现:
 * 1精确分片PreciseShardingAlgorithm、
 * 2范围分片RangeShardingAlgorithm
 * 3复合分片ComplexKeysShardingAlgorithm
 * 4非SQL解析分片HintShardingAlgorithm
 */
@Component
public class MemberTblComplexKeySharding implements ComplexKeysShardingAlgorithm {

    private static String shardingColumn1 = "id"; // todo: 业务扩展 shardingcolumn2...n
    private static String targetLogicTable = "mc_member";

    @Override
    public Collection<String> doSharding(Collection<String> logicTables, Collection<ShardingValue> shardingValues) {
        // 当设置多个shardingcolumn时,重写下面逻辑,根据shardingValues参数和实际分表业务规则计算出实际的actualTable
        List<Long> ids = new ArrayList<>(); // todo: 业务扩展 sharding-cloumn-parame-value-List2...n
        ShardingValue shardingValue = getShardingValue(shardingValues, shardingColumn1); // todo: 业务扩展 shardingValue2..n
        if (shardingValue instanceof PreciseShardingValue) {
            PreciseShardingValue<Long> preciseShardingValue = (PreciseShardingValue<Long>) shardingValue;
            Long id = preciseShardingValue.getValue();
            ids.add(id);
        } else if (shardingValue instanceof RangeShardingValue) {
            RangeShardingValue<Long> rangeShardingValue = (RangeShardingValue<Long>) shardingValue;
            Range<Long> range = rangeShardingValue.getValueRange();
            for (Long index = range.lowerEndpoint(); index <= range.upperEndpoint(); index++) {
                ids.add(index);
            }
        } else if (shardingValue instanceof ListShardingValue) {
            ListShardingValue<Long> listShardingValue = (ListShardingValue<Long>) shardingValue;
            ids.addAll(listShardingValue.getValues());
        }
        return getActualTables(logicTables, ids, targetLogicTable);  // todo:业务扩展 传参 sharding-cloumn-parame-value-List2...n
    }

    /**
     * 根据sql语句中的id值,和logicTable进行拼接,获取实际要查询的表.
     * 实际业务中遇到多个分片列时,除了ids还需要考虑其他key值,合并计算对逻辑表进行组装
     */
    private List<String> getActualTables(Collection<String> logicTables, List<Long> ids, String targetLogicTable) {
        List<String> actualTables = new ArrayList<>();
        if (logicTables.contains(targetLogicTable)) {
            for (Long id : ids) {
                // 作为演示,仅对id%2作为表后缀的匹配规则,今后应根据实际情况重置分片列和匹配规则。
                String actualTableName = targetLogicTable + id%2;
                if(!actualTables.contains(actualTableName)) {
                    actualTables.add(targetLogicTable + (id % 2));
                }
            }
        }
        if (actualTables.size() == 0) {
            throw new UnsupportedOperationException();
        }
        return actualTables;
    }

    /**
     * 根据预知的分拆列获取到对应的shardingvalue对象
     */
    private ShardingValue getShardingValue(Collection<ShardingValue> shardingValues, String column) {
        for (ShardingValue sv : shardingValues) {
            if (sv.getColumnName().equals(column)) {
                return sv;
            }
        }
        throw new UnsupportedOperationException();
    }
}
SpringCloud下结合shardingSphere进行分库分表(实现ShardingAlgorithm)

 

5)启动类

SpringCloud下结合shardingSphere进行分库分表(实现ShardingAlgorithm)
package com.chong.mcspcshardingdbtable;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@SpringBootApplication(exclude = {DruidDataSourceAutoConfigure.class})
@EnableDiscoveryClient
@EnableTransactionManagement
@ComponentScan(basePackages = {"com.chong.common","com.chong.mcspcshardingdbtable"})
public class McSpcShardingDbTableApplication {

    public static void main(String[] args) {
        SpringApplication.run(McSpcShardingDbTableApplication.class, args);
    }

}
SpringCloud下结合shardingSphere进行分库分表(实现ShardingAlgorithm)

 

上一篇:MySQL日期和时间函数汇总


下一篇:关闭子框架(Java iReport)时如何防止父框架关闭?