Shardingsphere 学习手册

196 阅读3分钟

使用准备

  网上有很多与Springboot结合使用的示例,需要对应具体的版本,官方针对每个版本都提供了使用示例,建议下载源码,生成官方的DEMO来学习使用.如果想要与druid或多数据源使用,看完官方的DEMO之后,也就知道怎么使用了,尤其是配置.

官方Spring boot 接入手册(不推荐)

  最新的版本已没有官方的接入手册,这个文档是5.1.1版本的,接入方式最好使用示例中自动生成的代码
  官方手册5.1.1版本

image.png

源码下载

github.com/apache/shar…

编译

需要修改IDEA配置或执行Maven命令后,然后重启IDEA.具体原因参见

IDEA 配置修改

idea.max.intellisense.filesize=10000

image.png

mvnw命令

./mvnw -DskipITs -DskipTests install -T1C

examples加入项目

image.png

image.png

image.png

生成示例

image.png

示例代码

image.png

开始使用

Spring boot 数据分片

  使用查看 sharding--spring-boot-starter-mybatis--standalone--local 这个示例项目,我学习的背景是,数据需要根据年月来分片,分片字段是创建时间,码云代码

配置资料

INLINE 表达式学习

actualDataNodes 数值需要使用INLINE表达式 官方文档

${}与$->{}区别

image.png

数据分片算法学习

algorithm-expression需要使用到 官方文档

遇到的问题

snakeyaml问题

Spring boot ,dynamic-datasource 多数据源结合使用会遇到

Correct the classpath of your application so that it contains a single, compatible version of org.apache.shardingsphere.infra.
    util.yaml.constructor.ShardingSphereYamlConstructor$1
<!--
这里一定要设置yml版本,否则在shardingsphere 5.2 版本及以上会出现如下问题:
    "Correct the classpath of your application so that it contains a single, compatible version of org.apache.shardingsphere.infra.
    util.yaml.constructor.ShardingSphereYamlConstructor$1"

因为需要的解析yaml文件的库版本不满足框架要求,根据不同的springboot版本做出调整。

我这里使用的是springboot 2.6.8, 需要手动依赖`snakeyarml`并指定版本才可以。
如果你使用的是更高版本的springboot,pom文件中的properties位置调整yaml版本号即可。Springboot框架自带了这个框架,不需要重新引用

具体以你使用的springboot 版本为准进行调整就好了。
-->
<dependency>
    <groupId>org.yaml</groupId>
    <artifactId>snakeyaml</artifactId>
    <version>1.33</version>
</dependency>

原文链接:blog.csdn.net/panyongjie2…

使用Join报错All tables must be in the same datasource

  原因在于使用join的查询中,分片表和非分片表进行了Join查询,解决方案如下:

  1. 将非分片表进行广播,非分片表数据量在10万以下,可以这么操作

image.png 2. 将非分片表进行业务剥离,在代码中进行查询,然后拼接到结果集中,推荐

  1. 非分片表加入规则

image.png

  1. 使用Hit强制路由,不推荐