博客
关于我
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 node pm2相关问题
查看>>
npm run build 失败Compiler server unexpectedly exited with code: null and signal: SIGBUS
查看>>
npm run build报Cannot find module错误的解决方法
查看>>
npm run build部署到云服务器中的Nginx(图文配置)
查看>>
npm run dev 和npm dev、npm run start和npm start、npm run serve和npm serve等的区别
查看>>
npm run dev 报错PS ‘vite‘ 不是内部或外部命令,也不是可运行的程序或批处理文件。
查看>>
npm scripts 使用指南
查看>>
npm should be run outside of the node repl, in your normal shell
查看>>
npm start运行了什么
查看>>
npm WARN deprecated core-js@2.6.12 core-js@<3.3 is no longer maintained and not recommended for usa
查看>>
npm 下载依赖慢的解决方案(亲测有效)
查看>>
npm 安装依赖过程中报错:Error: Can‘t find Python executable “python“, you can set the PYTHON env variable
查看>>
npm.taobao.org 淘宝 npm 镜像证书过期?这样解决!
查看>>
npm—小记
查看>>
npm上传自己的项目
查看>>
npm介绍以及常用命令
查看>>
NPM使用前设置和升级
查看>>
npm入门,这篇就够了
查看>>
npm切换到淘宝源
查看>>
npm切换源淘宝源的两种方法
查看>>