基于雪花算法的增强版ID生成器(基于雪花算法的增强版id生成器怎么用)

  本篇文章为你整理了基于雪花算法的增强版ID生成器(基于雪花算法的增强版id生成器怎么用)的详细内容,包含有基于雪花算法的增强版id生成器是什么 基于雪花算法的增强版id生成器怎么用 雪花算法生成id原理 雪花算法生成id长度可控吗 基于雪花算法的增强版ID生成器,希望能帮助你了解 基于雪花算法的增强版ID生成器。

   public Sequence sequence() {

   SequenceConfig sequenceConfig = new SimpleSequenceConfig();

   return new Sequence(sequenceConfig);

  

 

 

  使用序列器生成ID

  

 @Autowired

 

   private Sequence sequence;

   public long generateId() {

   return sequence.nextId();

  

 

  目前提供两个配置类

  io.github.mocreates.config.DefaultSequenceConfig

  io.github.mocreates.config.SimpleSequenceConfig

  前者需要显式地指定 workerId、datacenterId,可以结合数据库来使用,后者是利用网卡信息进行自适应

  
`node_info` varchar(512) NOT NULL,

   `gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,

   `gmt_modify` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,

   PRIMARY KEY (`id`)

  ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT=DB WorkerID Assigner for UID Generator;

  

 

 

  配置 (利用主键自增来分配workerId, 解决分布式环境下手动指定workerId的痛点)

  

 @Bean

 

   public Sequence sequence(WorkerNodeMapper workerNodeMapper) throws UnknownHostException {

   WorkerNode workerNode = new WorkerNode();

   InetAddress localHost = InetAddress.getLocalHost();

   workerNode.setNodeInfo(localHost.toString());

   workerNodeMapper.insertSelective(workerNode);

   DefaultSequenceConfig defaultSequenceConfig = new DefaultSequenceConfig();

   defaultSequenceConfig.setWorkerId(workerNode.getId());

   return new Sequence(defaultSequenceConfig);

  

 

  使用序列器生成ID

  

 @Autowired

 

   private Sequence sequence;

   public long generateId() {

   return sequence.nextId();

  

 

  JMH 性能测试

  测试机硬件情况

  MacBook Pro (13-inch, M1, 2020) 8C 16G

  Sequence 配置参数

  

 private static final DefaultSequenceConfig SEQUENCE_CONFIG = new DefaultSequenceConfig();

 

   static {

   SEQUENCE_CONFIG.setSequenceBits(22);

   SEQUENCE_CONFIG.setWorkerIdBits(0);

   SEQUENCE_CONFIG.setDatacenterIdBits(0);

   SEQUENCE_CONFIG.setTwepoch(System.currentTimeMillis());

   SEQUENCE_CONFIG.setWorkerId(0L);

   SEQUENCE_CONFIG.setDatacenterId(0L);

   private static final Sequence SEQUENCE = new Sequence(SEQUENCE_CONFIG);

  

 

  JMH参数

  

@BenchmarkMode(Mode.Throughput)

 

  @Threads(10)

  @Warmup(iterations = 3, time = 10, timeUnit = TimeUnit.SECONDS)

  @Measurement(iterations = 10, time = 10, timeUnit = TimeUnit.SECONDS)

  @State(value = Scope.Benchmark)

  @Fork(1)

  @OutputTimeUnit(TimeUnit.SECONDS)

  

 

  以上就是基于雪花算法的增强版ID生成器(基于雪花算法的增强版id生成器怎么用)的详细内容,想要了解更多 基于雪花算法的增强版ID生成器的内容,请持续关注盛行IT软件开发工作室。

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码: