利用Sharding-Jdbc進行分庫分表的操作代碼

1. Sharding-Jdbc介紹

https://shardingsphere.apache.org/

  • sharding-jdbc是一個分佈式的關系型數據庫中間件
  • 客戶端代理模式,不需要搭建服務器,隻需要後端數據庫即可,有個IDE就行瞭
  • 定位於輕量級的Java框架,以jar的方式提供服務
  • 可以理解為增強版的jdbc驅動
  • 完全兼容主流的ORM框架

sharding-jdbc提供瞭4種配置

  • Java API
  • yaml
  • properties
  • spring命名空間

與MyCat的區別

  • MyCat是服務端的代理,Sharding-Jdbc是客戶端代理
  • 實際開發中如果企業有DBA建議使用MyCat,都是開發人員建議使用sharding-jdbc
  • MyCat不支持在一個庫內進行水平分表,而sharding-jdbc支持在同一個數據庫中進行水平分表

名詞解釋

  • 邏輯表:物流的合並表
  • 真實表:存放數據的地方
  • 數據節點:存儲數據的MySQL節點
  • 綁定表:相當於MyCat中的子表
  • 廣播表:相當於MyCat中的全局表

2. Sharding-Jdbc引入使用

# 0.首先在兩個MySQL上創建兩個數據:shard_order
# 1.分表給兩個庫創建兩個表order_info_1,order_info_2
CREATE TABLE `order_info_1` (
  `id` int(11) NOT NULL,
  `order_amount` decimal(10,2) DEFAULT NULL,
  `order_status` int(255) DEFAULT NULL,
  `user_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `order_info_2` (
  `id` int(11) NOT NULL,
  `order_amount` decimal(10,2) DEFAULT NULL,
  `order_status` int(255) DEFAULT NULL,
  `user_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
# 2.切分規則,按照id的奇偶數切分到兩個數據庫,在自己的數據庫按照user_id進行表切分

代碼導入POM依賴

 <dependency>
            <groupId>org.apache.shardingsphere</groupId>
            <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
            <version>4.0.0-RC2</version>
        </dependency>

配置properties

# 給兩個數據源命名
spring.shardingsphere.datasource.names=ds0,ds1
# 數據源鏈接ds0要和命名一致
spring.shardingsphere.datasource.ds0.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds0.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds0.jdbcUrl=jdbc:mysql://39.103.163.215:3306/shard_order
spring.shardingsphere.datasource.ds0.username=gavin
spring.shardingsphere.datasource.ds0.password=123456
# 數據源鏈接ds1要和命名一致
spring.shardingsphere.datasource.ds1.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds1.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds1.jdbcUrl=jdbc:mysql://39.101.221.95:3306/shard_order
spring.shardingsphere.datasource.ds1.username=gavin
spring.shardingsphere.datasource.ds1.password=123456
# 具體的分片規則,基於數據節點
spring.shardingsphere.sharding.tables.order_info.actual-data-nodes=ds$->{0..1}.order_info_$->{1..2}
# 分庫的規則
spring.shardingsphere.sharding.tables.order_info.database-strategy.inline.sharding-column=id
spring.shardingsphere.sharding.tables.order_info.database-strategy.inline.algorithm-expression=ds$->{id % 2}
# 分表的規則
spring.shardingsphere.sharding.tables.order_info.table-strategy.inline.sharding-column=user_id
spring.shardingsphere.sharding.tables.order_info.table-strategy.inline.algorithm-expression=order_info_$->{user_id % 2 + 1}
//測試代碼
@SpringBootTest
class ShardingjdbcProjectApplicationTests {
    @Autowired
    JdbcTemplate jdbcTemplate;
    @Test
    void insertTest(){
        String sql = "insert into order_info(id,order_amount,order_status,user_id) values(3,213.88,1,2)";
        int i = jdbcTemplate.update(sql);
        System.out.println("影響行數:"+i);
    }
}

作業:自己練習一下sharding-jdbc的分庫分表

3. 配置廣播表

先在兩個庫上創建廣播表province_info

CREATE TABLE `province_info` (
  `id` int(11) NOT NULL,
  `name` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

在properties裡增加配置

spring.shardingsphere.sharding.broadcast-tables=province_info

測試插入和查詢的代碼

 @Test
    void insertBroadcast(){
        String sql = "insert into province_info(id,name) values(1,'beijing')";
        int i = jdbcTemplate.update(sql);
        System.out.println("******* 影響的結果:"+i);
    }

    @Test
    void selectBroadcast(){
        String sql = "select * from province_info";
        List<Map<String,Object>> result = jdbcTemplate.queryForList(sql);
        for (Map<String,Object> val: result) {
            System.out.println("=========== "+val.get("id")+" ----- "+val.get("name"));

        }
    }

4. 配置綁定表

首先按照order_info的建表順序創建order_item分別在兩個庫上建立order_item_1,order_item_2

​
 @Test
    void insertBroadcast(){
        String sql = "insert into province_info(id,name) values(1,'beijing')";
        int i = jdbcTemplate.update(sql);
        System.out.println("******* 影響的結果:"+i);
    }

    @Test
    void selectBroadcast(){
        String sql = "select * from province_info";
        List<Map<String,Object>> result = jdbcTemplate.queryForList(sql);
        for (Map<String,Object> val: result) {
            System.out.println("=========== "+val.get("id")+" ----- "+val.get("name"));

        }
    }

​

配置綁定表,將兩個表的分表邏輯和order_info保持一致

# 給兩個數據源命名
spring.shardingsphere.datasource.names=ds0,ds1
# 數據源鏈接ds0要和命名一致
spring.shardingsphere.datasource.ds0.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds0.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds0.jdbcUrl=jdbc:mysql://39.103.163.215:3306/shard_order
spring.shardingsphere.datasource.ds0.username=gavin
spring.shardingsphere.datasource.ds0.password=123456
# 數據源鏈接ds1要和命名一致
spring.shardingsphere.datasource.ds1.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds1.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds1.jdbcUrl=jdbc:mysql://39.101.221.95:3306/shard_order
spring.shardingsphere.datasource.ds1.username=gavin
spring.shardingsphere.datasource.ds1.password=123456

# 具體的分片規則,基於數據節點
spring.shardingsphere.sharding.tables.order_info.actual-data-nodes=ds$->{0..1}.order_info_$->{1..2}
# 分庫的規則
spring.shardingsphere.sharding.tables.order_info.database-strategy.inline.sharding-column=id
spring.shardingsphere.sharding.tables.order_info.database-strategy.inline.algorithm-expression=ds$->{id % 2}
# 分表的規則
spring.shardingsphere.sharding.tables.order_info.table-strategy.inline.sharding-column=user_id
spring.shardingsphere.sharding.tables.order_info.table-strategy.inline.algorithm-expression=order_info_$->{user_id % 2 + 1}

# 具體的分片規則,基於數據節點
spring.shardingsphere.sharding.tables.order_item.actual-data-nodes=ds$->{0..1}.order_item_$->{1..2}
# 分庫的規則
spring.shardingsphere.sharding.tables.order_item.database-strategy.inline.sharding-column=order_id
spring.shardingsphere.sharding.tables.order_item.database-strategy.inline.algorithm-expression=ds$->{order_id % 2}
# 分表的規則
spring.shardingsphere.sharding.tables.order_item.table-strategy.inline.sharding-column=user_id
spring.shardingsphere.sharding.tables.order_item.table-strategy.inline.algorithm-expression=order_item_$->{user_id % 2 + 1}

# 綁定表關系
spring.shardingsphere.sharding.binding-tables=order_info,order_item

# 廣播表
spring.shardingsphere.sharding.broadcast-tables=province_info

5. 讀寫分離配置

首先配置properties的數據源,如果有主機配置就必須要有從機配置

# 指定主從的配置節點
spring.shardingsphere.datasource.names=master0,master0slave0,master1,master1slave0
# master0數據源鏈接配置
spring.shardingsphere.datasource.master0.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.master0.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.master0.jdbcUrl=jdbc:mysql://39.103.163.215:3306/shard_order
spring.shardingsphere.datasource.master0.username=gavin
spring.shardingsphere.datasource.master0.password=123456
# master0slave0數據源鏈接配置
spring.shardingsphere.datasource.master0slave0.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.master0slave0.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.master0slave0.jdbcUrl=jdbc:mysql://39.99.212.46:3306/shard_order
spring.shardingsphere.datasource.master0slave0.username=gavin
spring.shardingsphere.datasource.master0slave0.password=123456
# master1數據源鏈接配置
spring.shardingsphere.datasource.master1.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.master1.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.master1.jdbcUrl=jdbc:mysql://39.101.221.95:3306/shard_order
spring.shardingsphere.datasource.master1.username=gavin
spring.shardingsphere.datasource.master1.password=123456
# master1slave0數據源鏈接配置
spring.shardingsphere.datasource.master1slave0.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.master1slave0.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.master1slave0.jdbcUrl=jdbc:mysql://localhost:3306/shard_order
spring.shardingsphere.datasource.master1slave0.username=root
spring.shardingsphere.datasource.master1slave0.password=gavin

# 具體的分片規則,基於數據節點
spring.shardingsphere.sharding.tables.order_info.actual-data-nodes=ds$->{0..1}.order_info_$->{1..2}
# 分庫的規則
spring.shardingsphere.sharding.tables.order_info.database-strategy.inline.sharding-column=id
spring.shardingsphere.sharding.tables.order_info.database-strategy.inline.algorithm-expression=ds$->{id % 2}
# 分表的規則
spring.shardingsphere.sharding.tables.order_info.table-strategy.inline.sharding-column=user_id
spring.shardingsphere.sharding.tables.order_info.table-strategy.inline.algorithm-expression=order_info_$->{user_id % 2 + 1}

# 具體的分片規則,基於數據節點
spring.shardingsphere.sharding.tables.order_item.actual-data-nodes=ds$->{0..1}.order_item_$->{1..2}
# 分庫的規則
spring.shardingsphere.sharding.tables.order_item.database-strategy.inline.sharding-column=order_id
spring.shardingsphere.sharding.tables.order_item.database-strategy.inline.algorithm-expression=ds$->{order_id % 2}
# 分表的規則
spring.shardingsphere.sharding.tables.order_item.table-strategy.inline.sharding-column=user_id
spring.shardingsphere.sharding.tables.order_item.table-strategy.inline.algorithm-expression=order_item_$->{user_id % 2 + 1}

# 綁定表關系
spring.shardingsphere.sharding.binding-tables=order_info,order_item

# 廣播表
spring.shardingsphere.sharding.broadcast-tables=province_info

# 讀寫分離主從關系綁定
spring.shardingsphere.sharding.master-slave-rules.ds0.master-data-source-name=master0
spring.shardingsphere.sharding.master-slave-rules.ds0.slave-data-source-names=master0slave0
spring.shardingsphere.sharding.master-slave-rules.ds0.load-balance-algorithm-type=round_robin

spring.shardingsphere.sharding.master-slave-rules.ds1.master-data-source-name=master1
spring.shardingsphere.sharding.master-slave-rules.ds1.slave-data-source-names=master1slave0
spring.shardingsphere.sharding.master-slave-rules.ds1.load-balance-algorithm-type=random

到此這篇關於Sharding-Jdbc進行分庫分表的文章就介紹到這瞭,更多相關Sharding-Jdbc分庫分表內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: