每日一水sharding-jdbc

2022-03-20 11:53:09 浏览数 (1)

## 1 分库分表

为什么要分库表呢?

1. 高并发。单库单表没法承受单并发。

2. 数据量大。磁盘空间问题

3. sql执行速度。单表太大

拆分的方式

* 水平拆分

* 大的数据量横向拆分成多个库表

* 垂直拆分

* 相关的业务拆分成独立的库表

## 2 为什么要用shardingjdbc

业界的技术

* tidb类似的数据库

* sharding-jdbc

* mycat proxy 层方案

* TDDL

* cobar proxy 层方案

* atlas proxy 层方案

sharding-jdbc社区活跃,使用方众多

官方地址:https://shardingsphere.apache.org/document/legacy/3.x/document/cn/quick-start/sharding-jdbc-quick-start/

## 3 sharding-jdbc的应用

提供的功能

* 分库分表

* 功能完善:聚合,分组,排序,limit,top查询,连表

* 支持内外连接查询

* 支持灵活的分表策略:等号,between,in多维度支持,自定义分表策略

* 基于hint强制分表路由

* 读写分离

* 柔性事务,TCC型事务

* 分布式主键

* 支持时间序列的id生成器

* 兼容性

* jpa,mybatis,jdbc-template

* 多种配置

* java,spring,yaml

概念

* 逻辑分表LogicTable

* 实际分表ActualTable

* 数据分片DataNode。数据分片的最小单元

* 动态分表。DynamicTable

* BindingTable,分表之间建立绑定关系

* shardingColumn分片字段

* shardingAlgorithm分片算法。支持range,hash,tag

* SQLHint。分片非sql决定的,支持灵活注入字段

基础应用

* 读写分离

* 分表

* 分库分表

读写分离的设置

```

<!-- 读写分离数据源 -->

<rdb:master-slave-data-source id="dataSource" master-data-source-ref="ds_0" slave-data-sources-ref="ds_1"/>

```

分库分表的设置

```

<rdb:strategy id="databaseShardingStrategyHouseLouDong" sharding-columns="city" algorithm-class="SingleKeyDbShardingAlgorithm"/>

<rdb:strategy id="tableShardingStrategyHouseLouDong" sharding-columns="id" algorithm-class="SingleKeyTableShardingAlgorithm"/>

<rdb:data-source id="dataSource">

<rdb:sharding-rule data-sources="ds_0, ds_1">

<rdb:table-rules>

<rdb:table-rule logic-table="user" actual-tables="user_${0..1}" database-strategy="databaseShardingStrategyHouseLouDong" table-strategy="tableShardingStrategyHouseLouDong">

<rdb:generate-key-column column-name="id"/>

</rdb:table-rule>

</rdb:table-rules>

</rdb:sharding-rule>

</rdb:data-source>

```

快速集成

```

<!-- mybatis plus 依赖 -->

<dependency>

<groupId>com.baomidou</groupId>

<artifactId>mybatis-plus-boot-starter</artifactId>

<version>${mybatis-plus-boot-starter.version}</version>

</dependency>

<dependency>

<groupId>com.baomidou</groupId>

<artifactId>mybatis-plus-annotation</artifactId>

<version>${mybatis-plus-annotation.version}</version>

<scope>compile</scope>

</dependency>

<dependency>

<groupId>com.baomidou</groupId>

<artifactId>mybatis-plus-generator</artifactId>

<version>${mybatis-plus-generator.version}</version>

</dependency>

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

<version>${mysql-connector-java.version}</version>

</dependency>

<dependency>

<groupId>com.github.pagehelper</groupId>

<artifactId>pagehelper</artifactId>

<version>${pagehelper.version}</version>

</dependency>

<dependency>

<groupId>org.apache.shardingsphere</groupId>

<artifactId>sharding-jdbc-spring-boot-starter</artifactId>

<version>${sharding-jdbc-spring-boot-starter.version}</version>

</dependency>

<dependency>

<groupId>org.apache.shardingsphere</groupId>

<artifactId>sharding-jdbc-spring-namespace</artifactId>

<version>${sharding-jdbc-spring-boot-starter.version}</version>

</dependency>

```

支持读写分离

```

#数据库

spring.shardingsphere.datasource.names=master,slave1

spring.shardingsphere.datasource.master.type=com.zaxxer.hikari.HikariDataSource

spring.shardingsphere.datasource.master.driver-class-name=com.mysql.cj.jdbc.Driver

spring.shardingsphere.datasource.master.jdbc-url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai&allowMultiQueries=true&allowPublicKeyRetrieval=true

spring.shardingsphere.datasource.master.username=root

spring.shardingsphere.datasource.master.password=123456

spring.shardingsphere.datasource.master.pool-name=HikariPool-1

spring.shardingsphere.datasource.master.minimum-idle=1

spring.shardingsphere.datasource.master.maximum-pool-size=5

spring.shardingsphere.datasource.master.idle-timeout=600000

spring.shardingsphere.datasource.master.max-lifetime=1200000

spring.shardingsphere.datasource.master.connection-timeout=30000

spring.shardingsphere.datasource.master.connection-test-query=SELECT 1

spring.shardingsphere.datasource.slave1.type=com.zaxxer.hikari.HikariDataSource

spring.shardingsphere.datasource.slave1.driver-class-name=com.mysql.cj.jdbc.Driver

spring.shardingsphere.datasource.slave1.jdbc-url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai&allowMultiQueries=true&allowPublicKeyRetrieval=true

spring.shardingsphere.datasource.slave1.username=root

spring.shardingsphere.datasource.slave1.password=123456

spring.shardingsphere.datasource.slave1.pool-name=HikariPool-2

spring.shardingsphere.datasource.slave1.minimum-idle=1

spring.shardingsphere.datasource.slave1.maximum-pool-size=5

spring.shardingsphere.datasource.slave1.idle-timeout=600000

spring.shardingsphere.datasource.slave1.max-lifetime=1200000

spring.shardingsphere.datasource.slave1.connection-timeout=30000

spring.shardingsphere.datasource.slave1.connection-test-query=SELECT 1

spring.shardingsphere.masterslave.load-balance-algorithm-type=round_robin

spring.shardingsphere.sharding.master-slave-rules.master.master-data-source-name=master

spring.shardingsphere.sharding.master-slave-rules.master.slave-data-source-names=slave1

```

0 人点赞