赞
踩
官方文档:https://shardingsphere.apache.org/document/5.1.0/cn/overview/#shardingsphere-sidecartodo
垂直分库分表
水平分库分表
应用
分库分表问题
pom.xml
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-jdbc-core</artifactId>
<version>${latest.release.version}</version>
</dependency>
库名:course_db,表名:course_1,course_2
# 真实数据源名称(别名),多个数据源用逗号区分 spring.shardingsphere.datasource.names=m1 # 配置数据源具体内容(连接池、驱动、地址、用户名、密码) spring.shardingsphere.datasource.m1.type=com.alibaba.druid.pool.DruidDataSource # mysql连接驱动为8版本以上需要加包名“.cj” spring.shardingsphere.datasource.m1.driver-class-name=com.mysql.cj.jdbc.Driver spring.shardingsphere.datasource.m1.jdbc-url=jdbc:mysql://localhost:3306/course_db?serverTimezone=GMT%2B8 spring.shardingsphere.datasource.m1.username=root spring.shardingsphere.datasource.m1.password=root # 指定course表分布情况,配置表在哪个数据数据库里面,表名都是什么($->{} 是行表达式) spring.shardingsphere.sharding.tables.course.actual-data-nodes=m1.course_$->{1..2} # 指定course表里面主键生成策略 spring.shardingsphere.rules.sharding.tables.course.key-generate-strategy.column= cid spring.shardingsphere.rules.sharding.tables.course.key-generate-strategy.key-generator-name=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} # 开启sql输出日志 spring.shardingsphere.props.sql.show=true
实操注意的问题:创建实体类时,默认一个实体类对应一张表,若要对应两张表,需要在properties文件中添加配置(spring.main.allow-bean-definition-overriding=true
)
数据库:m1,m2
表:course_1,course_2
# 真实数据源名称(别名),多个数据源用逗号区分 spring.shardingsphere.datasource.names=m1,m2 # 一个实体类对应两张表,覆盖 spring.main.allow-bean-definition-overriding=true # 配置第一个数据源具体内容(连接池、驱动、地址、用户名、密码) spring.shardingsphere.datasource.m1.type=com.alibaba.druid.pool.DruidDataSource # mysql连接驱动为8版本以上需要加包名“.cj” spring.shardingsphere.datasource.m1.driver-class-name=com.mysql.cj.jdbc.Driver spring.shardingsphere.datasource.m1.jdbc-url=jdbc:mysql://localhost:3306/edu_db1?serverTimezone=GMT%2B8 spring.shardingsphere.datasource.m1.username=root spring.shardingsphere.datasource.m1.password=root # 配置第二个数据源具体内容(连接池、驱动、地址、用户名、密码) spring.shardingsphere.datasource.m2.type=com.alibaba.druid.pool.DruidDataSource # mysql连接驱动为8版本以上需要加包名“.cj” spring.shardingsphere.datasource.m2.driver-class-name=com.mysql.cj.jdbc.Driver spring.shardingsphere.datasource.m2.jdbc-url=jdbc:mysql://localhost:3306/edu_db2?serverTimezone=GMT%2B8 spring.shardingsphere.datasource.m2.username=root spring.shardingsphere.datasource.m2.password=root # 指定数据库分布情况,数据库里面表的分布情况($->{} 是行表达式) spring.shardingsphere.sharding.tables.course.actual-data-nodes=m$->{1..2}.course_$->{1..2} # 指定数据库分片策略:约定user_id是偶数添加m1,奇数添加m2 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} # 指定course表里面主键生成策略 spring.shardingsphere.rules.sharding.tables.course.key-generate-strategy.column= cid spring.shardingsphere.rules.sharding.tables.course.key-generate-strategy.key-generator-name=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} # 开启sql输出日志 spring.shardingsphere.props.sql.show=true
专库专表:user_db.t_user
# 真实数据源名称(别名),多个数据源用逗号区分 spring.shardingsphere.datasource.names=m0 # 一个实体类对应两张表,覆盖 spring.main.allow-bean-definition-overriding=true # 配置数据源具体内容(连接池、驱动、地址、用户名、密码) spring.shardingsphere.datasource.m1.type=com.alibaba.druid.pool.DruidDataSource # mysql连接驱动为8版本以上需要加包名“.cj” spring.shardingsphere.datasource.m1.driver-class-name=com.mysql.cj.jdbc.Driver spring.shardingsphere.datasource.m1.jdbc-url=jdbc:mysql://localhost:3306/user_db?serverTimezone=GMT%2B8 spring.shardingsphere.datasource.m1.username=root spring.shardingsphere.datasource.m1.password=root # 配置user_db数据库里面t_user专库专表 spring.shardingsphere.sharding.tables.t_user.actual-data-nodes=m$->{0}.t_user # 指定t_user表里面主键生成策略 spring.shardingsphere.rules.sharding.tables.t_user.key-generate-strategy.column=user_id spring.shardingsphere.rules.sharding.tables.t_user.key-generate-strategy.key-generator-name=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 # 开启sql输出日志 spring.shardingsphere.props.sql.show=true
概念
# 配置库(m1,m2,m3)的公共表
spring.shardingsphere.sharding.broadcast-tables=t_udict
# 指定t_user表里面主键生成策略
spring.shardingsphere.rules.sharding.tables.t_udict.key-generate-strategy.column=dictid
spring.shardingsphere.rules.sharding.tables.t_udict.key-generate-strategy.key-generator-name=SNOWFLAKE
概念:当主服务器有写入(insert、update、delete)语句时候,从服务器自动获取;(insert、update、delete)语句操作一台服务器,(select) 操作另一台服务器
原理图:
Sharding-JDBC读写分离
# 真实数据源名称(别名),多个数据源用逗号区分 spring.shardingsphere.datasource.names=m1,s1 # 配置主数据源具体内容(连接池、驱动、地址、用户名、密码) spring.shardingsphere.datasource.m1.type=com.alibaba.druid.pool.DruidDataSource # mysql连接驱动为8版本以上需要加包名“.cj” spring.shardingsphere.datasource.m1.driver-class-name=com.mysql.cj.jdbc.Driver spring.shardingsphere.datasource.m1.jdbc-url=jdbc:mysql://localhost:3306/user_db?serverTimezone=GMT%2B8 spring.shardingsphere.datasource.m1.username=root spring.shardingsphere.datasource.m1.password=root # 配置从数据源具体内容(连接池、驱动、地址、用户名、密码) spring.shardingsphere.datasource.s1.type=com.alibaba.druid.pool.DruidDataSource # mysql连接驱动为8版本以上需要加包名“.cj” spring.shardingsphere.datasource.s1.driver-class-name=com.mysql.cj.jdbc.Driver spring.shardingsphere.datasource.s1.jdbc-url=jdbc:mysql://localhost:3307/user_db?serverTimezone=GMT%2B8 spring.shardingsphere.datasource.s1.username=root spring.shardingsphere.datasource.s1.password=root # 主从库逻辑数据源定义 ds0 为user_db spring.shardingsphere.sharding.master-slave-rules.ds0.master-data-source-name=m1 spring.shardingsphere.sharding.master-slave-rules.ds0.slave-data-source-names=s1 # 配置user_db数据库里面t_user专库专表 spring.shardingsphere.sharding.tables.t_user.actual-data-nodes=ds0.t_user
mysql -P3307 -uroot -p
方式连接 )Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。