ShardingJdbcⅡ
创始人
2024-03-05 08:58:26
0

序言

在前文的基础上继续梳理一下分片的相关信息.基于shardingsphere-sharding-api:jar:5.2.1的源码,感觉ShardingJdbc的版本变动频繁且比较大cuiyaonan2000@163.com

切入口是如下的内容,吐槽下官网的API文档不太够能把事情说清楚:

分片算法

从上面的自定义分片的可选类型我们就知道了,也不是完全的自定义,还是要按照官网的要求来操作的,能选择自定义分片策略只有STANDARD,COMPLEX,HINT这3中类型的自定义策略,对应到代码上就是如下的内容;

如上的接口限制了我们能够自定分片策略的范围.如下就是ShardingJdbc内置的一些分片算法了,这些算法都是实现了上面的接口cuiyaonan2000@163.com

官网也有一个列表展示了所有的内置算法类: 数据分片 :: ShardingSphere

InlineShardingAlgorithm

 我们以STANDARD类型的InlineShardingAlgorithm算法为引子来看一下,它的实现.

首先它实现了StandardShardingAlgorithm接口,表示他是Standard类型的算法.

/** Licensed to the Apache Software Foundation (ASF) under one or more* contributor license agreements.  See the NOTICE file distributed with* this work for additional information regarding copyright ownership.* The ASF licenses this file to You under the Apache License, Version 2.0* (the "License"); you may not use this file except in compliance with* the License.  You may obtain a copy of the License at**     http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package org.apache.shardingsphere.sharding.api.sharding.standard;import org.apache.shardingsphere.sharding.spi.ShardingAlgorithm;import java.util.Collection;/*** Standard sharding algorithm.* * @param  class type of sharding value*/
public interface StandardShardingAlgorithm> extends ShardingAlgorithm {/*** Sharding.* * @param availableTargetNames available data sources or table names* @param shardingValue sharding value* @return sharding result for data source or table name*/String doSharding(Collection availableTargetNames, PreciseShardingValue shardingValue);/*** Sharding.** @param availableTargetNames available data sources or table names* @param shardingValue sharding value* @return sharding results for data sources or table names*/Collection doSharding(Collection availableTargetNames, RangeShardingValue shardingValue);
}

如上就是Standard类型的算法需要实现的接口了,通过我们小学六年级英语可以知道,这2个方法就是用来告诉程序,该选择哪个数据库或者哪个表的的主要方法cuiyaonan2000@163.com

看一下示例,我们成功运行代码后打个断点看下里面的算法

 由上可见:

  • availableTargetNames: 是可以选择数据库的名称,在我们配置文件设置的2个数据源m1和m2
  • shardingValue: 就是我们的分片字段信息,里面有针对逻辑表明cui,字段名称id,value:1597852299895095298是要插入的id的值.

同一个方法如上是针对分库的选择,如下分表的选择(就是同一个方法用来进行分库分表的选择) 

相关内容

热门资讯

保存时出现了1个错误,导致这篇... 当保存文章时出现错误时,可以通过以下步骤解决问题:查看错误信息:查看错误提示信息可以帮助我们了解具体...
汇川伺服电机位置控制模式参数配... 1. 基本控制参数设置 1)设置位置控制模式   2)绝对值位置线性模...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
表格中数据未显示 当表格中的数据未显示时,可能是由于以下几个原因导致的:HTML代码问题:检查表格的HTML代码是否正...
本地主机上的图像未显示 问题描述:在本地主机上显示图像时,图像未能正常显示。解决方法:以下是一些可能的解决方法,具体取决于问...
表格列调整大小出现问题 问题描述:表格列调整大小出现问题,无法正常调整列宽。解决方法:检查表格的布局方式是否正确。确保表格使...
不一致的条件格式 要解决不一致的条件格式问题,可以按照以下步骤进行:确定条件格式的规则:首先,需要明确条件格式的规则是...
Android|无法访问或保存... 这个问题可能是由于权限设置不正确导致的。您需要在应用程序清单文件中添加以下代码来请求适当的权限:此外...
【NI Multisim 14...   目录 序言 一、工具栏 🍊1.“标准”工具栏 🍊 2.视图工具...
银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...