Skip to content

Repository files navigation

##Sharding-JDBC - A JDBC driver for shard databases and tables

Sharding-JDBC是当当应用框架ddframe中,关系型数据库模块dd-rdb中分离出来的数据库水平扩展框架,即透明化数据库分库分表访问。

Sharding-JDBCdubboxelastic-job之后,是ddframe系列开源的第三个产品。

Release Notes

  • sharding-jdbc Maven Status
  • sharding-jdbc-core Maven Status
  • sharding-jdbc-config-yaml Maven Status
  • sharding-jdbc-config-spring Maven Status

License

Hex.pm

Build Status

Build StatusCoverage Status

主要贡献者

**讨论QQ群:**532576663(不限于Sharding-JDBC,包括分布式,数据库相关以及其他互联网技术交流。)

简介

Sharding-JDBC直接封装JDBC API,可以理解为增强版的JDBC驱动,旧代码迁移成本几乎为零:

  • 可适用于任何基于javaORM框架,如:JPA, Hibernate, Mybatis, Spring JDBC Template或直接使用JDBC
  • 可基于任何第三方的数据库连接池,如:DBCP, C3P0, BoneCP, Druid等。
  • 理论上可支持任意实现JDBC规范的数据库。虽然目前仅支持MySQL,但已有支持OracleSQLServerDB2等数据库的计划。

Sharding-JDBC定位为轻量级java框架,使用客户端直连数据库,以jar包形式提供服务,未使用中间层,无需额外部署,无其他依赖,DBA也无需改变原有的运维方式。SQL解析使用Druid解析器,是目前性能最高的SQL解析器。

Sharding-JDBC功能灵活且全面:

  • 分片策略灵活,可支持=BETWEENIN等多维度分片,也可支持多分片键共用。
  • SQL解析功能完善,支持聚合,分组,排序,LimitOR等查询,并且支持Binding Table以及笛卡尔积的表查询。

Sharding-JDBC配置多样:

  • 可支持YAML和Spring命名空间配置
  • 灵活多样的inline方式

以下是常见的分库分表产品和Sharding-JDBC的对比:

功能CobarCobar-clientTDDLSharding-JDBC
分库未开源
分表未开源
中间层
ORM支持任意仅MyBatis任意任意
数据库支持仅MySQL任意任意任意
异构语言仅Java仅Java仅Java
外部依赖Diamond

整体架构图

整体架构图

柔性事务-最大努力送达型

相关文档

Release Notes

使用指南

详细功能列表

核心概念

架构图

Yaml文件和Spring命名空间配置

基于暗示(Hint)的分片键值注册方法

柔性事务

目录结构说明

阅读源码编译问题说明

使用限制

SQL支持详细列表

压力测试报告

未来线路规划

事务支持说明

InfoQ新闻

CSDN文章

Quick Start

引入maven依赖

<!-- 引入sharding-jdbc核心模块 -->
<dependency>
<groupId>com.dangdang</groupId>
<artifactId>sharding-jdbc-core</artifactId>
<version>${latest.release.version}</version>
</dependency>

规则配置

Sharding-JDBC的分库分表通过规则配置描述,请简单浏览配置全貌:

ShardingRuleshardingRule = newShardingRule(
dataSourceRule, Arrays.asList(tableRule), newDatabaseShardingStrategy("sharding_column_1", newXXXShardingAlgorithm()),
newTableShardingStrategy("sharding_column_2", newXXXShardingAlgorithm()));

规则配置包括数据源配置、表规则配置、分库策略和分表策略组成。这只是最简单的配置方式,实际使用可更加灵活,如:多分片键,分片策略直接和tableRule绑定等。

详细的规则配置请参考用户指南

使用原生JDBC接口

通过规则配置对象获取ShardingDataSourceShardingDataSource实现自JDBC的标准接口DataSource。然后可通过DataSource选择使用原生JDBC开发,或者使用JPA, MyBatisORM工具。 以JDBC原生实现为例:

DataSourcedataSource = newShardingDataSource(shardingRule);
Stringsql = "SELECT i.* FROM t_order o JOIN t_order_item i ON o.order_id=i.order_id WHERE o.user_id=? AND o.order_id=?";
try (
Connectionconn = dataSource.getConnection();
PreparedStatementpreparedStatement = conn.prepareStatement(sql)) {
preparedStatement.setInt(1, 10);
preparedStatement.setInt(2, 1001);
try (ResultSetrs = preparedStatement.executeQuery()) {
while(rs.next()) {
System.out.println(rs.getInt(1));
System.out.println(rs.getInt(2));
System.out.println(rs.getInt(3));
}
}
}

使用Spring命名空间配置

<?xml version="1.0" encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:rdb="http://www.dangdang.com/schema/ddframe/rdb"xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context.xsd  http://www.dangdang.com/schema/ddframe/rdb  http://www.dangdang.com/schema/ddframe/rdb/rdb.xsd ">
<context:property-placeholderlocation="classpath:conf/rdb/conf.properties"ignore-unresolvable="true"/>
<beanid="dbtbl_0"class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close">
<propertyname="driverClassName"value="com.mysql.jdbc.Driver"/>
<propertyname="url"value="jdbc:mysql://localhost:3306/dbtbl_0"/>
<propertyname="username"value="root"/>
<propertyname="password"value=""/>
</bean>
<beanid="dbtbl_1"class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close">
<propertyname="driverClassName"value="com.mysql.jdbc.Driver"/>
<propertyname="url"value="jdbc:mysql://localhost:3306/dbtbl_1"/>
<propertyname="username"value="root"/>
<propertyname="password"value=""/>
</bean>
<rdb:strategyid="orderTableStrategy"sharding-columns="order_id"algorithm-expression="t_order_${order_id.longValue() % 4}"/>
<rdb:strategyid="orderItemTableStrategy"sharding-columns="order_id"algorithm-expression="t_order_item_${order_id.longValue() % 4}"/>
<rdb:data-sourceid="shardingDataSource">
<rdb:sharding-ruledata-sources="dbtbl_0,dbtbl_1">
<rdb:table-rules>
<rdb:table-rulelogic-table="t_order"actual-tables="t_order_${0..3}"table-strategy="orderTableStrategy"/>
<rdb:table-rulelogic-table="t_order_item"actual-tables="t_order_item_${0..3}"table-strategy="orderItemTableStrategy"/>
</rdb:table-rules>
<rdb:default-database-strategysharding-columns="user_id"algorithm-expression="dbtbl_${user_id.longValue() % 2 + 1}"/>
</rdb:sharding-rule>
</rdb:data-source>
</beans>

About

A JDBC driver for shard databases and tables

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages