一、简要说明
版本: 5.2.0,为什么选择这个版本?这个是目前官方提供与SpringBoot结合的最高版本。
主要内容:如何配置,算法介绍, HINT的使用。
GitHubDemo: 下面写的各种配置demo里包含了。 GitHub - ji602089383/ShardingSphereDemo
下面就介绍几款工具。
MYSQL: Fabric
阿里巴巴: TDDL、Cobar、 mycat
百度: Heisenberg
youtube: Vitess
mycat的语雀文章: mycat2 team · 语雀
DBLE:一直再维护的。基于mycat开发的。 0.4 数据拆分简介 · dble manual
Vitess:Vitess | Scalable. Reliable. MySQL-compatible. Cloud-native. Database. 也还行,文档落后的。
Apache ShardingSphere: 概览 :: ShardingSphere
推荐使用的 MyCat, DBLE 和 ShardingSphere
1、MyCat,DBLE是中间件需要独立的服务器去部署,
2、ShardingSphere ShardingSphere这款产品发展了有段时间了,2016年出来的,现在是捐给了apache进行管理。他分为ShardingSphere JDBC 和ShardingSphere Proxy。
ShardingSphere JDBC 是跟随业务系统一起,引入依赖,做好规则配置就可以使用了。简单快速。
ShardingSphere Proxy 跟MyCat一样 独立部署的服务。 在它里面进行配置分库分表的策略,就是代理访问,向mysql, postgresql,oracle 等数据库服务器发送请求。
可以单独使用 ShardingSphere JDBC 和 ShardingSphere Proxy 也可以混合起来用。
本次主要研究的是ShardingSphere JDBC。
二、配置文件
官方文档中介绍了yaml 配置和JavaAPI配置,
目前5.2.xx及以下的版本是有SpringBoot的jar包的。 当然还有SpringBoot一起使用的配置。
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-jdbc-core-spring-boot-starter</artifactId>
<version>5.2.1</version>
</dependency>
1、模式配置
默认是内存, 还有单机,集群。现在大部分都是分布式系统而且有自己的分布式策略,所以这块可以忽略。就用默认的内存模式。
2、数据源配置
每个版本的配置可能不太一样。这个要注意。 JNDI 单机服务自己部署容器的时候用起来很方便,但是现在都是集群化配置了,JNDI使用的较少。
properties:
datasource: &dataSourceType #公共配置,比如数据原来连接池之类的
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.jdbc.Driver
spring:
application:
name: order
shardingsphere:
datasource:
names: ds0-master, ds1-master, ds2-master, ds0-salve, ds1-salve, ds2-salve #真实数据源名称,多个数据源用逗号区分
ds0-master: #对应每个数据源的配置
<<: *dataSourceType #引用公共配置
jdbc-url: jdbc:mysql://localhost:3306/order-0-master
username: jicai
password: 123456
ds0-salve:
<<: *dataSourceType
jdbc-url: jdbc:mysql://localhost:3306/order-0-salve
username: jicai
password: 123456
ds1-master:
<<: *dataSourceType
jdbc-url: jdbc:mysql://localhost:3306/order-1-master
username: jicai
password: 123456
ds1-salve:
<<: *dataSourceType
jdbc-url: jdbc:mysql://localhost:3306/order-1-salve
username: jicai
password: 123456
ds2-master:
<<: *dataSourceType
jdbc-url: jdbc:mysql://localhost:3306/order-2-master
username: jicai
password: 123456
ds2-salve:
<<: *dataSourceType
jdbc-url: jdbc:mysql://localhost:3306/order-2-salve
username: jicai
password: 123456
3、规则配置
读写分离
动态跟静态的。 区别就是谁来区分读写表。配置也比较简单,我是更推荐用静态的,自己掌控读写表。
spring:
shardingsphere:
rules:
readwrite_splitting:
data-sources:
ds0:
static-strategy:
write-data-source-name: ds0-master
read-data-source-names: ds0-salve
loadBalancerName: my-round
ds1:
static-strategy:
write-data-source-name: ds1-master
read-data-source-names: ds1-salve
loadBalancerName: my-round
ds2:
static-strategy:
write-data-source-name: ds2-master
read-data-source-names: ds2-salve
loadBalancerName: my-round
load-balancers:
my-round:
type: RANDOM
特别注意的点:
loadBalancerName的属性值 是算法的名称,在load-balancers中要配置的。
数据分片
spring:
shardingsphere:
sharding:
tables:
order:
actual-data-nodes: ds$->{0..2}.order_$->{0..2}
database-strategy:
standard:
sharding-column: id
sharding-algorithm-name: database-default
table-strategy:
standard:
sharding-column: id
sharding-algorithm-name: table-default
binding-tables:
- order
broadcast-tables:
- config
sharding-algorithms:
database-default:
type: inline
props:
algorithm-expression: ds$->{id % 3}
allow-range-query-with-inline-sharding: true
table-default:
type: inline
props:
algorithm-expression: order_$->{id % 3}
allow-range-query-with-inline-sharding: true
它提供了自动分片 和 标准分片。 上面展示的是标准分片的配置。
自动分片和标准分片的区别再于 它提供的算法不同,但是有些算法又是可以共用的。 在配置文件的key上区别就是
spring:
shardingsphere:
sharding:
tables:
<A表名>:
和
spring:
shardingsphere:
sharding:
auth-tables:
<A表名>:
配置大家看看,也不是很复杂。
建议用标准分片。大部门算法都是标准分片在用,而且自己控制分片会清晰。
!!!注意,如果某表没有配置分库分表策略,那么它会被路由到第一个库中。如果想控制它固定落在一个库中,需要用HINT方式,这个在下面会详细说明用法
4、聊聊算法:
自动分片算法、标准分片算法、复合分片算法和 Hint 分片算法、自定义分片算法
自动分片算法:
取模(MOD,比如要分五张表,那就 id % 5 得到余数就是0 到4)
哈希取模(HASH_MOD,就是把 定义的分片键 进行hash计算 然后再取模)
CosId(COSID_MOD。这里是提供了用CosId的取模算法。 CosId是一个分布式id工具,它本身提供多种生成分布式id的方案。这里是将两个工具结合到一起)
基于分片容量的范围分片算法(VOLUME_RANGE, 会让你设计范围上界和下界,分片容量。它会根据上界和下界生成3个范围,大于上界一个范围,小于下界一个范围,中间一个范围,然后把中间范围除以分片容量得到中间分片数N,那么总共的分片数就是N+2.注意了,如果你设计的分片数不是N+2的话,可能再添加数据时报错。这个要谨慎的计算好再设置。)
基于分片边界的范围分片算法(BOUNDARY_RANGE, 相当于自己做了 分片容量的控制。 会根据sharding-ranges=100,200,300进行逗号分隔得到分片数)
自动时间段分片算法(AUTO_INTERVAL,根据配置的datetime-lower, datetime-upper, sharding-seconds, 跟分片容量算法类似)
标准分片算法:
行表达式分片算法(INLINE, 就是Groovy 的表达式,这个配置是最简单的)
时间范围分片算法(INTERVAL, 跟容量范围算法类似,需要注意时间格式要匹配的上)
基于CosId的固定时间范围算法(COSID_INTERVAL)
基于CosId的雪花id固定时间范围算法(COSID_INTERVAL_SNOWFLAKE)
复合分片算法
复合行表达式分片算法(COMPLEX_INLINE, 就是以多个键进行组合,比如先以id取模,然后以时间范围进行分片)
自定义类分片算法
自定义的算法(CLASS_BASED, 配置标准,复合,固定三种策略,自定义类的类名。)
下面介绍Hint的使用