您当前的位置: 首页 >  ar

止步前行

暂无认证

  • 4浏览

    0关注

    247博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

ShardingSphere 5.0.0-alpha 分库分表解决方案(二)——分库分表

止步前行 发布时间:2022-01-08 14:15:00 ,浏览量:4

文章目录
  • 1. ShardingSphere-JDBC分库分表
    • 1.1 环境搭建
      • 1.1.1 pom.xml
      • 1.1.2 创建数据库和表
      • 1.1.3 编写业务代码
      • 1.1.4 配置文件
      • 1.1.5 测试结果
  • 2. 说明

1. ShardingSphere-JDBC分库分表 1.1 环境搭建

环境说明:SpringBoot 2.5.7+ MyBatisPlus + ShardingSphere-JDBC 5.0.0-alpha + Druid+ MySQL 8.0

1.1.1 pom.xml

    
        org.springframework.boot
        spring-boot-starter-web
    
    
        org.projectlombok
        lombok
        true
    
    
        io.springfox
        springfox-swagger2
        2.9.2
    
    
        io.springfox
        springfox-swagger-ui
        2.9.2
    
    
        com.alibaba
        druid
        1.1.13
    
    
        com.baomidou
        mybatis-plus-boot-starter
        3.4.3
    
    
        mysql
        mysql-connector-java
        8.0.27
    
    
        org.apache.shardingsphere
        shardingsphere-jdbc-core-spring-boot-starter
        5.0.0-alpha
    
    
        org.springframework.boot
        spring-boot-starter-test
        test
    

1.1.2 创建数据库和表

按照水平分表的方式,创建数据库和数据库表,在上一篇分表中,已经创建了ss_course_db_1

  • 创建数据库:ss_course_db_1ss_course_db_2
  • 在两个数据库分表都创建两张表:t_course_1t_course_2
  • 约定分库规则:约定userId值偶数添加到ss_course_db_1库,userId是奇数添加到ss_course_db_2
  • 约定分表规则:如果cid是偶数把数据添加t_course_1,如果cid奇数添加到t_course_2

注意:此处的ss_course_db_1ss_course_db_2都在同一个数据库中

drop table if EXISTS t_course_1;
create table t_course_1(
   cid BIGINT(20) PRIMARY KEY,
   cname VARCHAR(50) NOT NULL,
   user_id BIGINT(20) NOT NULL,
   cstatus VARCHAR(10) NOT NULL
);

drop table if EXISTS t_course_2;
create table t_course_2(
   cid BIGINT(20) PRIMARY KEY,
   cname VARCHAR(50) NOT NULL,
   user_id BIGINT(20) NOT NULL,
   cstatus VARCHAR(10) NOT NULL
);

在这里插入图片描述

1.1.3 编写业务代码

此处编写业务代码略,具体代码可以下面的源码地址,经过本人调试过。代码里集成了Swagger,用于方便测试。

1.1.4 配置文件
server.port=8002

spring.shardingsphere.enabled=true
# 打开sql输出日志
spring.shardingsphere.props.sql-show=true

# 配置数据源,给数据源起名称
spring.shardingsphere.datasource.names=m1,m2

# 配置第一个数据源具体内容,包含连接池,驱动,地址,用户名和密码
spring.shardingsphere.datasource.common.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.common.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m1.url=jdbc:mysql://127.0.0.1:3306/ss_course_db_1?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2b8
spring.shardingsphere.datasource.m1.username=root
spring.shardingsphere.datasource.m1.password=123456

# 配置第二个数据源
spring.shardingsphere.datasource.m2.url=jdbc:mysql://127.0.0.1:3306/ss_course_db_2?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2b8
spring.shardingsphere.datasource.m2.username=root
spring.shardingsphere.datasource.m2.password=123456

# 指定course表里面主键cid 生成策略  SNOWFLAKE
spring.shardingsphere.rules.sharding.key-generators.snowflake.type=SNOWFLAKE
spring.shardingsphere.rules.sharding.key-generators.snowflake.props.worker-id=123

# 指定分库策略  约定userId值偶数添加到m1库,userId是奇数添加到m2库
spring.shardingsphere.rules.sharding.sharding-algorithms.database-inline.type=INLINE
spring.shardingsphere.rules.sharding.sharding-algorithms.database-inline.props.algorithm-expression=m$->{user_id%2 + 1}

# 指定分表策略  约定cid值偶数添加到t_course_1表,如果cid是奇数添加到t_course_2表
spring.shardingsphere.rules.sharding.sharding-algorithms.table-inline.type=INLINE
spring.shardingsphere.rules.sharding.sharding-algorithms.table-inline.props.algorithm-expression=t_course_$->{cid % 2 + 1}

spring.shardingsphere.rules.sharding.tables.t_course.actual-data-nodes=m$->{1..2}.t_course_$->{1..2}
spring.shardingsphere.rules.sharding.tables.t_course.database-strategy.standard.sharding-column=user_id
spring.shardingsphere.rules.sharding.tables.t_course.database-strategy.standard.sharding-algorithm-name=database-inline
spring.shardingsphere.rules.sharding.tables.t_course.table-strategy.standard.sharding-column=cid
spring.shardingsphere.rules.sharding.tables.t_course.table-strategy.standard.sharding-algorithm-name=table-inline
1.1.5 测试结果

启动程序,在浏览器输入:http://localhost:8002/swagger-ui.html

在这里插入图片描述

添加课程

在这里插入图片描述

在这里插入图片描述

查看数据库表数据

在这里插入图片描述

查看所有课程数据

在这里插入图片描述

在这里插入图片描述

2. 说明

源码地址:https://github.com/Hofanking/springboot-shardingsphere-example

源代码目录结构说明:

springboot-shardingsphere-example

​ |— shardingsphere-database (分库分表)

​ |— shardingsphere-database-table-write-read (分库分表读写分离)

​ |— shardingsphere-proxy-table (使用proxy分表)

​ |— shardingsphere-public (公共表)

​ |— shardingsphere-table (分表)

​ |— shardingsphere-write-read (读写分离)

关注
打赏
1657848381
查看更多评论
立即登录/注册

微信扫码登录

0.1847s