• ShardingJdbcⅡ


    序言

    在前文的基础上继续梳理一下分片的相关信息.基于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类型的算法.

    1. /*
    2. * Licensed to the Apache Software Foundation (ASF) under one or more
    3. * contributor license agreements. See the NOTICE file distributed with
    4. * this work for additional information regarding copyright ownership.
    5. * The ASF licenses this file to You under the Apache License, Version 2.0
    6. * (the "License"); you may not use this file except in compliance with
    7. * the License. You may obtain a copy of the License at
    8. *
    9. * http://www.apache.org/licenses/LICENSE-2.0
    10. *
    11. * Unless required by applicable law or agreed to in writing, software
    12. * distributed under the License is distributed on an "AS IS" BASIS,
    13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14. * See the License for the specific language governing permissions and
    15. * limitations under the License.
    16. */
    17. package org.apache.shardingsphere.sharding.api.sharding.standard;
    18. import org.apache.shardingsphere.sharding.spi.ShardingAlgorithm;
    19. import java.util.Collection;
    20. /**
    21. * Standard sharding algorithm.
    22. *
    23. * @param class type of sharding value
    24. */
    25. public interface StandardShardingAlgorithmextends Comparable> extends ShardingAlgorithm {
    26. /**
    27. * Sharding.
    28. *
    29. * @param availableTargetNames available data sources or table names
    30. * @param shardingValue sharding value
    31. * @return sharding result for data source or table name
    32. */
    33. String doSharding(Collection availableTargetNames, PreciseShardingValue shardingValue);
    34. /**
    35. * Sharding.
    36. *
    37. * @param availableTargetNames available data sources or table names
    38. * @param shardingValue sharding value
    39. * @return sharding results for data sources or table names
    40. */
    41. Collection doSharding(Collection availableTargetNames, RangeShardingValue shardingValue);
    42. }

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

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

     由上可见:

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

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

  • 相关阅读:
    Hive 中的各种常用set设置
    阿里云效 + jenkins配置自动构建测试环境
    node.js,vue-cli,webpack-cli安装教程(集合和完善别人的文章,解决了按照他文章出现的问题的解决方法,学生必备)
    ZooKeeper 8:请求处理逻辑与源码分析
    Gradle系列——常用指令,修改gradle源,Wrapper包装器(源于文档7.5版本,SpringBoot使用)day1-2
    转载)word输出高分辨PDF并且有链接跳转功能
    货币系统(求方案数的背包)
    UE4 回合游戏项目 19- 添加血量UI
    10 nginx 中的 slab
    开放智慧,助力学习——电大搜题,打开学无止境的新篇章
  • 原文地址:https://blog.csdn.net/cuiyaonan2000/article/details/128115947