zoukankan      html  css  js  c++  java
  • Sharding-JDBC实现垂直拆分

    参考资料:猿天地  https://mp.weixin.qq.com/s/wl8h6LIQUHztVuVbjfsU3Q  作者:尹吉欢 

      当一个项目量增大,数据表数量增多时,就需要对数据表进行垂直拆分,比如:把会员相关的表放到一个库里,订单相关的表放一个库,商品库存的表放一个库里。垂直拆分后,项目中就需要读取多个数据源,根据表不同动态切换数据源进行操作:

      垂直拆分的优点:拆分之后业务规划清晰,数据维护简单,分担了数据集中存储的压力。

      垂直拆分的缺点:缺点也很明显,多表join查询无法实现,只能通过接口方式解决,提高了系统复杂度等问题。

      技术选型:SpringBoot + Sharding-JDBC + MyBatis

    1. 核心Jar包

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- sharding -->
        <dependency>
            <groupId>org.apache.shardingsphere</groupId>
            <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
            <version>4.0.0-RC1</version>
        </dependency>
        <!-- mybatis-plus -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.2.0</version>
        </dependency>
        <!--阿里数据库连接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.14</version>
        </dependency>
        <!-- mysql -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.18</version>
         </dependency>
         <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.60</version>
         </dependency>
     </dependencies>

    2. yml文件配置

    spring:
      main:
        allow-bean-definition-overriding: true
      shardingsphere:
        datasource:
          names: db1,db2
          # user数据源
          db1:
            type: com.alibaba.druid.pool.DruidDataSource
            driver-class-name: com.mysql.cj.jdbc.Driver
            url: jdbc:mysql://localhost:3306/db_user?characterEncoding=utf-8
            username: ****
            password: ****
          # order数据源
          db2:
            type: com.alibaba.druid.pool.DruidDataSource
            driver-class-name: com.mysql.cj.jdbc.Driver
            url: jdbc:mysql://localhost:3306/db_order?characterEncoding=utf-8
            username: ****
            password: ****
        sharding:
          tables:
            # 绑定表节点
            user_info:
              actual-data-nodes: db1.user_info
            address_info:
              actual-data-nodes: db2.order_info
        props:
          # 开启SQL显示,默认false
          sql:
            show: true

      注意:在sharding.tables节点上,要配置每张表所属的数据库。

    3. 启动项目测试

    2019-12-18 16:19:24.324  INFO 18224 --- [    main] com.alibaba.druid.pool.DruidDataSource   : {dataSource-1} inited
    2019-12-18 16:19:25.470  INFO 18224 --- [    main] com.alibaba.druid.pool.DruidDataSource   : {dataSource-2} inited

    执行SQL时,会打印一下日志:

     --- [   main] ShardingSphere-SQL     : Actual SQL: db1 ::: SELECT  id,address,gender,username  FROM user_info
     --- [   main] ShardingSphere-SQL     : Actual SQL: db2 ::: SELECT  id,order_no  FROM order_info

    4. 垂直拆分后的读写分离

    每个库增加一个从节点的配置就可以了,然后用master-slave-rules将主从数据源进行绑定。

    spring:
      main:
        allow-bean-definition-overriding: true
      shardingsphere:
        datasource:
          names: db1,db1slave,db2,db2slave
          # user数据源
          db1:
            type: com.alibaba.druid.pool.DruidDataSource
            driver-class-name: com.mysql.cj.jdbc.Driver
            url: jdbc:mysql://localhost:3306/db_user?characterEncoding=utf-8
            username: ****
            password: ****
          db1slave:
            type: com.alibaba.druid.pool.DruidDataSource
            driver-class-name: com.mysql.cj.jdbc.Driver
            url: jdbc:mysql://localhost:3306/db_user_slave?characterEncoding=utf-8
            username: ****
          # order数据源
          db2:
            type: com.alibaba.druid.pool.DruidDataSource
            driver-class-name: com.mysql.cj.jdbc.Driver
            url: jdbc:mysql://localhost:3306/db_order?characterEncoding=utf-8
            username: ****
            password: ****
          db2slave:
            type: com.alibaba.druid.pool.DruidDataSource
            driver-class-name: com.mysql.cj.jdbc.Driver
            url: jdbc:mysql://localhost:3306/db_order?characterEncoding=utf-8
            username: ****
            password: ****
        sharding:
          tables:
            # 绑定表节点
            user_info:
              actual-data-nodes: db1.user_info
            address_info:
              actual-data-nodes: db2.order_info
          # 读写分离
          master-slave-rules:
            db1:
              master-data-source-name: db1
              slave-data-source-names: db1slave
            db2:
              master-data-source-name: db2
              slave-data-source-names: db2slave
        props:
          # 开启SQL显示,默认false
          sql:
            show: true
  • 相关阅读:
    轮播图2
    点击按钮切换轮播图
    轮播图
    2016.5.5_十进制转二进制【ABAP】
    2016.4.26_longtext长文本【ABAP】
    2016.4.26_动态内表【ABAP】
    2016.4.26_下载abap代码【ABAP】
    2016.4.15_debug小技巧【ABAP】
    2016.4.1_js向controller传数据【笔记】
    2016.3.21_TABLE CONTROL【ABAP】
  • 原文地址:https://www.cnblogs.com/huanshilang/p/12061342.html
Copyright © 2011-2022 走看看