ShardingSphere 实现分库分表

1、具体步骤

1.1 引入依赖

        在pom.xml文件中引入ShardingSphere 的相关依赖:

<shardingsphere.version>5.3.2</shardingsphere.version>

<dependency>
    <groupId>org.apache.shardingsphere</groupId>
    <artifactId>shardingsphere-jdbc-core</artifactId>
    <version>${shardingsphere.version}</version>
</dependency>

1.2 新建配置文件

        在resource文件夹下新建 shardingsphere-config.yaml 配置文件:    

1.2.1 数据源配置

dataSources:
  # 以电商订单库数据源,按编号区分
  order_db_0:
    dataSourceClassName: com.zaxxer.hikari.HikariDataSource
    driverClassName: com.mysql.cj.jdbc.Driver
    jdbcUrl: jdbc:mysql://127.0.0.1:3306/ecommerce_order_0?useUnicode=true&characterEncoding=UTF-8&rewriteBatchedStatements=true&allowMultiQueries=true&serverTimezone=Asia/Shanghai
    username: root
    password: root
  order_db_1:
    dataSourceClassName: com.zaxxer.hikari.HikariDataSource
    driverClassName: com.mysql.cj.jdbc.Driver
    jdbcUrl: jdbc:mysql://127.0.0.1:3306/ecommerce_order_1?useUnicode=true&characterEncoding=UTF-8&rewriteBatchedStatements=true&allowMultiQueries=true&serverTimezone=Asia/Shanghai
    username: root
    password: root

1.2.2 分片规则配置

rules:
  - !SHARDING
    tables:
      # 电商订单主表
      t_order:
        actualDataNodes: order_db_${0..1}.t_order_${0..15}
        databaseStrategy:
          standard:
            shardingColumn: user_id  # 电商场景常用用户ID作为分库键,便于查询用户的所有订单
            shardingAlgorithmName: order_db_inline
        tableStrategy:
          standard:
            shardingColumn: user_id  # 表级也按用户ID分片,确保同一用户订单在同一表
            shardingAlgorithmName: order_table_inline
      # 订单明细表(与订单主表保持一致分片规则)
      t_order_item:
        actualDataNodes: order_db_${0..1}.t_order_item_${0..15}
        databaseStrategy:
          standard:
            shardingColumn: user_id
            shardingAlgorithmName: order_db_inline
        tableStrategy:
          standard:
            shardingColumn: user_id
            shardingAlgorithmName: order_table_inline
    shardingAlgorithms:
      # 订单分库算法
      order_db_inline:
        type: INLINE
        props:
          algorithm-expression: order_db_${user_id % 2}  # 按用户ID取模2,分配到2个库
      # 订单分表算法
      order_table_inline:
        type: INLINE
        props:
          algorithm-expression: t_order_${user_id % 16}  # 每个库内分16张表

1.2.3 全局属性配置

props:
  sql-show: true  # 开发环境打印SQL,便于调试分库分表路由
  executor-size: 10  # 执行线程池大小,根据电商并发量调整

1.2.4 完整配置

dataSources:
  # 电商订单库数据源,按编号区分
  order_db_0:
    dataSourceClassName: com.zaxxer.hikari.HikariDataSource
    driverClassName: com.mysql.cj.jdbc.Driver
    jdbcUrl: jdbc:mysql://127.0.0.1:3306/ecommerce_order_0?useUnicode=true&characterEncoding=UTF-8&rewriteBatchedStatements=true&allowMultiQueries=true&serverTimezone=Asia/Shanghai
    username: root
    password: root
  order_db_1:
    dataSourceClassName: com.zaxxer.hikari.HikariDataSource
    driverClassName: com.mysql.cj.jdbc.Driver
    jdbcUrl: jdbc:mysql://127.0.0.1:3306/ecommerce_order_1?useUnicode=true&characterEncoding=UTF-8&rewriteBatchedStatements=true&allowMultiQueries=true&serverTimezone=Asia/Shanghai
    username: root
    password: root

rules:
  - !SHARDING
    tables:
      # 电商订单主表
      t_order:
        actualDataNodes: order_db_${0..1}.t_order_${0..15}
        databaseStrategy:
          standard:
            shardingColumn: user_id  # 电商场景常用用户ID作为分库键,便于查询用户的所有订单
            shardingAlgorithmName: order_db_inline
        tableStrategy:
          standard:
            shardingColumn: user_id  # 表级也按用户ID分片,确保同一用户订单在同一表
            shardingAlgorithmName: order_table_inline
      # 订单明细表(与订单主表保持一致分片规则)
      t_order_item:
        actualDataNodes: order_db_${0..1}.t_order_item_${0..15}
        databaseStrategy:
          standard:
            shardingColumn: user_id
            shardingAlgorithmName: order_db_inline
        tableStrategy:
          standard:
            shardingColumn: user_id
            shardingAlgorithmName: order_table_inline
    shardingAlgorithms:
      # 订单分库算法
      order_db_inline:
        type: INLINE
        props:
          algorithm-expression: order_db_${user_id % 2}  # 按用户ID取模2,分配到2个库
      # 订单分表算法
      order_table_inline:
        type: INLINE
        props:
          algorithm-expression: t_order_${user_id % 16}  # 每个库内分16张表

props:
  sql-show: true  # 开发环境打印SQL,便于调试分库分表路由
  executor-size: 10  # 执行线程池大小,根据电商并发量调整
    折磨多配置能不能分块讲解一下

1.3 在application.yaml里添加配置

spring:
  datasource:
    driver-class-name: org.apache.shardingsphere.driver.ShardingSphereDriver
    url: jdbc:shardingsphere:classpath:shardingsphere-config.yaml
<think>好的,用户想了解在ruoyi-vue-pro项目中如何集成ShardingSphere实现分库分表。我需要先回顾一下相关的引用内容,特别是引用[2]提到yudao-cloud集成shardingsphere-jdbc的过程,用户可能希望类似的步骤。 首先,用户可能已经知道ShardingSphere的基本概念,但需要具体的配置示例。根据引用[3],单库分表的配置方式是通过设置actual-data-nodes,比如分表到t_order_0到t_order_4。这可能是一个配置示例的关键点。 然后,引用[2]提到需要查看官网文档和解决集成中的bug,说明在集成过程中可能会遇到问题,用户可能需要详细的步骤和注意事项。此外,引用[4]指出分库分表可能看起来复杂,但实际配置可能较简单,这提示我需要将步骤分解得清晰易懂。 接下来,我需要确定ruoyi-vue-pro项目的结构,通常Spring Boot项目会在application.yml中配置ShardingSphere。需要添加Maven依赖,如shardingsphere-jdbc-core-spring-boot-starter,版本可能需要根据项目选择。 然后,配置分表规则,比如根据用户ID分片,使用算法。需要编写具体的分片策略类,如UserIdShardingAlgorithm,并配置actual-data-nodes和分片键。同时,需要启用ShardingSphere的配置,设置spring.shardingsphere.enabled为true。 用户可能还会关心如何验证分表是否成功,比如插入数据后查看不同表的数据分布。此外,可能遇到的问题如依赖冲突、分片键选择不合理,需要给出解决建议。 最后,生成相关问题,帮助用户进一步学习,比如分库分表策略选择、动态扩容等。确保回答结构清晰,步骤详细,并参考提供的引用内容,特别是引用[2]和[3]中的配置示例和集成经验。</think>根据Yudao-Cloud项目集成ShardingSphere-JDBC的实践经验,在Ruoyi-Vue-Pro项目中实现分库分表可分为以下步骤: ### 一、环境准备 1. 确保项目使用Spring Boot 2.x + MyBatis框架 2. 在`pom.xml`添加ShardingSphere依赖: ```xml <dependency> <groupId>org.apache.shardingsphere</groupId> <artifactId>shardingsphere-jdbc-core-spring-boot-starter</artifactId> <version>5.3.2</version> <!-- 按需选择版本 --> </dependency> ``` ### 二、分表配置示例(以订单表t_order为例) ```yaml spring: shardingsphere: enabled: true datasource: names: ds0 ds0: type: com.zaxxer.hikari.HikariDataSource driver-class-name: com.mysql.cj.jdbc.Driver jdbc-url: jdbc:mysql://localhost:3306/db0 username: root password: 123456 rules: sharding: tables: t_order: actual-data-nodes: ds0.t_order_$->{0..4} # 分5张表[^3] table-strategy: standard: sharding-column: order_id sharding-algorithm-name: order-mod sharding-algorithms: order-mod: type: MOD props: sharding-count: 5 ``` ### 三、分片算法实现 可自定义精确分片算法: ```java public class OrderIdShardingAlgorithm implements PreciseShardingAlgorithm<Long> { @Override public String doSharding(Collection<String> availableTargetNames, PreciseShardingValue<Long> shardingValue) { long orderId = shardingValue.getValue(); return "t_order_" + (orderId % 5); } } ``` ### 四、注意事项 1. **分片键选择**:应选择高频查询字段且数据分布均匀的列,如用户ID、订单ID[^2] 2. **SQL兼容性**:避免跨分片JOIN、子查询等复杂操作 3. **分布式事务**:建议结合Seata实现分布式事务 4. **数据迁移**:存量数据需通过`ShardingSphere-Scaling`迁移工具处理 ### 五、验证分表效果 1. 执行插入测试: ```sql INSERT INTO t_order (order_id, user_id) VALUES (123, 1001) ``` 2. 观察数据实际存储在`t_order_3`表(123 mod 5=3) ### 六、常见问题解决 1. **依赖冲突**:排除冲突的JDBC连接池依赖(如Druid) 2. **分片失效**:检查SQL是否包含分片键 3. **配置不生效**:确认`spring.shardingsphere.enabled=true`
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值