Mysql分库分表(Sharding JDBC)
一、Sharding JDBC实现分库分表
对于Sharding JDBC分库分表,配置残烛可以分为几大块:
数据源配置:
配置参数:spring.shardingsphere.datasource.names
说明:如果用到分表,则需要配置多个数据源,多数据源之间用逗号分隔
数据源连接信息配置:
配置参数:spring.shardingsphere.datasource.dbname.*
说明:其中dbname就是上面配置的数据源名称,后面需要配置数据库类型、数据库驱动、地址、用户名密码等信息
表的分配策略:
配置参数:spring.shardingsphere.sharding.tables.tablename.actual-data-nodes
说明:其中tablename就是我们实际要用来分库分表的表名;该配置用来配置该表分几个库几张表:
如果只分库,配置样例为:ds1.order_info_$->{1..4},这就表明,对于order_info这张表,都存在ds1的这个数据库中,而表则分4张表,分别为order_info_1到order_info_4;
如果只分库,配置样例为:ds$->{1..2}.order_info_new,这就表明,对于order_info_new这张表,会根据分库策略将其存入ds1和ds2两个数据库中,但是不分表,都是存在库中的order_info_new表中;
既分库又分表:配置样例为:ds$->{1..2}.order_info_$->{1..4},这就表明,对于order_info这张表,会根据分库策略将其存入ds1和ds2两个数据库中,同时也会更具分表策略,将数据存入order_info_1到order_info_4中;
设置主键信息:
配置参数:spring.shardingsphere.sharding.tables.order_info.key-generator.*
分表策略配置:
配置参数:spring.shardingsphere.sharding.tables.tablename.table-strategy.inline.sharding-column
说明:tablename表的分表策略使用哪一列来分
配置参数:spring.shardingsphere.sharding.tables.tablename.table-strategy.inline.algorithm-expression
说明:tablename表的分表策略,例如value为order_info_$->{order_id % 4 + 1},说明根据order_id的值与4取余加1的表,例如order_id为5,那么数据就会被存入order_info_2表中。
分库配置:
配置参数:spring.shardingsphere.sharding.tables.tablename.database-strategy.inline.sharding-column
说明:tablename表的分库策略使用哪一列来分
配置参数:spring.shardingsphere.sharding.tables.tablename.database-strategy.inline.algorithm-expression=
说明:tablename表的分库策略,例如value为ds$->{vender_id % 2 + 1},说明根据vender_id的值与2取余加1的库,例如vender_id为5,那么数据就会被存入ds2库中。
(一)分表
1、引入依赖
<!-- Druid连接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.20</version> </dependency> <!-- Mysql驱动依赖 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- MybatisPlus --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.0.5</version> </dependency> <!-- Sharding-JDBC --> <dependency> <groupId>org.apache.shardingsphere</groupId> <artifactId>sharding-jdbc-spring-boot-starter</artifactId> <version>4.0.0-RC2</version> </dependency>
2、创建表
在不同的两个库中(192.168.1.104和192.168.1.106),分别创建订单表(分4张表)
create table order_info_1(id bigint(11) PRIMARY KEY NOT NULL auto_increment,order_id bigint(11),vender_id bigint(11)); create table order_info_2(id bigint(11) PRIMARY KEY NOT NULL auto_increment,order_id bigint(11),vender_id bigint(11)); create table order_info_3(id bigint(11) PRIMARY KEY NOT NULL auto_increment,order_id bigint(11),vender_id bigint(11)); create table order_info_4(id bigint(11) PRIMARY KEY NOT NULL auto_increment,order_id bigint(11),vender_id bigint(11));
3、配置分表
分表主要就是配置数据源信息,分表策略(分几张表,从几到几),分表列(根据那一列进行分表),分片策略(根据什么样的策略进行分表,hash、取模等)
spring.shardingsphere.datasource.names=lcldata104 # 配置数据源具体内容————————包含 连接池, 驱动, 地址, 用户名, 密码 # 由于上面配置数据源只有lcldata104因此下面只配置lcldata104.type,lcldata104.driver-class-name,lcldata104.url,lcldata104.username,lcldata104.password spring.shardingsphere.datasource.lcldata104.type=com.alibaba.druid.pool.DruidDataSource spring.shardingsphere.datasource.lcldata104.driver-class-name=com.mysql.cj.jdbc.Driver spring.shardingsphere.datasource.lcldata104.url=jdbc:mysql://192.168.1.104:3306/lcltest?characterEncoding=utf-8&useUnicode=true&useSSL=false&serverTimezone=UTC spring.shardingsphere.datasource.lcldata104.username=root spring.shardingsphere.datasource.lcldata104.password=root # 配置表的分布,表的策略 spring.shardingsphere.sharding.tables.order_info.actual-data-nodes=lcldata104.order_info_$->{1..4} # 指定订单表 根据订单号生成策略为 SNOWFLAKE spring.shardingsphere.sharding.tables.order_info.key-generator.column=order_id spring.shardingsphere.sharding.tables.order_info.key-generator.type=SNOWFLAKE # 指定分片策略 spring.shardingsphere.sharding.tables.order_info.table-strategy.inline.sharding-column=order_id spring.shardingsphere.sharding.tables.order_info.table-strategy.inline.algorithm-expression=order_info_$->{order_id % 4 + 1} # 打开sql输出日志 spring.shardingsphere.props.sql.show=true
4、测试
测试就直接上代码
@Data @Builder public class OrderInfo { private long id; private long orderId; private long venderId; }
@Repository public interface OrderMapper extends BaseMapper<OrderInfo> { }
@RequestMapping("/save") public String save(long orderId, long venderId){ orderService.save(OrderInfo.builder().orderId(orderId).venderId(venderId).build()); return "OK"; }
@SpringBootApplication @MapperScan("com.lcl.galaxy.lcl.galaxy.mysql.dao") public class LclGalaxyMysqlApplication { public static void main(String[] args) { SpringApplication.run(LclGalaxyMysqlApplication.class, args); } }
测试结果如预期一样,根据订单号取余加一,落在了不同的表中。
(二)分库
分库的话就需要配置多个数据源,同时配置分库分表策略时,只分库,不分表,同时配置分库策略
spring.shardingsphere.datasource.names=ds1,ds2 spring.shardingsphere.datasource.ds1.type=com.alibaba.druid.pool.DruidDataSource spring.shardingsphere.datasource.ds1.driver-class-name=com.mysql.cj.jdbc.Driver spring.shardingsphere.datasource.ds1.url=jdbc:mysql://192.168.1.104:3306/lcltest?characterEncoding=utf-8&useUnicode=true&useSSL=false&serverTimezone=UTC spring.shardingsphere.datasource.ds1.username=root spring.shardingsphere.datasource.ds1.password=root spring.shardingsphere.datasource.ds2.type=com.alibaba.druid.pool.DruidDataSource spring.shardingsphere.datasource.ds2.driver-class-name=com.mysql.cj.jdbc.Driver spring.shardingsphere.datasource.ds2.url=jdbc:mysql://192.168.1.106:3306/lcltest?characterEncoding=utf-8&useUnicode=true&useSSL=false&serverTimezone=UTC spring.shardingsphere.datasource.ds2.username=root spring.shardingsphere.datasource.ds2.password=root # 配置表的分布,表的策略 spring.shardingsphere.sharding.tables.order_info_new.actual-data-nodes=ds$->{1..2}.order_info_new # 指定分片策略 spring.shardingsphere.sharding.tables.order_info_new.database-strategy.inline.sharding-column=vender_id spring.shardingsphere.sharding.tables.order_info_new.database-strategy.inline.algorithm-expression=ds$->{vender_id % 2 + 1} # 打开sql输出日志 spring.shardingsphere.props.sql.show=true
(三)分库分表
分库分表与分库的区别就是,配置表的策略时,既分库又分表,同时既要配置分库策略又要配置分表策略。
spring.shardingsphere.datasource.names=ds1,ds2 spring.shardingsphere.datasource.ds1.type=com.alibaba.druid.pool.DruidDataSource spring.shardingsphere.datasource.ds1.driver-class-name=com.mysql.cj.jdbc.Driver spring.shardingsphere.datasource.ds1.url=jdbc:mysql://192.168.1.104:3306/lcltest?characterEncoding=utf-8&useUnicode=true&useSSL=false&serverTimezone=UTC spring.shardingsphere.datasource.ds1.username=root spring.shardingsphere.datasource.ds1.password=root spring.shardingsphere.datasource.ds2.type=com.alibaba.druid.pool.DruidDataSource spring.shardingsphere.datasource.ds2.driver-class-name=com.mysql.cj.jdbc.Driver spring.shardingsphere.datasource.ds2.url=jdbc:mysql://192.168.1.106:3306/lcltest?characterEncoding=utf-8&useUnicode=true&useSSL=false&serverTimezone=UTC spring.shardingsphere.datasource.ds2.username=root spring.shardingsphere.datasource.ds2.password=root # 配置表的分布,表的策略 spring.shardingsphere.sharding.tables.order_info.actual-data-nodes=ds$->{1..2}.order_info_$->{1..4} # 指定订单表 根据订单号生成策略为 SNOWFLAKE spring.shardingsphere.sharding.tables.order_info.key-generator.column=order_id spring.shardingsphere.sharding.tables.order_info.key-generator.type=SNOWFLAKE # 指定分片策略 spring.shardingsphere.sharding.tables.order_info.table-strategy.inline.sharding-column=order_id spring.shardingsphere.sharding.tables.order_info.table-strategy.inline.algorithm-expression=order_info_$->{order_id % 4 + 1} spring.shardingsphere.sharding.tables.order_info.database-strategy.inline.sharding-column=vender_id spring.shardingsphere.sharding.tables.order_info.database-strategy.inline.algorithm-expression=ds$->{vender_id % 2 + 1} # 打开sql输出日志 spring.shardingsphere.props.sql.show=true
二、Sharding JDBC实现读写分离
读写分离,主要就是需要配置配置主从服务器连接,然后配置读写分离的轮询策略等内容。
spring: datasource: url: jdbc:mysql://192.168.1.104:3306/lcltest?characterEncoding=utf-8&useUnicode=true&useSSL=false&serverTimezone=UTC #用户名密码 username: root password: root #数据库驱动 #此處驱动有两个 #com.mysql.jdbc.Driver #com.mysql.cj.jdbc.Driver #MySQL5用的驱动url是com.mysql.jdbc.Driver,MySQL6以后用的是com.mysql.cj.jdbc.Driver。 #使用何种驱动,根据安装MySQL的版本而定 driver-class-name: com.mysql.cj.jdbc.Driver main: allow-bean-definition-overriding: true shardingsphere: datasource: names: lcl104,lcl106 # 主数据源 lcl104: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://192.168.1.104:3306/lcltest?characterEncoding=utf-8&useUnicode=true&useSSL=false&serverTimezone=UTC username: root password: root # 从数据源 lcl106: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://192.168.1.106:3306/lcltest?characterEncoding=utf-8&useUnicode=true&useSSL=false&serverTimezone=UTC username: root password: root masterslave: # 读写分离配置 load-balance-algorithm-type: round_robin # 最终的数据源名称 name: dataSource # 主库数据源名称 master-data-source-name: lcl104 # 从库数据源名称列表,多个逗号分隔 slave-data-source-names: lcl106 props: # 开启SQL显示,默认false sql: show: true
-----------------------------------------------------------
---------------------------------------------
朦胧的夜 留笔~~
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律