• MySQL中的分库分表框架-ShardingSphere


    一、前言

    ShardingSphere,大家多少都有听过吧,Apache顶级项目,国内大佬的巨作,Java中用的最多的一个分库分表框架,如果你们的系统中需要分库分表,强烈建议使用,完全可以满足你的所有需求。

    本文并不会介绍什么是分库分表,而是通过大量案例,让你了解ShardingSphere可以做什么?如何做?以及SpringBoot中如何使用它等等。

    ShardingSphere的Git官网地址

    https://github.com/apache/shardingsphere

    ShardingSphere目前最新版本5.X了,大版本之间变化比较大,本次以4.1.1为例来介绍。

    如果对分库分表没有概念,可以先去下面这个地址看看,然后再继续向下看。 

    https://shardingsphere.apache.org/document/legacy/4.x/document/cn/overview/

    原文地址:

    http://itsoku.com/course/25/410

     

    二、纯Java API代码案例

    1)引入shardingsphere的maven配置

    1. <dependency>
    2. <groupId>org.apache.shardingspheregroupId>
    3. <artifactId>sharding-jdbc-coreartifactId>
    4. <version>4.1.1version>
    5. dependency>

    2)完整的maven配置如下

    1. "1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <modelVersion>4.0.0modelVersion>
    5. <parent>
    6. <groupId>org.springframework.bootgroupId>
    7. <artifactId>spring-boot-starter-parentartifactId>
    8. <version>2.7.1version>
    9. <relativePath/>
    10. parent>
    11. <groupId>com.bcgroupId>
    12. <artifactId>demoartifactId>
    13. <version>0.0.1-SNAPSHOTversion>
    14. <name>demoname>
    15. <description>Demo project for Spring Bootdescription>
    16. <properties>
    17. <java.version>1.8java.version>
    18. properties>
    19. <dependencies>
    20. <dependency>
    21. <groupId>org.springframework.bootgroupId>
    22. <artifactId>spring-boot-starterartifactId>
    23. dependency>
    24. <dependency>
    25. <groupId>org.springframework.bootgroupId>
    26. <artifactId>spring-boot-starter-testartifactId>
    27. <scope>testscope>
    28. dependency>
    29. <dependency>
    30. <groupId>org.mybatis.spring.bootgroupId>
    31. <artifactId>mybatis-spring-boot-starterartifactId>
    32. <version>2.2.2version>
    33. dependency>
    34. <dependency>
    35. <groupId>mysqlgroupId>
    36. <artifactId>mysql-connector-javaartifactId>
    37. dependency>
    38. <dependency>
    39. <groupId>org.projectlombokgroupId>
    40. <artifactId>lombokartifactId>
    41. <optional>trueoptional>
    42. dependency>
    43. <dependency>
    44. <groupId>org.apache.shardingspheregroupId>
    45. <artifactId>sharding-jdbc-coreartifactId>
    46. <version>4.1.1version>
    47. dependency>
    48. dependencies>
    49. <build>
    50. <plugins>
    51. <plugin>
    52. <groupId>org.springframework.bootgroupId>
    53. <artifactId>spring-boot-maven-pluginartifactId>
    54. plugin>
    55. plugins>
    56. build>
    57. project>

    其完整的项目结构如下所示:

      

    2.1、案例1:单库多表

    需求:一个库中有2个订单表,按照订单id取模,将数据路由到指定的表。

    在db1数据库中创建表t_order_0t_order_1

    1. drop database if exists db1;
    2. create database db1;
    3. use db1;
    4. drop table if exists t_order_0;
    5. create table t_order_0(
    6. order_id bigint not null primary key,
    7. user_id bigint not null,
    8. price bigint not null
    9. );
    10. drop table if exists t_order_1;
    11. create table t_order_1(
    12. order_id bigint not null primary key,
    13. user_id bigint not null,
    14. price bigint not null
    15. );

    Java代码:

    1. package com.bc.demo.apidemo;
    2. import com.zaxxer.hikari.HikariDataSource;
    3. import org.apache.shardingsphere.api.config.sharding.ShardingRuleConfiguration;
    4. import org.apache.shardingsphere.api.config.sharding.TableRuleConfiguration;
    5. import org.apache.shardingsphere.api.config.sharding.strategy.InlineShardingStrategyConfiguration;
    6. import org.apache.shardingsphere.shardingjdbc.api.ShardingDataSourceFactory;
    7. import org.apache.shardingsphere.underlying.common.config.propertie
  • 相关阅读:
    如何搭建测试环境?一文解决你所有疑惑!
    国际公认—每个领导者必须拥抱的11项领导力转变
    YOLOv5优化:独家创新(SC_C_Detect)检测头结构创新,实现涨点 | 检测头新颖创新系列
    微信小程序自定义头部
    微服务--Zuul详解
    # 智慧社区管理系统-核心业务管理-03投诉信息
    laravel中 指定字段 指定数值排序
    matlab 2ask 4ask 信号调制
    交互与前端10 Tabulator+Flask开发日志007
    黑龙江—等保测评三级安全设计思路
  • 原文地址:https://blog.csdn.net/y_bccl27/article/details/127718628