README.md 10.4 KB
Newer Older
T
terrymanu 已提交
1
# [Sharding-JDBC - Distributed database middleware](http://shardingjdbc.io/)
T
terrymanu 已提交
2

T
terrymanu 已提交
3 4
[![License](https://img.shields.io/badge/license-Apache%202-4EB1BA.svg)](https://www.apache.org/licenses/LICENSE-2.0.html)

T
terrymanu 已提交
5
[![Maven Status](https://maven-badges.herokuapp.com/maven-central/io.shardingjdbc/sharding-jdbc/badge.svg)](https://maven-badges.herokuapp.com/maven-central/io.shardingjdbc/sharding-jdbc)
T
terrymanu 已提交
6
[![GitHub release](https://img.shields.io/github/release/shardingjdbc/sharding-jdbc.svg)](https://github.com/shardingjdbc/sharding-jdbc/releases)
T
terrymanu 已提交
7 8
[![Download](https://img.shields.io/badge/release-download-orange.svg)](https://github.com/shardingjdbc/sharding-jdbc-doc/raw/master/dist/sharding-jdbc-server-2.1.0-SNAPSHOT-assembly.tar.gz)

T
terrymanu 已提交
9
[![Build Status](https://secure.travis-ci.org/shardingjdbc/sharding-jdbc.png?branch=master)](https://travis-ci.org/shardingjdbc/sharding-jdbc)
T
terrymanu 已提交
10
[![Coverage Status](https://codecov.io/github/shardingjdbc/sharding-jdbc/coverage.svg?branch=master)](https://codecov.io/github/shardingjdbc/sharding-jdbc?branch=master)
H
haocao 已提交
11
[![Gitter](https://badges.gitter.im/Sharding-JDBC/shardingjdbc.svg)](https://gitter.im/Sharding-JDBC/shardingjdbc?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
T
terrymanu 已提交
12 13
[![OpenTracing-1.0 Badge](https://img.shields.io/badge/OpenTracing--1.0-enabled-blue.svg)](http://opentracing.io)
[![Skywalking Tracing](https://img.shields.io/badge/Skywalking%20Tracing-enable-brightgreen.svg)](https://github.com/OpenSkywalking/skywalking)
T
terrymanu 已提交
14

T
terrymanu 已提交
15
# Overview
T
terrymanu 已提交
16

T
terrymanu 已提交
17
Sharding-JDBC: A data sharding, read-write splitting, BASE transaction and database orchestration middleware. It provides maximum compatibilities for applications by JDBC and MySQL protocol.
T
terrymanu 已提交
18

T
terrymanu 已提交
19 20
# Document

T
terrymanu 已提交
21
[![EN doc](https://img.shields.io/badge/document-English-blue.svg)](http://shardingjdbc.io/docs_en/00-overview/)
T
terrymanu 已提交
22
[![Roadmap](https://img.shields.io/badge/roadmap-English-blue.svg)](ROADMAP.md)
T
terrymanu 已提交
23

T
terrymanu 已提交
24 25
[![CN doc](https://img.shields.io/badge/文档-中文版-blue.svg)](http://shardingjdbc.io/docs_cn/00-overview/)

T
terrymanu 已提交
26
# Features
T
terrymanu 已提交
27

T
terrymanu 已提交
28 29 30 31
## 1. Data sharding
* Both databases and tables sharding supported.
* Standard aggregation functions, GROUP BY, ORDER BY, LIMIT and JOIN DQL supported.
* Standard DML, DDL, TCL and database administrator command supported.
T
terrymanu 已提交
32
* Sharding operator `=`, `BETWEEN` and `IN` supported.
T
terrymanu 已提交
33 34 35
* Sharding algorithm customization and inline expression supported.
* Route by hint supported.
* Distributed sequence supported.
T
terrymanu 已提交
36

T
terrymanu 已提交
37
## 2. Read-write splitting
T
terrymanu 已提交
38 39
* Multiple slaves replica supported. 
* Data consistency guarantee in same thread supported.
T
terrymanu 已提交
40
* Mix read-write splitting and data sharding supported.
T
terrymanu 已提交
41
* Route by hint supported.
T
terrymanu 已提交
42

T
terrymanu 已提交
43
## 3. BASE Transaction
T
terrymanu 已提交
44
* Best efforts delivery transaction supported.
T
terrymanu 已提交
45
* Try confirm cancel transaction (TBD).
T
terrymanu 已提交
46

T
terrymanu 已提交
47 48
## 4. Orchestration
* Configuration center supported, can refresh dynamically.
T
terrymanu 已提交
49
* Circuit breaker supported.
T
terrymanu 已提交
50 51 52 53 54 55 56
* Open tracing supported.

# Architecture

## Sharding-JDBC-Driver

Use JDBC connect databases without redirect cost for java application, best performance for production.
T
terrymanu 已提交
57

wu-sheng's avatar
wu-sheng 已提交
58 59 60
* ORM compatible. JPA, Hibernate, Mybatis, Spring JDBC Template or JDBC supported.
* Connection-pool compatible. DBCP, C3P0, BoneCP, Druid supported.
* Multi SQL-based databases compatible. Any Database supported theoretically. Support MySQL, Oracle, SQLServer and PostgreSQL right now.
T
terrymanu 已提交
61

T
terrymanu 已提交
62
![Sharding-JDBC-Driver Architecture](http://ovfotjrsi.bkt.clouddn.com/driver_architecture_en.png)
T
terrymanu 已提交
63

T
terrymanu 已提交
64 65
## Sharding-JDBC-Server

wu-sheng's avatar
wu-sheng 已提交
66
Database router. Deploy as a stateless server, support MySQL protocol for now.
T
terrymanu 已提交
67 68

* Use standard MySQL protocol, application do not care about whether proxy or real MySQL.
wu-sheng's avatar
wu-sheng 已提交
69
* Any MySQL command line and UI workbench supported in theoretically. (TODO: which tools are tested, at least one version of that tool.)
T
terrymanu 已提交
70 71

![Sharding-JDBC-Server Architecture](http://ovfotjrsi.bkt.clouddn.com/server_architecture_en.png)
T
terrymanu 已提交
72

T
terrymanu 已提交
73 74 75 76 77 78 79 80 81
## Sharding-JDBC-Sidecar(TBD)

Use sidecar to connect databases, best for Kubernetes or Mesos together.

![Sharding-JDBC-Sidecar Architecture](http://ovfotjrsi.bkt.clouddn.com/sidecar_architecture_en.png)

# Quick Start

## Sharding-JDBC-Driver
T
terrymanu 已提交
82

T
terrymanu 已提交
83
### Add maven dependency
T
terrymanu 已提交
84

T
terrymanu 已提交
85
```xml
T
terrymanu 已提交
86
<!-- import sharding-jdbc core -->
T
terrymanu 已提交
87
<dependency>
T
terrymanu 已提交
88
    <groupId>io.shardingjdbc</groupId>
T
terrymanu 已提交
89
    <artifactId>sharding-jdbc-core</artifactId>
H
haocao 已提交
90
    <version>${latest.release.version}</version>
T
terrymanu 已提交
91 92
</dependency>

T
terrymanu 已提交
93
<!-- import other module if need -->
T
terrymanu 已提交
94
```
T
terrymanu 已提交
95

T
terrymanu 已提交
96
### Rule configuration
T
terrymanu 已提交
97

T
terrymanu 已提交
98
```java
T
terrymanu 已提交
99 100 101
    Map<String, DataSource> dataSourceMap = new HashMap<>();
    
    BasicDataSource dataSource1 = new BasicDataSource();
C
Chris Cao 已提交
102
    dataSource1.setDriverClassName("com.mysql.jdbc.Driver");
T
terrymanu 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
    dataSource1.setUrl("jdbc:mysql://localhost:3306/ds_0");
    dataSource1.setUsername("root");
    dataSource1.setPassword("");
    dataSourceMap.put("ds_0", dataSource1);
    
    BasicDataSource dataSource2 = new BasicDataSource();
    dataSource2.setDriverClassName("com.mysql.jdbc.Driver");
    dataSource2.setUrl("jdbc:mysql://localhost:3306/ds_1");
    dataSource2.setUsername("root");
    dataSource2.setPassword("");
    dataSourceMap.put("ds_1", dataSource2);
    
    TableRuleConfiguration orderTableRuleConfig = new TableRuleConfiguration();
    orderTableRuleConfig.setLogicTable("t_order");
    orderTableRuleConfig.setActualDataNodes("ds_${0..1}.t_order_${[0, 1]}");
    
    orderTableRuleConfig.setDatabaseShardingStrategyConfig(new InlineShardingStrategyConfiguration("user_id", "ds_${user_id % 2}"));
    orderTableRuleConfig.setTableShardingStrategyConfig(new InlineShardingStrategyConfiguration("order_id", "t_order_${order_id % 2}"));
    
    ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
    shardingRuleConfig.getTableRuleConfigs().add(orderTableRuleConfig);
    
    // config order_item table rule...
    
    DataSource dataSource = ShardingDataSourceFactory.createDataSource(dataSourceMap, shardingRuleConfig);
```

Or use yaml to configure:

```yaml
dataSources:
  ds_0: !!org.apache.commons.dbcp.BasicDataSource
    driverClassName: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/ds_0
    username: root
    password: 
  ds_1: !!org.apache.commons.dbcp.BasicDataSource
    driverClassName: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/ds_1
    username: root
    password: 

H
haocao 已提交
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
shardingRule:
  tables:
    t_order: 
      actualDataNodes: ds_${0..1}.t_order_${0..1}
      databaseStrategy: 
        inline:
          shardingColumn: user_id
          algorithmExpression: ds_${user_id % 2}
      tableStrategy: 
        inline:
          shardingColumn: order_id
          algorithmExpression: t_order_${order_id % 2}
    t_order_item: 
      actualDataNodes: ds_${0..1}.t_order_item_${0..1}
      databaseStrategy: 
        inline:
          shardingColumn: user_id
          algorithmExpression: ds_${user_id % 2}
      tableStrategy: 
        inline:
          shardingColumn: order_id
          algorithmExpression: t_order_item_${order_id % 2}  
T
terrymanu 已提交
167 168 169 170
```

```java
    DataSource dataSource = ShardingDataSourceFactory.createDataSource(yamlFile);
T
terrymanu 已提交
171
```
T
terrymanu 已提交
172

T
terrymanu 已提交
173
### Use raw JDBC API
T
terrymanu 已提交
174

T
terrymanu 已提交
175
```java
T
terrymanu 已提交
176
DataSource dataSource = ShardingDataSourceFactory.createDataSource(dataSourceMap, shardingRuleConfig);
T
terrymanu 已提交
177 178 179
String sql = "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 (
        Connection conn = dataSource.getConnection();
T
terrymanu 已提交
180 181 182 183
        PreparedStatement preparedStatement = conn.prepareStatement(sql)) {
    preparedStatement.setInt(1, 10);
    preparedStatement.setInt(2, 1001);
    try (ResultSet rs = preparedStatement.executeQuery()) {
T
terrymanu 已提交
184 185 186 187 188 189
        while(rs.next()) {
            System.out.println(rs.getInt(1));
            System.out.println(rs.getInt(2));
        }
    }
}
T
terrymanu 已提交
190
```
H
haocao 已提交
191

T
terrymanu 已提交
192
### Use spring namespace
T
terrymanu 已提交
193

T
terrymanu 已提交
194
```xml
H
haocao 已提交
195 196 197 198
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
T
terrymanu 已提交
199
    xmlns:sharding="http://shardingjdbc.io/schema/shardingjdbc/sharding" 
H
haocao 已提交
200 201 202 203
    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 
T
terrymanu 已提交
204 205
                        http://shardingjdbc.io/schema/shardingjdbc/sharding 
                        http://shardingjdbc.io/schema/shardingjdbc/sharding/sharding.xsd 
H
haocao 已提交
206
                        ">
T
terrymanu 已提交
207
    <context:property-placeholder location="classpath:conf/conf.properties" ignore-unresolvable="true" />
H
haocao 已提交
208
    
T
terrymanu 已提交
209 210 211 212 213
    <bean id="ds_0" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/ds_0" />
        <property name="username" value="root" />
        <property name="password" value="" />
H
haocao 已提交
214
    </bean>
T
terrymanu 已提交
215 216 217 218 219
    <bean id="ds_1" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/ds_1" />
        <property name="username" value="root" />
        <property name="password" value="" />
H
haocao 已提交
220
    </bean>
T
terrymanu 已提交
221 222 223 224 225 226 227 228 229 230 231 232 233
    
    <sharding:inline-strategy id="databaseStrategy" sharding-column="user_id" algorithm-expression="ds_${user_id % 2}" />
    <sharding:inline-strategy id="orderTableStrategy" sharding-column="order_id" algorithm-expression="t_order_${order_id % 2}" />
    <sharding:inline-strategy id="orderItemTableStrategy" sharding-column="order_id" algorithm-expression="t_order_item_${order_id % 2}" />
    
    <sharding:data-source id="shardingDataSource">
        <sharding:sharding-rule data-source-names="ds_0,ds_1">
            <sharding:table-rules>
                <sharding:table-rule logic-table="t_order" actual-data-nodes="ds_${0..1}.t_order_${0..1}" database-strategy-ref="databaseStrategy" table-strategy-ref="orderTableStrategy" />
                <sharding:table-rule logic-table="t_order_item" actual-data-nodes="ds_${0..1}.t_order_item_${0..1}" database-strategy-ref="databaseStrategy" table-strategy-ref="orderItemTableStrategy" />
            </sharding:table-rules>
        </sharding:sharding-rule>
    </sharding:data-source>
H
haocao 已提交
234
</beans>
T
terrymanu 已提交
235
```
T
terrymanu 已提交
236 237 238 239 240

## Sharding-JDBC-Server

### Rule configuration

T
terrymanu 已提交
241
Edit `${sharding-jdbc-server}\conf\sharding-config.yaml`. Same format with Sharding-JDBC-Driver's `yaml` configuration. 
T
terrymanu 已提交
242 243 244 245

### Start server

``` shell
T
terrymanu 已提交
246
${sharding-jdbc-server}\bin\start.sh ${port}
T
terrymanu 已提交
247
```