【Java】SPI在Shardingsphere中的使用总结

前言

  Shardingsphere大量使用了SPI技术,提供良好的可扩展性

  参考:https://shardingsphere.apache.org/document/legacy/4.x/document/en/features/spi/

  SPI基本概念:https://www.cnblogs.com/jy107600/p/11464985.html

 

本文以ShardingKeyGenerator接口为例说明SPI在Shardingsphere中的使用

  本文的代码都可以在项目源码中找到:https://github.com/apache/shardingsphere/tree/4.1.1

 

1. ShardingKeyGenerator接口及其子类

org.apache.shardingsphere.spi.keygen.ShardingKeyGenerator
{
    Comparable<?> generateKey();
}    

ShardingKeyGenerator默认实现了四种生成主键的方式,分别是自增、自减、UUID、SnowFlake方式

org.apache.shardingsphere.shardingjdbc.orchestration.spring.fixture.IncrementKeyGenerator
org.apache.shardingsphere.shardingjdbc.orchestration.spring.fixture.DecrementKeyGenerator
org.apache.shardingsphere.core.strategy.keygen.UUIDShardingKeyGenerator
org.apache.shardingsphere.core.strategy.keygen.SnowflakeShardingKeyGenerator

2. ShardingKeyGenerator SPI的初始化

package org.apache.shardingsphere.spi.algorithm.keygen;

public final class ShardingKeyGeneratorServiceLoader extends TypeBasedSPIServiceLoader<ShardingKeyGenerator> {
    
    // 静态代码块注册ShardingKeyGenerator所有实现类
    static {
        NewInstanceServiceLoader.register(ShardingKeyGenerator.class);
    }
    
    public ShardingKey
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值