一、sharding-JDBC
是轻量级的Java框架,增强版的JDBC驱动,很好的解决了分库分表的问题。
1. 分库分表的方式:
- 水平分库:将原数据库的数据分为两部分,存放到其他数据库中。
- 垂直分库:将原数据库的字段集合分为两部分,存放到其他数据库中。
- 水平分表:将原表的数据分为两部分,存放到两张结构相同的数据库表中。
- 垂直分表:将原表的字段集合分为两部分,存放到两张不同的表中。
2. springBoot整合 sharding-JDBC
首先引入sharding-JDBC的pom
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>4.0.0-RC1</version>
</dependency>
application.properties的配置文件
# 配置数据源,给数据源起别名
spring.shardingsphere.datasource.names=m1
# 一个实体类对应两张表,覆盖
spring.main.allow-bean-definition-overriding=true
# 配置数据源的具体内容,包含连接池,驱动,地址,用户名,密码
spring.shardingsphere.datasource.m1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.m1.url=jdbc:mysql://localhost:3306/course_db_1?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.m1.username=root
spring.shardingsphere.datasource.m1.password=root
# 打开 sql 输出日志
spring.shardingsphere.props.sql.show=true
配置水平分表:
# 指定course表分布的情况,配置表在哪个数据库里,表的名称都是什么 m1.course_1,m1.course_2
spring.shardingsphere.sharding.tables.course.actual-data-nodes=m$->{1}.course_$->{1..2}
# 指定 course 表里面主键 cid 的生成策略 SNOWFLAKE
spring.shardingsphere.sharding.tables.course.key-generator.column=cid
spring.shardingsphere.sharding.tables.course.key-generator.type=SNOWFLAKE
# 配置分表策略 这里是 cid 值偶数添加到 course_1 表,如果 cid 是奇数添加到 course_2 表
spring.shardingsphere.sharding.tables.course.table-strategy.inline.sharding-column=cid
spring.shardingsphere.sharding.tables.course.table-strategy.inline.algorithm-expression=course_$->{cid % 2 + 1}
配置水平分库:
# 配置数据水平分库策略 指定按照哪个字段进行分库 规则是什么
spring.shardingsphere.sharding.tables.course.database-strategy.inline.sharding-column=user_id
spring.shardingsphere.sharding.tables.course.database-strategy.inline.algorithm-expression=m$->{user_id % 2 +1}
还有一种方式进行设置水平分库,但是不推荐。
# default-database-strategy是指定所有表都按照这个规则做处理,所以不推荐
spring.shardingsphere.sharding.default-database-strategy.inline.sharding-column=userId
spring.shardingsphere.sharding.default-database-strategy.inline.algorithm-expression=m$->{user % 2 +1}
配置公共表:会使每个数据源的数据库中都会创建 t_udict 表。增加一个条数据,所有库中的表都会增加一条数据,修改同理。
# 配置公共表 也需要配置Id
spring.shardingsphere.sharding.broadcast-tables=t_udict
spring.shardingsphere.sharding.tables.t_udict.key-generator.column=dictid
spring.shardingsphere.sharding.tables.t_udict.key-generator.type=SNOWFLAKE
tips:t_udict 需要指定在entity上通过mybtais-plus相关注解进行指定表名。
@Data
@TableName("t_udict")
public class Dict {
private Long dictId;
private String ustatus;
private String uvalue;
}
配置专库转表:
# 指定数据的分布,这里就直接写死
spring.shardingsphere.sharding.tables.t_user.actual-data-nodes=m0.t_user
# 声明主键以及生成方式
spring.shardingsphere.sharding.tables.t_user.key-generator.column=user_id
spring.shardingsphere.sharding.tables.t_user.key-generator.type=SNOWFLAKE
# 分表策略 直接写死
spring.shardingsphere.sharding.tables.t_user.table-strategy.inline.sharding-column=user_id
spring.shardingsphere.sharding.tables.t_user.table-strategy.inline.algorithm-expression=t_user
配置读写分离:
spring.shardingsphere.datasource.names=m0,s0
spring.main.allow-bean-definition-overriding=true
# 配置主从数据源的时候一定要注意是同一张表,这里省略
# 主库从库逻辑数据源定义 ds0 为 user_db
spring.shardingsphere.sharding.master-slave-rules.ds0.master-data-source-name=m0
spring.shardingsphere.sharding.master-slave-rules.ds0.slave-data-source-names=s0
# 配置user_db数据库里面t_user 专库专表
#spring.shardingsphere.sharding.tables.t_user.actual-data-nodes=m0.t_user
# t_user 分表策略,固定分配至 ds0 的 t_user 真实表,这里需要用ds0,不能继续用m0
spring.shardingsphere.sharding.tables.t_user.actual-data-nodes=ds0.t_user
二、shrading-Proxy(4.1.1)
一个透明的代理,可以通过cmd等其他中间件直接访问。
首先需要先下载shrading-Proxy,下载完成后直接解压。我们需要更改相关的配置文件,就能顺利启动。
首先需要将 mysql-connector.jar 粘贴到 lib目录下。如果要启动其他类型的数据库,也需要将对应的jar包放到 lib 目录下。
然后修改lib目录下的部分文件为 .jar 结尾 下载的时候有问题。
修改server.yml 文件
authentication:
users:
root:
password: root # 这里设置密码,这里并不是mysql的密码
sharding:
password: sharding
authorizedSchemas: sharding_db # 这里是数据库的名称,虚拟的一层
props:
max.connections.size.per.query: 1
acceptor.size: 16 # The default value is available processors count * 2.
executor.size: 16 # Infinite by default.
proxy.frontend.flush.threshold: 128 # The default value is 128.
# LOCAL: Proxy will run with LOCAL transaction.
# XA: Proxy will run with XA transaction.
# BASE: Proxy will run with B.A.S.E transaction.
proxy.transaction.type: LOCAL
proxy.opentracing.enabled: false
proxy.hint.enabled: false
query.with.cipher.column: true
sql.show: false
allow.range.query.with.inline.sharding: false
修改config-sharding.yml 文件
schemaName: sharding_db # 这里需要和server.yml 的配置对应上
dataSources: # 定义数据源
ds_0:
url: jdbc:mysql://127.0.0.1:3306/course_db_1?serverTimezone=UTC&useSSL=false
username: root
password: root
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
ds_1:
url: jdbc:mysql://127.0.0.1:3306/course_db_2?serverTimezone=UTC&useSSL=false
username: root
password: root
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
shardingRule:
tables:
course:
# 定义数据的分布情况
actualDataNodes: ds_${0..1}.course_${1..2}
tableStrategy:
inline:
shardingColumn: cid # 声明按照哪个字段进行划分
algorithmExpression: course_${cid % 2 +1} # 定义如何区分
keyGenerator: # 声明主键
type: SNOWFLAKE
column: cid
bindingTables:
- course # 绑定的表,这里多个用逗号分隔
defaultDatabaseStrategy: # 分库策略
inline:
shardingColumn: user_id
algorithmExpression: ds_${user_id % 2 + 1}
defaultTableStrategy:
none:
双击start.bat进行启动。看到active就为成功。