Sharding-Sphere 学习笔记

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
云数据库 RDS MySQL Serverless,价值2615元额度,1个月
简介: Sharding-Sphere 学习笔记

Sharding-Sphere 学习笔记

请在此添加图片描述

请在此添加图片描述

Sharding-Sphere 是一个开源的分布式数据库中间件,提供了分库分表、读写分离、分布式事务等功能,支持 MySQL、Oracle、SQL Server 等主流数据库。本文将介绍 Sharding-Sphere 的使用方法和代码示例。

一、Sharding-Sphere 的核心组件

Sharding-Sphere 的核心组件包括三个部分:Sharding-JDBC、Sharding-Proxy 和 Sharding-Sidecar。

  1. Sharding-JDBC

Sharding-JDBC 是 Sharding-Sphere 的核心组件,用于实现分库分表和读写分离。它是一个 JDBC 驱动,可以直接替换应用程序中原本使用的 JDBC 驱动,从而实现对数据库的透明访问。

  1. Sharding-Proxy

Sharding-Proxy 是一个基于 MySQL 协议的数据库代理,可以将请求路由到多个实际的数据库节点上。它可以实现分库分表、读写分离、分布式事务等功能。

  1. Sharding-Sidecar

Sharding-Sidecar 是一个基于 Envoy 的数据库代理,可以将请求路由到多个实际的数据库节点上。它可以实现分库分表、读写分离、分布式事务等功能。

请在此添加图片描述

二、Sharding-JDBC 的使用方法

  1. 引入依赖

在 Maven 中引入 Sharding-JDBC 的依赖:

<dependency>
    <groupId>io.shardingsphere</groupId>
    <artifactId>sharding-jdbc-core</artifactId>
    <version>4.2.2</version>
</dependency>
  1. 配置数据源

在 application.yml 中配置数据源:

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test
    username: root
    password: root
  1. 配置分库分表规则

在 application.yml 中配置分库分表规则:

sharding:
  tables:
    order:
      actualDataNodes: ds${0..1}.order_${0..1}
      tableStrategy:
        inline:
          shardingColumn: order_id
          algorithmExpression: order_${order_id % 2}
      keyGenerateStrategy:
        column: order_id
        keyGeneratorName: snowflake
  bindingTables:
    - order
  defaultDatabaseStrategy:
    inline:
      shardingColumn: user_id
      algorithmExpression: ds${user_id % 2}
  defaultTableStrategy:
    none:
  1. 使用 Sharding-JDBC

在 Java 代码中使用 Sharding-JDBC:

@Autowired
DataSource dataSource;

public void example() {
    try (Connection conn = dataSource.getConnection();
         PreparedStatement ps = conn.prepareStatement("insert into order (order_id, user_id) values (?, ?)")) {
        ps.setLong(1, 1L);
        ps.setLong(2, 1L);
        ps.executeUpdate();
    }
}

以上代码将会将数据插入到分布式的 order 表中,Sharding-JDBC 会自动将数据路由到对应的数据库节点。

请在此添加图片描述

三、Sharding-Proxy 的使用方法

  1. 引入依赖

在 Maven 中引入 Sharding-Proxy 的依赖:

<dependency>
    <groupId>io.shardingsphere</groupId>
    <artifactId>sharding-proxy-backend</artifactId>
    <version>4.2.2</version>
</dependency>
  1. 配置数据源

在 application.yml 中配置数据源:

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test
    username: root
    password: root
  1. 配置分库分表规则

在 application.yml 中配置分库分表规则:

sharding:
  tables:
    order:
      actualDataNodes: ds${0..1}.order_${0..1}
      tableStrategy:
        inline:
          shardingColumn: order_id
          algorithmExpression: order_${order_id % 2}
      keyGenerateStrategy:
        column: order_id
        keyGeneratorName: snowflake
  bindingTables:
    - order
  defaultDatabaseStrategy:
    inline:
      shardingColumn: user_id
      algorithmExpression: ds${user_id % 2}
  defaultTableStrategy:
    none:
  1. 启动 Sharding-Proxy

请在此添加图片描述

在 Java 代码中启动 Sharding-Proxy:

public void startProxy() {
    ProxyConfiguration proxyConfiguration = new ProxyConfiguration();
    proxyConfiguration.setFrontendConfiguration(new FrontendConfiguration("jdbc", new InetSocketAddress("127.0.0.1", 3307)));
    proxyConfiguration.setBackendConfiguration(new BackendConfiguration(Collections.singletonList(new DataSourceConfiguration(
            "test", "com.mysql.jdbc.Driver", "jdbc:mysql://localhost:3306/test", "root", "root")), new RuleConfiguration(Collections.singletonList(
            new ShardingRuleConfiguration(Collections.singletonList(new TableRuleConfiguration("order", "ds${0..1}.order_${0..1}")), new ShardingStrategyConfiguration(
            "user_id", "ds${user_id % 2}", "order_id", "order_${order_id % 2}", new StandardShardingAlgorithmConfiguration("MODULO")))))));
    ShardingProxy shardingProxy = new ShardingProxy();
    shardingProxy.start(proxyConfiguration);
}

以上代码将会在本地启动一个 Sharding-Proxy 服务,监听在 127.0.0.1:3307 上。应用程序可以通过 JDBC 连接到该地址,Sharding-Proxy 会将请求路由到对应的数据库节点。

四、Sharding-Sidecar 的使用方法

  1. 引入依赖

在 Maven 中引入 Sharding-Sidecar 的依赖:

<dependency>
    <groupId>io.shardingsphere</groupId>
    <artifactId>sharding-sidecar</artifactId>
    <version>4.2.2</version>
</dependency>
  1. 配置数据源

在 application.yml 中配置数据源:

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test
    username: root
    password: root
  1. 配置分库分表规则

在 application.yml 中配置分库分表规则:

sharding:
  tables:
    order:
      actualDataNodes: ds${0..1}.order_${0..1}
      tableStrategy:
        inline:
          shardingColumn: order_id
          algorithmExpression: order_${order_id % 2}
      keyGenerateStrategy:
        column: order_id
        keyGeneratorName: snowflake
  bindingTables:
    - order
  defaultDatabaseStrategy:
    inline:
      shardingColumn: user_id
      algorithmExpression: ds${user_id % 2}
  defaultTableStrategy:
    none:
  1. 启动 Sharding-Sidecar

在 Java 代码中启动 Sharding-Sidecar:

public void startSidecar() {
    SidecarConfiguration sidecarConfiguration = new SidecarConfiguration();
    sidecarConfiguration.setServicePort(9090);
    sidecarConfiguration.setAdminPort(9091);
    sidecarConfiguration.setProxyConfiguration(new ProxyConfiguration(Collections.singletonList(new DataSourceConfiguration(
            "test", "com.mysql.jdbc.Driver", "jdbc:mysql://localhost:3306/test", "root", "root")), new RuleConfiguration(Collections.singletonList(
            new ShardingRuleConfiguration(Collections.singletonList(new TableRuleConfiguration("order", "ds${0..1}.order_${0..1}")), new ShardingStrategyConfiguration(
            "user_id", "ds${user_id % 2}", "order_id", "order_${order_id % 2}", new StandardShardingAlgorithmConfiguration("MODULO")))))));
    ShardingSidecar shardingSidecar = new ShardingSidecar();
    shardingSidecar.start(sidecarConfiguration);
}

以上代码将会在本地启动一个 Sharding-Sidecar 服务,监听在 9090 端口上。应用程序可以通过 Envoy 连接到该地址,Sharding-Sidecar 会将请求路由到对应的数据库节点。

五、总结

本文介绍了 Sharding-Sphere 的核心组件和使用方法,并提供了代码示例。Sharding-Sphere 是一个功能强大的分布式数据库中间件,可以大大简化数据库的管理和维护,提高系统的可扩展性和可靠性。如需了解更多信息,请参考官方文档。

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
SQL 关系型数据库 MySQL
ShardingSphere-Sharding-Proxy(安装和分表配置)| 学习笔记
快速学习ShardingSphere-Sharding-Proxy(安装和分表配置)。
875 0
ShardingSphere-Sharding-Proxy(安装和分表配置)| 学习笔记
|
中间件 数据库 开发者
ShardingSphere-Sharding-JDBC水平分库(2)| 学习笔记
快速学习ShardingSphere-Sharding-JDBC水平分库(2)。
80 0
ShardingSphere-Sharding-JDBC水平分库(2)| 学习笔记
|
中间件 Java 数据库连接
ShardingSphere-Sharding-JDBC (垂直切分)| 学习笔记
快速学习ShardingSphere-Sharding-JDBC (垂直切分)。
107 0
ShardingSphere-Sharding-JDBC (垂直切分)| 学习笔记
|
SQL 算法 Java
ShardingSphere-Sharding-JDBC水平分库(1) | 学习笔记
快速学习ShardingSphere-Sharding-JDBC水平分库(1)。
147 0
ShardingSphere-Sharding-JDBC水平分库(1) | 学习笔记
|
SQL druid 算法
ShardingSphere-Sharding-JDBC水平分表(分片策略) | 学习笔记
快速学习ShardingSphere-Sharding-JDBC水平分表(分片策略)。
518 0
ShardingSphere-Sharding-JDBC水平分表(分片策略) | 学习笔记
|
SQL 算法 druid
Sharding-Sphere实现多租户架构
Sharding-Sphere实现多租户架构
1237 0
Sharding-Sphere实现多租户架构
|
存储 SQL Java
基于 Sharding Sphere,实现数据 “一键脱敏”!
在真实业务场景中,数据库中经常需要存储某些客户的关键性敏感信息如:身份证号、银行卡号、姓名、手机号码等,此类信息按照合规要求,通常需要实现加密存储以满足合规要求。
基于 Sharding Sphere,实现数据 “一键脱敏”!
|
开发框架 Java 应用服务中间件
sharding-sphere中的xa事务
Apache ShardingSphere 默认的 XA 事务管理器为 Atomikos。
1312 0
sharding-sphere中的xa事务
|
存储 Web App开发 关系型数据库
PostgreSQL PostGIS point join polygon (by ST_xxxx) - pglz_decompress 性能优化
标签 PostgreSQL , postgis , pglz_decompress , perf , tuning 背景 在空间数据中,通常会有轨迹、点、面的数据,假设有两张表,一张为面的表,一张为点的表,使用包含 ST_xxxx(c.geom, p.geom) 来进行JOIN(例如以面为单位,聚合统计点的数量)。
1469 0