关于数据库:ShardingSphere-知识库更新-官方样例集助你快速上手

10次阅读

共计 7696 个字符,预计需要花费 20 分钟才能阅读完成。

Apache ShardingSphere 作为 Apache 顶级我的项目,是数据库畛域最受欢迎的开源我的项目之一。通过 5 年多的倒退,ShardingSphere 已取得超 14K Stars 的关注,270+ 贡献者,建设起了沉闷的社区生态。

随着我的项目的蓬勃发展,版本的一直更迭,Apache ShardingSphere 反对的个性逐步增多,性能日益弱小,配置规定也在一直优化。为了帮忙用户更好地了解各项个性和配置规定,不便用户疾速测试并运行相干性能组件,找到最佳实现,shardingsphere-example 我的项目应运而生。

shardingsphere-example 是一个独立的 Maven 我的项目,位于 Apache ShardingSphere 我的项目的 examples 目录下。我的项目地址:
https://github.com/apache/sha…


江龙滔
SphereEx 中间件研发工程师,Apache ShardingSphere contributor。目前专一于 ShardingSphere 数据库中间件研发及开源社区建设。


侯阳
SphereEx 中间件研发工程师,目前从事 ShardingSphere 数据库中间件研发,酷爱开源,心愿同大家一起建设更好的社区。

模块详解

shardingsphere-example 我的项目蕴含多个模块,将为用户带来程度拆分、读写拆散、分布式治理、分布式事务、数据加密、强制路由、影子库等性能的应用及配置样例,笼罩 Java API、YAML、Spring Boot、Spring Namespace 等多种业务罕用的接入状态。除了 ShardingSphere-JDBC,shardingsphere-example 中还减少了 ShardingSphere-Proxy 和 ShardingSphere-Parser 的应用案例。

所有波及到 Apache ShardingSphere 的性能个性、接入场景以及各种灵便的配置形式,都能够在官网的 repo 里找到样例,不便用户查问和参考。下表展现了 shardingsphere-example 的模块散布状况:

shardingsphere-example
  ├── example-core
  │   ├── config-utility
  │   ├── example-api
  │   ├── example-raw-jdbc
  │   ├── example-spring-jpa
  │   └── example-spring-mybatis
  ├── shardingsphere-jdbc-example
  │   ├── sharding-example
  │   │   ├── sharding-raw-jdbc-example
  │   │   ├── sharding-spring-boot-jpa-example
  │   │   ├── sharding-spring-boot-mybatis-example
  │   │   ├── sharding-spring-namespace-jpa-example
  │   │   └── sharding-spring-namespace-mybatis-example
  │   ├── governance-example
  │   │   ├── governance-raw-jdbc-example
  │   │   ├── governance-spring-boot-mybatis-example
  │   │   └── governance-spring-namespace-mybatis-example
  │   ├── transaction-example
  │   │   ├── transaction-2pc-xa-atomikos-raw-jdbc-example
  │   │   ├── transaction-2pc-xa-bitronix-raw-jdbc-example
  │   │   ├── transaction-2pc-xa-narayana-raw-jdbc-example
  │   │   ├── transaction-2pc-xa-spring-boot-example
  │   │   ├── transaction-2pc-xa-spring-namespace-example
  │   │   ├── transaction-base-seata-raw-jdbc-example       
  │   │   └── transaction-base-seata-spring-boot-example
  │   ├── other-feature-example
  │   │   ├── encrypt-example
  │   │   │   ├── encrypt-raw-jdbc-example
  │   │   │   ├── encrypt-spring-boot-mybatis-example
  │   │   │   └── encrypt-spring-namespace-mybatis-example
  │   │   ├── hint-example
  │   │   │   └── hint-raw-jdbc-example
  │   │   └── shadow-example
  │   │   │   ├── shadow-raw-jdbc-example
  │   │   │   ├── shadow-spring-boot-mybatis-example
  │   │   │   └── shadow-spring-namespace-mybatis-example
  │   ├── extension-example
  │   │   └── custom-sharding-algortihm-example
  ├── shardingsphere-parser-example
  ├── shardingsphere-proxy-example
  │   ├── shardingsphere-proxy-boot-mybatis-example
  │   └── shardingsphere-proxy-hint-example
  └── src/resources
        └── manual_schema.sql

1. example-core

example 外围模块,蕴含实体、接口定义和其余专用代码。

2. shardingsphere-jdbc-example

ShardingSphere-JDBC 示例模块,展现 ShardingSphere-JDBC 的性能个性和各种应用形式。

(1)sharding-example

展现如何应用 ShardingSphere-JDBC 进行数据分片,蕴含分库、分表、分库 + 分表、读写拆散、读写拆散 + 分库分表的利用场景。在 ORM 集成方面,本模块也贴心的为用户提供了 MyBatis 和 JPA 的集成样例。

(2)governance-example

展现 ShardingSphere-JDBC 在分布式治理方面的利用,蕴含了分库分表、读写拆散、数据加密、影子库等个性与分布式治理相结合的利用场景。

留神:分布式治理 example 依赖 Apache ZooKeeper,请自行部署。

(3)transaction-example

展现 ShardingSphere-JDBC 反对的多种分布式事务管理形式,用户能够依据利用场景抉择适宜的分布式事务管理器进行应用。鉴于分布式事务的特殊性,本模块的示例都是基于分库、分表或分库 + 分表的场景设计的。

留神:Seata 事务管理器须要自行部署。

(4)other-feature-example

ShardingSphere-JDBC 其余性能个性的示例,目前蕴含了 encrypt(数据加密)、hint(强制路由)、shadow(影子库)几种类型。

① encrypt-example

数据加密性能示例,同样蕴含了 Java API、YAML、Spring Boot、Spring Namespace 等几种接入形式的样例。

② hint-example

强制路由性能示例,目前只提供了 YAML 配置形式的案例,更多场景欢送补充。

③ shadow-example

影子库性能示例,蕴含了影子库个性与数据加密、分库分表、读写拆散等个性联合的利用样例。

(5)extension-example

本模块展现 ShardingSphere-JDBC 的自定义扩大能力,用户能够通过 SPI 或 ShardingSphere 提供的其余形式进行性能扩大。

① custom-sharding-algortihm-example

展现了如何通过 ‘CLASS_BASED’ 形式进行自定义分片算法的扩大。

3. shardingsphere-parser-example

SQLParserEngine 是 Apache ShardingSphere 定制的 SQL 解析引擎,也是 ShardingSphere-JDBC 和 ShardingSphere-Proxy 的能力根底。用户输出的 SQL 文本通过 SQLParserEngine 解析成能够辨认的语法对象,之后能力进行路由、改写等加强操作。

从 5.0.0-alpha 版本开始,Apache ShardingSphere 将 SQL 解析这一外围能力凋谢给用户,用户能够通过 API 调用 SQLParserEngine,在本人的利用零碎中进行高效的 SQL 解析,满足更多个性化的业务须要。

本模块展现了 SQLParserEngine API 的应用形式,笼罩了 MySQL、PostgreSQL、Oracle、SQLServer 以及 SQL92 等各种语法模式。

4. shardingsphere-proxy-example

ShardingSphere-Proxy 示例模块,蕴含了分库分表、读写拆散和强制路由等罕用场景的配置样例。因为 ShardingSphere-Proxy 与 ShardingSphere-JDBC 在性能个性的反对度上大体雷同,未列举的示例也能够对照 shardingsphere-jdbc-example 进行参考。

(1)shardingsphere-proxy-boot-mybatis-example

展现了通过 Proxy 配置数据分片,并应用 SpringBoot + MyBatis 的形式进行数据拜访的场景示例。

(2)shardingsphere-proxy-hint-example

展现了通过 Proxy 配置强制路由,并应用 Java 客户端进行数据拜访的场景示例。

近期优化

在 Apache ShardingSphere 5.0.0-beta 版本公布之际,社区贡献者对 shardingsphere-example 也进行了降级和优化,次要包含:

JDK 版本升级
组件版本升级
类命名优化
配置文件优化
SQL 脚本优化

以下是降级相干的具体内容:

JDK 版本升级

在以 Java 作为次要语言的业余开发者中,Java 8 LTS(长期反对版本)依然是最受欢迎的版本。


起源《JetBrains 公司 2020 对于 Java 的报告》:https://blog.jetbrains.com/zh…

shardingsphere-example 降级当前要求 Java 8 作为最低版本。如果您以后应用的是 Java 7 或更早版本,则须要先降级 JDK。

Spring 依赖降级

shardingsphere-example 对 Spring 相干组件进行降级。
spring-boot version 由 1.5.17 降级到 2.0.9.RELEASE
springframework version 由 4.3.20.RELEASE 降级到 5.0.13.RELEASE
mybatis-spring-boot-start version 由 1.3.0 降级到 2.0.1
mybatis-spring version 由 1.3.0 降级到 2.0.1

长久层框架降级

shardingsphere-example 对长久层框架 MyBatis 和 Hibernate 进行了降级。
mybatis version 由 3.4.2 降级到 3.5.1
hibernate version 由 4.3.11.Final 降级到 5.2.18.Final

数据库连接池降级

shardingsphere-example 对数据库连接池 HikariCP 进行了降级。
HikariCP artifactId 由 HikariCP-java7 降级到 HikariCP
HikariCP version 由 2.4.11 降级到 3.4.2

数据库驱动降级

shardingsphere-example 对 MySQL 和 PostgreSQL 连贯驱动进行了降级。
mysql-connector-java version 由 5.1.42 降级到 5.1.47
postgresql version 由 42.2.5.jre7 降级到 42.2.5

Example 运行示例

从这里开始,咱们将通过几个典型场景来阐明如何配置和运行 example。

因为 shardingsphere-example 我的项目模块泛滥,本次筛选几个关注度较高的 ShardingSphere-JDBC 利用场景来举例说明。

前置筹备

  1. shardingsphere-example 应用 Maven 作为构建工具,请提前准备 Maven 环境;
  2. 筹备 Apache ShardingSphere,如果你的设施中尚未装置 Apache ShardingSphere,能够依照如下形式进行下载和编译:
## 克隆 Apache ShardingSphere 我的项目
git clone https://github.com/apache/shardingsphere.git
## 编译源代码
cd shardingsphere
mvn clean install -Prelease
  1. 将 shardingsphere-example 我的项目导入本人的 IDE 中;
  2. 筹备一个可治理的数据库环境,例如本地的 MySQL 实例;
  3. 如需运行读写拆散测试,请确保数据库的主从同步机制工作失常;
  4. 执行数据库初始化脚本:examples/src/resources/manual_schema.sql

场景示例

sharding-spring-boot-mybatis-example「分库分表场景」

1. 模块门路

examples/shardingsphere-jdbc-example/sharding-example/sharding-spring-boot-mybatis-example

2. 场景指标

本示例展现 ShardingSphere-JDBC 联合 SpringBoot 和 MyBatis 进行分库分表的利用场景。此次分片的指标是 2 库 4 表,行将一张逻辑表拆分为 4 个分片,平均保留在 2 个不同的数据库中。

3. 运行筹备

配置 application.properties
将 spring.profiles.active 设置为 sharding-databases-tables
配置 application-sharding-databases-tables.properties
jdbc-url 批改为本人的数据库地址,并配置正确的用户名明码等信息
spring.shardingsphere.props.sql-show 属性设置为 true

具体配置阐明请浏览配置手册:
https://shardingsphere.apache…

4. 启动运行

运行启动类:ShardingSpringBootMybatisExample.java

此时即可通过日志中的「Logic SQL」和「Actual SQL」察看每一条 SQL 语句的路由状况,了解分库分表的运行机制。

sharding-raw-jdbc-example「读写拆散场景」

1. 模块门路

examples/shardingsphere-jdbc-example/sharding-example/sharding-raw-jdbc-example

2. 场景指标

本示例展现如何应用 YAML 配置 ShardingSphere-JDBC 的读写拆散性能。此次演示的场景是一个写库 + 两个读库的拆散配置。

3. 运行筹备

配置 META-INF/readwrite-splitting.yaml

jdbc-url 批改为本人的数据库地址,并配置正确的用户名明码等信息

props.sql-show 属性设置为 true

具体配置阐明请浏览配置手册:
https://shardingsphere.apache…

4. 启动运行

关上启动类:ShardingRawYamlConfigurationExample.java,将 shardingType 设置为 ShardingType.READWRITE_SPLITTING,并启动运行。

此时即可通过日志中的「Logic SQL」和「Actual SQL」察看每一条 SQL 语句的路由状况,了解读写拆散的运行机制。

留神:如果主从数据库无奈失常同步,将会产生查问异样。

custom-sharding-algortihm-example「自定义算法场景」

1. 模块门路:

examples/shardingsphere-jdbc-example/extension-example/custom-sharding-algortihm-example/class-based-sharding-algorithm-example

2. 场景指标

本示例展现如何应用 CLASS_BASED 形式进行自定义算法扩大,让 ShardingSphere-JDBC 在进行分片路由时,应用用户提供的算法来计算分片后果。此次演示的场景是应用自定义分片算法进行分库。

  1. 运行筹备
    筹备一个自定义的分片算法,该算法应依据利用须要,实现
    StandardShardingAlgorithm、ComplexKeysShardingAlgorithmHintShardingAlgorithm 其中一个接口,如示例中提供的 ClassBasedStandardShardingAlgorithmFixture

配置 META-INF/sharding-databases.yaml

jdbc-url 批改为本人的数据库地址,并配置正确的用户名明码等信息
props.sql-show 属性设置为 true
留神 shardingAlgorithms 配置项,当 typeCLASS_BASED 时,能够通过 props 指定自定义算法的类别和全门路,这样就能够实现自定义算法的配置。

具体配置阐明请浏览配置手册:
https://shardingsphere.apache…

4. 启动运行

运行启动类:

YamlClassBasedShardingAlgorithmExample.java

此时即可通过日志察看分库运行状况,并能够通过 DEBUG 等形式查看自定义算法的输入输出是否合乎预期。

结语

以上就是本次知识库分享的全部内容。无关 ShardingSphere-JDBC、ShardingSphere-Proxy 和 ShardingSphere-Parser 的示例将在将来持续为大家分享。如果读者有感兴趣的场景需要,或是发现了新的问题、新的晋升点,欢送在 GitHub issue 列表提出倡议,也可提交 Pull Request 参加到开源社区,为世界级的我的项目贡献力量。

GitHub issue:
https://github.com/apache/sha…
奉献指南:
https://shardingsphere.apache…

往期精彩请点击原文链接。

欢送扫码关注咱们

正文完
 0