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

本文共 2329 字,大约阅读时间需要 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,ds1
spring.shardingsphere.datasource.ds0.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ds0.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds0.url=jdbc:mysql://localhost:3306/ds0
spring.shardingsphere.datasource.ds0.username=root
spring.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_id
spring.shardingsphere.sharding.tables.t_order.table-strategy.inline.algorithm-expression=t_order_$->{order_id % 2}
spring.shardingsphere.sharding.tables.t_order.key-generator.type=SNOWFLAKE
spring.shardingsphere.sharding.tables.t_order.key-generator.column=order_id
spring.shardingsphere.sharding.tables.t_order.database-strategy.inline.sharding-column=user_id
spring.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/

你可能感兴趣的文章
nnU-Net 终极指南
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>
No qualifying bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs<?>‘ available
查看>>
No resource identifier found for attribute 'srcCompat' in package的解决办法
查看>>
no session found for current thread
查看>>