本文共 7094 字,大约阅读时间需要 23 分钟。
在基于springboot的ShardingSphere5.X的分库分表的解决方案之分表解决方案(一)这一个章节已经给大家讲解了分表的方案的实现,那么本章将接续上一个章节的内容,将为大家讲解如何基于springboot的ShardingSphere5.X的分库的解决方案,购买了专栏的小伙伴可以加微信:aaabbb158,加微信的时候请加上您购买专栏的当前账号名称的备注,我会为你解答你遇到相应的问题,同时为您提供当前专栏的源代码。
在第一章分表方案的时候我们已经创建了一个db0的数据库,本章我们需要实现分库的解决方案,那么我们需要在第一章的基础上创建一个新的数据库,我们暂且命名为db1,如下所示我们创建一个db1数据库:
然后我们直接在Navicat for MySQL的数据库管理工切换到db1数据库然后执行我们第一章中的建表的SQL脚本,如下所示:
执行的脚本与第一章一样的,SQL脚本如下所示:
drop table if exists t_order0;/*==============================================================*//* Table: t_order0 *//*==============================================================*/create table t_order0( order_id bigint not null comment '订单流水ID', user_id bigint comment '用户流水ID', order_no varchar(100) comment '订单编号', create_time date comment '创建时间', primary key (order_id));alter table t_order0 comment '订单表';drop table if exists t_order1;/*==============================================================*//* Table: t_order1 *//*==============================================================*/create table t_order1( order_id bigint not null comment '订单流水ID', user_id bigint comment '用户流水ID', order_no varchar(100) comment '订单编号', create_time date comment '创建时间', primary key (order_id));alter table t_order1 comment '订单表';drop table if exists t_order2;/*==============================================================*//* Table: t_order2 *//*==============================================================*/create table t_order2( order_id bigint not null comment '订单流水ID', user_id bigint comment '用户流水ID', order_no varchar(100) comment '订单编号', create_time date comment '创建时间', primary key (order_id));alter table t_order2 comment '订单表';drop table if exists t_order3;/*==============================================================*//* Table: t_order3 *//*==============================================================*/create table t_order3( order_id bigint not null comment '订单流水ID', user_id bigint comment '用户流水ID', order_no varchar(100) comment '订单编号', create_time date comment '创建时间', primary key (order_id));alter table t_order3 comment '订单表';
我们现在新增了分库的策略,那么我们需要修改我们的代码,如果我们需要修改我们原先的代码,那么我们的ShardingSphere就会失去他的优势和靓点了,这时候我们完全不需要修改我们的源代码,我们需要做的仅仅是修改我们的application.yml配置就可以完美的实现我们的分库了。
以下是我们第一章的数据源的配置:
那么我们需要在spring.shardingsphere.datasource.names节点后面加上db0,同时配置db0的数据库的配置信息修改完成以后application.yml配置如下所示:
# mybatis的配置mybatis: config-location: classpath:mybatis/mybatis-config.xml mapper-locations: classpath:mybatis/mapper/*.xml# 分页插件的配置pagehelper: helperDialect: mysql reasonable: true# 当前工程端口的配置server: port: 8798spring: shardingsphere: # 展示修改以后的sql语句 props: sql-show: true datasource: common: driver-class-name: com.mysql.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource # 配置多数据源,若我们需要分更多的数据库直接在此处采用小写的逗号分隔以后往后面加即可 names: db0,db1 db0: url: jdbc:mysql://127.0.0.1:3306/db0?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2b8 username: root password: '123456' db1: url: jdbc:mysql://127.0.0.1:3306/db1?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2b8 username: root password: '123456' rules: sharding: key-generators: # 此处必须要配置,否则会导致报错,因为shardingsphere-jdbc-core-spring-boot-starter需要加载此项配置,官网的demo例子有错 snowflake: type: SNOWFLAKE props: worker-id: 123 sharding-algorithms: table-inline: type: INLINE props: # 此处写法必须是t_order$->{order_id % 4}不能写为t_order${order_id % 4},yml不认这种写法 algorithm-expression: t_order$->{ order_id % 4} tables: t_order: # 配置t_order的分表的规则 actual-data-nodes: db0.t_order$->{ 0..3} table-strategy: standard: sharding-column: order_id sharding-algorithm-name: table-inline enabled: true
在上一章我们只配置了分表策略,那么到了此处我们需要进行分库策略,具体的分库分表的原理可以直接在官网的模块上面有详细的讲解,这里就不再详细的叙述了,我们直接上如何使用即可。
配置我们的分库策略一样的也是在spring.shardingsphere.rules.sharding.sharding-algorithms这个节点底下进行配置,以下是我们配置好以后的分库策略的application.yml:
# mybatis的配置mybatis: config-location: classpath:mybatis/mybatis-config.xml mapper-locations: classpath:mybatis/mapper/*.xml# 分页插件的配置pagehelper: helperDialect: mysql reasonable: true# 当前工程端口的配置server: port: 8798spring: shardingsphere: # 展示修改以后的sql语句 props: sql-show: true datasource: common: driver-class-name: com.mysql.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource names: db0,db1 db0: url: jdbc:mysql://127.0.0.1:3306/db0?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2b8 username: root password: '123456' db1: url: jdbc:mysql://127.0.0.1:3306/db1?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2b8 username: root password: '123456' rules: sharding: key-generators: # 此处必须要配置,否则会导致报错,因为shardingsphere-jdbc-core-spring-boot-starter需要加载此项配置,官网的demo例子有错 snowflake: type: SNOWFLAKE props: worker-id: 123 sharding-algorithms: # table-inline这个名称大家可以根据自己的表的名称来随便取,当前为分表的逻辑配置 table-inline: # 使用了内置的分片算法-INLINE type: INLINE props: # 此处写法必须是t_order$->{order_id % 4}不能写为t_order${order_id % 4},yml不认这种写法 algorithm-expression: t_order$->{ order_id % 4} # database-inline这个名称一样大家可以根据自己的数据库来随便取,当前为分库的逻辑配置 database-inline: type: INLINE props: # 此处根据order_id取余来实现指向相应的数据库,例如你的order_id为1则指向db1数据库,order_id为2则指向db0数据库 algorithm-expression: db$->{ order_id % 2} tables: # t_order表的分库分表的策略 t_order: # 配置t_order的分表的规则 actual-data-nodes: db$->{ 0..1}.t_order$->{ 0..3} table-strategy: standard: sharding-column: order_id sharding-algorithm-name: table-inline # 配置t_order的分库规则 database-strategy: standard: sharding-column: order_id # 此处使用的就是我们在sharding-algorithms里面定义的规则 sharding-algorithm-name: database-inline enabled: true
到此处我们已经完成了分库分表的配置了,这时候我们可以启动我们的项目,然后同第一章一样直接访问:,然后我们首先模拟往数据库db0插入一条数据,前端输入如下所示:
这时候我们可以通过控制台的日志看下我们的数据是不是插入到了db0【10%2=0】的t_order2【10%4=2】表中:
通过观察我们发现的确在日志中数据已经插入到我们的db0的t_order2表中了,这时候我们需要验证最后一把,那就是通过我们的数据库管理工具,打开我们的db0数据库的t_order2表看下里面是否有数据:
很明显我们的数据已经进入了我们想要的表中了,那么到此处我们就完成了我们的基于springboot的ShardingSphere5.X的分库的方案的实现了。
转载地址:http://kokvf.baihongyu.com/