ShardingSphere数据库分库分表中间件

ShardingSphere

数据库分库分表中间件

一款分布式的数据库生态系统,可以将任意的数据库转换为分布式数据库,并通过数据分片,弹性伸缩,加密等能力对原有的数据库进行增强

基于底层数据库提供分布式数据库解决方案

可以水平扩展计算和存储,使用ShardingSphere的数据分片即可实现分库分表

导入依赖

xml 复制代码
<dependency>
    <groupId>com.jzo2o</groupId>
    <artifactId>jzo2o-shardingsphere-jdbc</artifactId>
</dependency>

配置

yml 复制代码
dataSources:
	//0号库
  jzo2o-orders-0:
    dataSourceClassName: com.zaxxer.hikari.HikariDataSource
    jdbcUrl: jdbc:mysql://192.168.101.68:3306/jzo2o-orders-0?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai
    username: root
    password: mysql
    //1号库
  jzo2o-orders-1:
    dataSourceClassName: com.zaxxer.hikari.HikariDataSource
    jdbcUrl: jdbc:mysql://192.168.101.68:3306/jzo2o-orders-1?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai
    username: root
    password: mysql
    //2号库
  jzo2o-orders-2:
    dataSourceClassName: com.zaxxer.hikari.HikariDataSource
    jdbcUrl: jdbc:mysql://192.168.101.68:3306/jzo2o-orders-2?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai
    username: root
    password: mysql
rules:
- !TRANSACTION
  defaultType: BASE
- !SHARDING
//分表规则
  tables:
    orders:
      actualDataNodes: jzo2o-orders-${0..2}.orders_${0..2}
      tableStrategy:
        standard:
          shardingColumn: id
          shardingAlgorithmName: orders_table_inline
      databaseStrategy:
        standard:
          shardingColumn: user_id
          shardingAlgorithmName: orders_database_inline
    orders_serve:
      actualDataNodes: jzo2o-orders-${0..2}.orders_serve_${0..2}
      tableStrategy:
        standard:
          shardingColumn: id
          shardingAlgorithmName: orders_serve_table_inline
      databaseStrategy:
        standard:
          shardingColumn: serve_provider_id
          shardingAlgorithmName: orders_serve_database_inline
    biz_snapshot:
      actualDataNodes: jzo2o-orders-${0..2}.biz_snapshot_${0..2}
      tableStrategy:
        standard:
          shardingColumn: biz_id
          shardingAlgorithmName: biz_snapshot_table_inline
      databaseStrategy:
        standard:
          shardingColumn: db_shard_id
          shardingAlgorithmName: biz_snapshot_database_inline
  shardingAlgorithms:
    # 订单-分库算法
    orders_database_inline:
      type: INLINE
      props:
        # 分库算法表达式
        algorithm-expression: jzo2o-orders-${user_id % 3}
        # 分库支持范围查询
        allow-range-query-with-inline-sharding: true
    # 订单-分表算法
    orders_table_inline:
      type: INLINE
      props:
        # 分表算法表达式
        algorithm-expression: orders_${(int)Math.floor(id % 10000000000 / 15000000)}
        # 允许范围查询
        allow-range-query-with-inline-sharding: true
    # 服务单-分库算法
    orders_serve_database_inline:
      type: INLINE
      props:
        # 分库算法表达式
        algorithm-expression: jzo2o-orders-${serve_provider_id % 3}
        # 允许范围查询
        allow-range-query-with-inline-sharding: true
    # 服务单-分表算法
    orders_serve_table_inline:
      type: INLINE
      props:
        # 允许范围查询
        algorithm-expression: orders_serve_${(int)Math.floor(id % 10000000000 / 15000000)}
        # 允许范围查询
        allow-range-query-with-inline-sharding: true
    # 快照-分库算法
    biz_snapshot_database_inline:
      type: INLINE
      props:
        # 分库算法表达式
        algorithm-expression: jzo2o-orders-${db_shard_id % 3}
        # 允许范围查询
        allow-range-query-with-inline-sharding: true
    # 快照-分表算法
    biz_snapshot_table_inline:
      type: INLINE
      props:
        # 允许范围查询
        algorithm-expression: biz_snapshot_${(int)Math.floor((Long.valueOf(biz_id))  % 10000000000 / 15000000)}
        # 允许范围查询
        allow-range-query-with-inline-sharding: true
  # id生成器
  keyGenerators:
    snowflake:
      type: SNOWFLAKE
- !BROADCAST
//广播,代表不做分库分表的表
  tables:
    - breach_record
    - orders_canceled
    - orders_refund
    - orders_dispatch
    - orders_seize
    - serve_provider_sync
    - state_persister
    - orders_dispatch_receive
    - undo_log
    - history_orders_sync
    - history_orders_serve_sync
props:
  sql-show: true
指定数据源

在springdatasource中指定ShardingSphereDriver作为数据源

相关推荐
Java初学者小白25 分钟前
秋招Day18 - MyBatis - 基础
java·数据库·mybatis
ALLSectorSorft1 小时前
教务管理系统学排课教务系统模块设计
数据库·sql·oracle
小云数据库服务专线2 小时前
GaussDB 数据库架构师(八) 等待事件概述-1
数据库·数据库架构·gaussdb
是Yu欸3 小时前
【浏览器插件冲突】Edge浏览器加载不出来CSDN创作者中心
java·数据库·edge
安卓开发者3 小时前
Android Room 持久化库:简化数据库操作
android·数据库
极简之美3 小时前
spring boot h2数据库无法链接问题
数据库·spring boot·oracle
SelectDB5 小时前
浩瀚深度:从 ClickHouse 到 Doris,支撑单表 13PB、534 万亿行的超大规模数据分析场景
大数据·数据库·apache
SelectDB5 小时前
公开免费!Apache Doris & SelectDB 培训与认证课程正式上线
大数据·数据库·apache
我的ID配享太庙呀6 小时前
从零开始:在 PyCharm 中搭建 Django 商城的用户注册与登录功能(轮播图+商品页-小白入门版)
数据库·python·django·sqlite·web·教育电商
大视码垛机6 小时前
协作机器人掀起工厂革命:码垛场景如何用数据重塑制造业命脉?
大数据·数据库·人工智能