博客
关于我
Sharding-JDBC 快速入门第一课
阅读量:416 次
发布时间:2019-03-06

本文共 2303 字,大约阅读时间需要 7 分钟。

ShardingSphere-JDBC技术文档

ShardingSphere是一套开源的分布式数据库中间件解决方案组成的生态圈,由Sharding-JDBC、Sharding-Proxy和Sharding-Sidecar(计划中)三个独立产品组成。这些产品提供标准化的数据分片、分布式事务和数据库治理功能,适用于Java同构、异构语言、云原生等多样化场景。

ShardingSphere-JDBC概述

Sharding-JDBC定位为轻量级Java框架,在JDBC层提供增强服务。它以客户端直连数据库的形式提供服务,无需额外部署和依赖,兼容JDBC和各种ORM框架。适用于任何基于JDBC的ORM框架,如JPA、Hibernate、Mybatis、Spring JDBC Template或直接使用JDBC。

功能特点

  • 支持任意数据库连接池:如DBCP、C3P0、BoneCP、Druid、HikariCP等。
  • 支持任意实现JDBC规范的数据库:如MySQL、Oracle、SQLServer、PostgreSQL及遵循SQL92标准的数据库。
  • 灵活的分片策略:支持5种分片策略,可通过行表达式进行灵活配置。

数据分片

在性能和运维成本方面,分库分表是解决集中式数据库瓶颈的重要手段。ShardingSphere支持两种主要分片方式:

  • 垂直分片:根据业务拆分,将多个表分布到不同数据库,专库专用。
  • 水平分片:通过主键或其他字段进行数据水平分散,解决单机处理能力限制。

分片策略与实现

  • 核心概念

    • 数据节点:分片的最小单元,由数据源名称和表名组成,如ds_0.t_order_0
    • 分片键:用于分片的字段,决定数据分布的规则。
    • 分片算法:支持=>=<=><BETWEENIN等操作。
    • 行表达式:通过配置表达式定义分片规则,支持范围区间和枚举值。
  • 雪花算法:默认用于主键生成,确保分布式环境下的唯一性和有序性。

读写分离

读写分离可以提升系统吞吐量,但也带来数据一致性挑战。合理搭配分库分表与读写分离,需要仔细设计架构和运维流程。

示例配置

以下是基于Spring Boot的配置示例:

# 数据源配置spring.shardingsphere.datasource.names=ds0,ds1spring.shardingsphere.datasource.ds0.type=com.alibaba.druid.pool.DruidDataSourcespring.shardingsphere.datasource.ds0.driver-class-name=com.mysql.jdbc.Driverspring.shardingsphere.datasource.ds0.url=jdbc:mysql://localhost:3306/ds0spring.shardingsphere.datasource.ds0.username=rootspring.shardingsphere.datasource.ds0.password=123456# 数据表规则配置spring.shardingsphere.sharding.tables.t_order.actual-data-nodes=ds$->{0..1}.t_order_$->{0..1}spring.shardingsphere.sharding.tables.t_order.table-strategy.inline.sharding-column=order_idspring.shardingsphere.sharding.tables.t_order.table-strategy.inline.algorithm-expression=t_order_$->{order_id % 2}spring.shardingsphere.sharding.tables.t_order.key-generator.type=SNOWFLAKEspring.shardingsphere.sharding.tables.t_order.key-generator.column=order_idspring.shardingsphere.sharding.tables.t_order.database-strategy.inline.sharding-column=user_idspring.shardingsphere.sharding.tables.t_order.database-strategy.inline.algorithm-expression=ds$->{user_id % 2}

工程结构

/src/  main/    java/      com/        cjs/          example/            sharding/              entity/              repository/              service/              controller/    resources/      application.properties

注意事项

  • 分库分表:需要谨慎设计,避免全表扫描。
  • 读写分离:需确保数据一致性,避免并发问题。
  • 数据库选择:建议使用支持分片的数据库,如MySQL、PostgreSQL。

通过合理配置和使用ShardingSphere-JDBC,可以有效解决高并发和大数据存储的性能瓶颈问题。

转载地址:http://xmqkz.baihongyu.com/

你可能感兴趣的文章
npm切换源淘宝源的两种方法
查看>>
npm前端包管理工具简介---npm工作笔记001
查看>>
npm包管理深度探索:从基础到进阶全面教程!
查看>>
npm升级以及使用淘宝npm镜像
查看>>
npm发布包--所遇到的问题
查看>>
npm发布自己的组件UI包(详细步骤,图文并茂)
查看>>
npm和package.json那些不为常人所知的小秘密
查看>>
npm和yarn清理缓存命令
查看>>
npm和yarn的使用对比
查看>>
npm如何清空缓存并重新打包?
查看>>
npm学习(十一)之package-lock.json
查看>>
npm安装 出现 npm ERR! code ETIMEDOUT npm ERR! syscall connect npm ERR! errno ETIMEDOUT npm ERR! 解决方法
查看>>
npm安装crypto-js 如何安装crypto-js, python爬虫安装加解密插件 找不到模块crypto-js python报错解决丢失crypto-js模块
查看>>
npm安装教程
查看>>
npm报错Cannot find module ‘webpack‘ Require stack
查看>>
npm报错Failed at the node-sass@4.14.1 postinstall script
查看>>
npm报错fatal: Could not read from remote repository
查看>>
npm报错File to import not found or unreadable: @/assets/styles/global.scss.
查看>>
npm报错TypeError: this.getOptions is not a function
查看>>
npm报错unable to access ‘https://github.com/sohee-lee7/Squire.git/‘
查看>>