分庫分表——sharding-sphere

sharding-sphere

簡介

Sharding-Sphere是一套開源的分佈式數據庫中間件解決方案組成的生態圈,它由Sharding-JDBC、Sharding-Proxy和Sharding-Sidecar這3款相互獨立的產品組成。他們均提供標準化的數據分片、讀寫分離、柔性事務和數據治理功能,可適用於如Java同構、異構語言、容器、雲原生等各種多樣化的應用場景。

官網

http://shardingjdbc.io/

Github

https://github.com/sharding-sphere

三大核心模塊分別是Sharding-JDBC、Sharding-Proxy和Sharding-Sidecar。

Sharding-JDBC

定位爲輕量級Java框架,在Java的JDBC層提供的額外服務。 它使用客戶端直連數據庫,以jar包形式提供服務,無需額外部署和依賴,可理解爲增強版的JDBC驅動,完全兼容JDBC和各種ORM框架。

image

Sharding-Proxy

定位爲透明化的數據庫代理端,提供封裝了數據庫二進制協議的服務端版本,用於完成對異構語言的支持。 目前先提供MySQL版本,它可以使用任何兼容MySQL協議的訪問客戶端(如:MySQL Command Client, MySQL Workbench等)操作數據,對DBA更加友好。

image

Sharding-Sidecar

定位爲Kubernetes或Mesos的雲原生數據庫代理,以DaemonSet的形式代理所有對數據庫的訪問。 通過無中心、零侵入的方案提供與數據庫交互的的齧合層,即Database Mesh,又可稱數據網格。

image

sharding-sphere-example

在Github上分別有三個項目,分別是sharding-sphere、sharding-sphere-doc和sharding-sphere-example。從字面就可以看出每個項目是做什麼的。

既然是要入門,那就clone下sharding-sphere-example這個項目。

1、克隆項目

在命令行執行git clone https://github.com/sharding-sphere/sharding-sphere-example.git

完成後,就可以看到sharding-sphere-example項目,導入intellij idea中。

2、編譯項目

進入項目根目錄下,編譯項目。

我這邊下載的項目sharding-sphere.version是3.0.0.M2-SNAPSHOT,編譯的時候一直報該版本找不到,無法下載,去中央倉庫也沒有找到。

image

想着可能要本地編譯打包,所以就換成了3.0.0.M1版本,編譯通過。

image

3、配置數據源

因爲是本機測試,所以在本地配置mysql數據庫。

image

4、編寫數據分片代碼

sharding-sphere-example項目中有基於不同場景包括spring-boot、jpa、mybatis的具體分庫分表的實例代碼。

本文主要結合sharding-sphere官方文檔給出的數據分片代碼講解如何實現分庫分表的。

測試類ShardingDataSource(自建測試類,來源http://shardingsphere.io/document/current/cn/manual/sharding-jdbc/usage/sharding/

 


package practice;

import io.shardingsphere.core.api.ShardingDataSourceFactory;

import io.shardingsphere.core.api.config.ShardingRuleConfiguration;

import io.shardingsphere.core.api.config.TableRuleConfiguration;

import io.shardingsphere.core.api.config.strategy.InlineShardingStrategyConfiguration;

import io.shardingsphere.example.jdbc.fixture.DataRepository;

import org.apache.commons.dbcp.BasicDataSource;

import javax.sql.DataSource;

import java.sql.SQLException;

import java.util.HashMap;

import java.util.Map;

import java.util.Properties;

import java.util.concurrent.ConcurrentHashMap;

public class ShardingDataSource {

public static void main(String[] args)throws SQLException {

ShardingDataSource shardingDataSource =new ShardingDataSource();

DataSource dataSource = shardingDataSource.sharding();

new DataRepository(dataSource).demo();

}

public DataSource sharding()throws SQLException {

// 配置真實數據源

       Map dataSourceMap =new HashMap<>();

// 配置第一個數據源

       BasicDataSource dataSource1 =new BasicDataSource();

dataSource1.setDriverClassName("com.mysql.jdbc.Driver");

dataSource1.setUrl("jdbc:mysql://127.0.0.1:3306/ds0");

dataSource1.setUsername("root");

dataSource1.setPassword("root");

dataSourceMap.put("ds0", dataSource1);

// 配置第二個數據源

       BasicDataSource dataSource2 =new BasicDataSource();

dataSource2.setDriverClassName("com.mysql.jdbc.Driver");

dataSource2.setUrl("jdbc:mysql://127.0.0.1:3306/ds1");

dataSource2.setUsername("root");

dataSource2.setPassword("root");

dataSourceMap.put("ds1", dataSource2);

// 配置Order表規則

       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}"));

orderTableRuleConfig.setTableShardingStrategyConfig(new InlineShardingStrategyConfiguration("order_item_id","t_order_item${order_item_id % 2}"));

// 配置分片規則

       ShardingRuleConfiguration shardingRuleConfig =new ShardingRuleConfiguration();

shardingRuleConfig.getTableRuleConfigs().add(orderTableRuleConfig);

// 配置order_item表規則...

       TableRuleConfiguration orderItemTableRuleConfig =new TableRuleConfiguration();

orderItemTableRuleConfig.setLogicTable("t_order_item");

orderItemTableRuleConfig.setActualDataNodes("ds${0..1}.t_order_item${0..1}");

shardingRuleConfig.getTableRuleConfigs().add(orderItemTableRuleConfig);

// 獲取數據源對象

       return ShardingDataSourceFactory.createDataSource(dataSourceMap, shardingRuleConfig,new ConcurrentHashMap(),new Properties());

}

}

注意

1、代碼中類似"ds{0..1}.t_order{0..1}"成爲行表達式,形如"{ expression }或->{ expression }"。該表達式可用於配置數據節點和配置分片算法。

${begin..end}表示範圍區間,即表示從begin到end個

${[unit1, unit2, unit_x]}表示枚舉值

2、orderTableRuleConfig.setActualDataNodes("ds{0..1}.t_order{0..1}");

這裏表示的是使用行表達式配置數據節點即數據庫分別是ds0、ds1,表分別是t_order0、t_order1。

該表達的等價組合是:ds0.t_order0, ds0.t_order1, ds1.t_order0, ds1.t_order1。

3、orderTableRuleConfig.setTableShardingStrategyConfig(new InlineShardingStrategyConfiguration("order_id", "t_order${order_id % 2}"));

這裏表示的是使用行表達式配置分片算法。該行表示針對t_order表中的元素按照order_id模2將不同的元素放進不同的表中。

比如order_id=5,5%2=1,則放入t_order1中

order_id=6, 6%2=0, 則放入t_order0中

4、除此以外還要一些類似"邏輯表"這樣的概念,可以到官方文檔自行查詢。

工具類DataRespository(該類來源sharding-sphere-example項目)

 


/*

* Copyright 2016-2018 shardingsphere.io.

*

* Licensed under the Apache License, Version 2.0 (the "License");

* you may not use this file except in compliance with the License.

* You may obtain a copy of the License at

*

*     http://www.apache.org/licenses/LICENSE-2.0

*

* Unless required by applicable law or agreed to in writing, software

* distributed under the License is distributed on an "AS IS" BASIS,

* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

* See the License for the specific language governing permissions and

* limitations under the License.

*

*/

package io.shardingsphere.example.jdbc.fixture;

import io.shardingsphere.core.api.HintManager;

import javax.sql.DataSource;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

public class DataRepository {

private final DataSourcedataSource;

public DataRepository(final DataSource dataSource) {

this.dataSource = dataSource;

}

public void demo()throws SQLException {

createTable();

insertData();

System.out.println("1.Query with EQUAL--------------");

queryWithEqual();

System.out.println("2.Query with IN--------------");

queryWithIn();

System.out.println("3.Query with Hint--------------");

queryWithHint();

System.out.println("4.Drop tables--------------");

dropTable();

System.out.println("5.All done-----------");

}

private void createTable()throws SQLException {

execute("CREATE TABLE IF NOT EXISTS t_order (order_id BIGINT NOT NULL AUTO_INCREMENT, user_id INT NOT NULL, status VARCHAR(50), PRIMARY KEY (order_id))");

execute("CREATE TABLE IF NOT EXISTS t_order_item (order_item_id BIGINT NOT NULL AUTO_INCREMENT, order_id BIGINT NOT NULL, user_id INT NOT NULL, PRIMARY KEY (order_item_id))");

}

private void insertData()throws SQLException {

for (int i =1; i <10; i++) {

long orderId = insertAndGetGeneratedKey("INSERT INTO t_order (user_id, status) VALUES (10, 'INIT')");

execute(String.format("INSERT INTO t_order_item (order_id, user_id) VALUES (%d, 10)", orderId));

orderId = insertAndGetGeneratedKey("INSERT INTO t_order (user_id, status) VALUES (11, 'INIT')");

execute(String.format("INSERT INTO t_order_item (order_id, user_id) VALUES (%d, 11)", orderId));

}

}

private long insertAndGetGeneratedKey(final String sql)throws SQLException {

long result = -1;

try (

Connection connection =dataSource.getConnection();

Statement statement = connection.createStatement()) {

statement.executeUpdate(sql, Statement.RETURN_GENERATED_KEYS);

try (ResultSet resultSet = statement.getGeneratedKeys()) {

if (resultSet.next()) {

result = resultSet.getLong(1);

}

}

}

return result;

}

private void queryWithEqual()throws SQLException {

String sql ="SELECT i.* FROM t_order o JOIN t_order_item i ON o.order_id=i.order_id WHERE o.user_id=?";

try (

Connection connection =dataSource.getConnection();

PreparedStatement preparedStatement = connection.prepareStatement(sql)) {

preparedStatement.setInt(1,10);

printQuery(preparedStatement);

}

}

private void queryWithIn()throws SQLException {

String sql ="SELECT i.* FROM t_order o JOIN t_order_item i ON o.order_id=i.order_id WHERE o.user_id IN (?, ?)";

try (

Connection connection =dataSource.getConnection();

PreparedStatement preparedStatement = connection.prepareStatement(sql)) {

preparedStatement.setInt(1,10);

preparedStatement.setInt(2,11);

printQuery(preparedStatement);

}

}

private void queryWithHint()throws SQLException {

String sql ="SELECT i.* FROM t_order o JOIN t_order_item i ON o.order_id=i.order_id";

try (

HintManager hintManager = HintManager.getInstance();

Connection connection =dataSource.getConnection();

PreparedStatement preparedStatement = connection.prepareStatement(sql)) {

hintManager.addDatabaseShardingValue("t_order","user_id",11);

printQuery(preparedStatement);

}

}

private void printQuery(final PreparedStatement preparedStatement)throws SQLException {

try (ResultSet resultSet = preparedStatement.executeQuery()) {

while (resultSet.next()) {

System.out.print("order_item_id:" + resultSet.getLong(1) +", ");

System.out.print("order_id:" + resultSet.getLong(2) +", ");

System.out.print("user_id:" + resultSet.getInt(3));

System.out.println();

}

}

}

private void dropTable()throws SQLException {

execute("DROP TABLE t_order_item");

execute("DROP TABLE t_order");

}

private void execute(final String sql)throws SQLException {

try (

Connection connection =dataSource.getConnection();

Statement statement = connection.createStatement()) {

statement.execute(sql);

}

}

}

注意

1、createTable

該方法會根據配置的數據節點表達式創建分表。這裏分別創建t_order和t_order_item兩張邏輯表。

2、insertData

該方法同樣根據配置的數據分片表達書創建數據

3、queryWithEqual等方法

這些方法是不同的查詢場景,有精確查詢也有範圍查詢

4、queryWithHint

該方法比較特殊。

通過解析SQL語句提取分片鍵列與值並進行分片是Sharding-Sphere對SQL零侵入的實現方式。若SQL語句中沒有分片條件,則無法進行分片,需要全路由。

好比queryWithHint這個方法中的"String sql = "SELECT i.* FROM t_order o JOIN t_order_item i ON o.order_id=i.order_id";"就沒有包含路由信息,即where

條件語句中沒有order_id和user_id的信息。

所以該方法中通過強制指定路由信息進行路由。"hintManager.addDatabaseShardingValue("t_order", "user_id", 11);"這裏執行user_id爲11的條件,通過這個條件也可以推測出是隻會路由到ds1庫中(11%2=1)。

5、dropTable

該方法用於清理現場,將所有表和表數據清除。

5、執行結果

執行完代碼,控制檯打印

 


1.Query with EQUAL--------------

2.Query with IN--------------

3.Query with Hint--------------

4.Drop tables--------------

5.All done-----------

執行代碼前,只有兩個數據庫ds0,ds1,執行代碼後得到結果如下圖所示

image

小結

sharding-sphere是一天非常強大的分佈式數據庫中間件解決方法。

有簡單易懂的行表達式用於配置數據節點和數據分片算法。

有自己的諸多大殺器,比如強制路由等。

官方文檔齊全,實例代碼項目case較全,能夠在較短時間完成分庫分表。

本篇通過一個簡單的demo代碼,大致瞭解了sharding-sphere(主要是sharding-jdbc)的基本玩法,後續有時間可以學習下底層的設計和實現原理。


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章