这篇文章主要讲解了“ShardingSphere5.0.0-alpha如何实现MySQL分库分表”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“ShardingSphere5.0.0-alpha如何实现mysql分库分表”吧!

高昌网站建设公司创新互联公司,高昌网站设计制作,有大型网站制作公司丰富经验。已为高昌上千家提供企业网站建设服务。企业网站搭建\外贸网站建设要多少钱,请找那个售后服务好的高昌做网站的公司定做!
声明
- 本文会基于 Springboot + mybatis + shardingsphere + mysql5.6 + druid 进行实战讲解 
- 本文在上一篇文章[数据分表]的基础上增加了 分库的功能 
- 本文不会介绍 shardingsphere 以及分库分表的相关概念 
- 本文采用的 shardingsphere 版本是 5.0.0-alpha, 具体见 pom 文件 
- 本文涉及的源码请参考 分库 
- 如果看官方文档时, 请选择对应的版本 !!! 
- 文中涉及的源码可能会有误, 请以上传到 gitee 的源码为准. 
正文
需求
我们有两个数据库 miaosha2 和 miaosha3, 每个数据库中都有 2 张被拆分过的用户表 user_info0 和 user_info1
当我们往用户表插数据时, 会按照一定的规则(根据自增id取模), 落到某个 miaosha 库中的某张 user_info 表中.
准备工作
1. 数据库表
create database miaosha2; DROP TABLE IF EXISTS `miaosha2`.`user_info0`; CREATE TABLE `miaosha2`.`user_info0` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `user_label` varchar(32) COLLATE utf8_bin DEFAULT NULL, `username` varchar(64) COLLATE utf8_bin DEFAULT NULL, `email` varchar(64) COLLATE utf8_bin DEFAULT NULL, `phone` varchar(64) COLLATE utf8_bin DEFAULT NULL, `password` varchar(128) COLLATE utf8_bin NOT NULL, `active` tinyint(4) NOT NULL DEFAULT '1', PRIMARY KEY (`id`) ) ENGINE = InnoDB AUTO_INCREMENT = 7 DEFAULT CHARSET = utf8 COLLATE = utf8_bin; DROP TABLE IF EXISTS `miaosha2`.`user_info1`; CREATE TABLE `miaosha2`.`user_info1` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `user_label` varchar(32) COLLATE utf8_bin DEFAULT NULL, `username` varchar(64) COLLATE utf8_bin DEFAULT NULL, `email` varchar(64) COLLATE utf8_bin DEFAULT NULL, `phone` varchar(64) COLLATE utf8_bin DEFAULT NULL, `password` varchar(128) COLLATE utf8_bin NOT NULL, `active` tinyint(4) NOT NULL DEFAULT '1', PRIMARY KEY (`id`) ) ENGINE = InnoDB AUTO_INCREMENT = 6 DEFAULT CHARSET = utf8 COLLATE = utf8_bin; create database miaosha3; DROP TABLE IF EXISTS `miaosha3`.`user_info0`; CREATE TABLE `miaosha3`.`user_info0` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `user_label` varchar(32) COLLATE utf8_bin DEFAULT NULL, `username` varchar(64) COLLATE utf8_bin DEFAULT NULL, `email` varchar(64) COLLATE utf8_bin DEFAULT NULL, `phone` varchar(64) COLLATE utf8_bin DEFAULT NULL, `password` varchar(128) COLLATE utf8_bin NOT NULL, `active` tinyint(4) NOT NULL DEFAULT '1', PRIMARY KEY (`id`) ) ENGINE = InnoDB AUTO_INCREMENT = 7 DEFAULT CHARSET = utf8 COLLATE = utf8_bin; DROP TABLE IF EXISTS `miaosha3`.`user_info1`; CREATE TABLE `miaosha3`.`user_info1` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `user_label` varchar(32) COLLATE utf8_bin DEFAULT NULL, `username` varchar(64) COLLATE utf8_bin DEFAULT NULL, `email` varchar(64) COLLATE utf8_bin DEFAULT NULL, `phone` varchar(64) COLLATE utf8_bin DEFAULT NULL, `password` varchar(128) COLLATE utf8_bin NOT NULL, `active` tinyint(4) NOT NULL DEFAULT '1', PRIMARY KEY (`id`) ) ENGINE = InnoDB AUTO_INCREMENT = 6 DEFAULT CHARSET = utf8 COLLATE = utf8_bin;
2. pom 依赖
4.0.0 org.springframework.boot spring-boot-starter-parent 2.3.2.RELEASE com.nimo shardingsphere-demo 0.0.1-SNAPSHOT shardingsphere-demo 1.8 org.springframework.boot spring-boot-starter-web org.mybatis.spring.boot mybatis-spring-boot-starter 2.1.4 mysql mysql-connector-java runtime org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test org.apache.shardingsphere shardingsphere-jdbc-core-spring-boot-starter 5.0.0-alpha com.alibaba druid 1.2.3 org.springframework.boot spring-boot-maven-plugin org.projectlombok lombok 
3. application.yml
再次强调下, 本文采用的 shardingsphere 版本是 5.0.0-alpha. 不同版本配置会有差异.
本文在上一篇文章的基础上增加, 并修改了几个配置, 下面的源码中有标记出来
- 添加了一个数据源配置 
- 添加了一个分库策略 
- 添加了一个分库算法 
server:
  port: 8777
spring:
  shardingsphere:
    # 展示修改以后的sql语句
    props:
      sql-show: true
    datasource:
      # (这里增加了一个 ds1 的数据源)
      names: ds0,ds1
      common:
        type: com.alibaba.druid.pool.DruidDataSource
      ds0:
        url: jdbc:mysql://127.0.0.1:3306/miaosha2?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2b8
        username: root
        password: '123456'
        driver-class-name: com.mysql.cj.jdbc.Driver
      # (新增的配置)
      ds1:
        url: jdbc:mysql://127.0.0.1:3306/miaosha3?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2b8
        username: root
        password: '123456'
        driver-class-name: com.mysql.cj.jdbc.Driver
    rules:
      sharding:
        # 分布式序列算法配置
        key-generators:
          # 此处必须要配置,否则会导致报错
          snowflake:
            type: SNOWFLAKE
            props:
              worker-id: 123
        # 配置 user_info 表
        tables:
          user_info:
            # 分库策略 (新增的配置)
            database-strategy:
              standard:
                  sharding-column: id
                  sharding-algorithm-name: database-inline
            # 配置user_info的分库分表的规则 (增加了数据源的配置)
            actual-data-nodes: ds$->{0..1}.user_info$->{0..1}
            # 单分片键的标准分片
            table-strategy:
              standard:
                sharding-column: id
                sharding-algorithm-name: table-inline
            # 主键id生成策略(雪花算法)
            key-generate-strategy:
              key-generator-name: snowflake
              column: id
        # 配置分片算法
        sharding-algorithms:
          # 通过 id 取模的方式确定数据落到哪个库 (新增的配置)
          database-inline:
            type: INLINE
            props:
              algorithm-expression: ds$->{id % 2}
          # 通过 id 取模的方式确定数据落到哪个表
          table-inline:
            type: INLINE
            props:
              algorithm-expression: user_info$->{id % 2}
    enabled: true
mybatis:
  typeAliasesPackage: com.nimo.shardingdatabasedemo.entity
  mapperLocations: classpath:mapper/*.xml4. 主要代码
// sqlinsert into user_info(id, username, password) values (#{id}, #{username}, #{password}) // 新增一个用户信息 @PostMapping("userinfo") public Object addUserInfo(@RequestBody UserInfo userInfo) { return userInfoMapper.addUser(userInfo); }
5. 测试命令
curl -X POST --location "http://localhost:8777/userinfo" \
    -H "Content-Type: application/json" \
    -d "{
          \"username\": \"wangbadan\",
          \"password\": \"123456\"
        }"感谢各位的阅读,以上就是“ShardingSphere5.0.0-alpha如何实现mysql分库分表”的内容了,经过本文的学习后,相信大家对ShardingSphere5.0.0-alpha如何实现mysql分库分表这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是创新互联,小编将为大家推送更多相关知识点的文章,欢迎关注!
新闻名称:ShardingSphere5.0.0-alpha如何实现mysql分库分表
本文地址:http://www.scyingshan.cn/article/pogggj.html

 建站
建站
 咨询
咨询 售后
售后
 建站咨询
建站咨询 
 