sharding jdbc初探

官方网址:http://shardingsphere.io

与其他中间件对比

image

Sharding-JDBC的整体架构图

image

image.png

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/
实现了按某个字段所带的日期进行分表,供参考,后台的表半年一个,例如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();
    }
}

源码分析参考:

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容