学习skywalking的源码,发现skywalking的全局ID生成值得借鉴,在这里分享出来,大家一起学习

/** 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.skywalking.apm.agent.core.context.ids;import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;import org.apache.skywalking.apm.util.StringUtil;public final class GlobalIdGenerator {private static final String PROCESS_ID = UUID.randomUUID().toString().replaceAll("-", "");private static final ThreadLocal<IDContext> THREAD_ID_SEQUENCE = ThreadLocal.withInitial(() -> new IDContext(System.currentTimeMillis(), (short) 0));private GlobalIdGenerator() {}/*** Generate a new id, combined by three parts.* <p>* The first one represents application instance id.* <p>* The second one represents thread id.* <p>* The third one also has two parts, 1) a timestamp, measured in milliseconds 2) a seq, in current thread, between* 0(included) and 9999(included)** @return unique id to represent a trace or segment*/public static String generate() {return StringUtil.join('.',PROCESS_ID,String.valueOf(Thread.currentThread().getId()),String.valueOf(THREAD_ID_SEQUENCE.get().nextSeq()));}private static class IDContext {private long lastTimestamp;private short threadSeq;// Just for considering time-shift-back only.private long runRandomTimestamp;private int lastRandomValue;private IDContext(long lastTimestamp, short threadSeq) {this.lastTimestamp = lastTimestamp;this.threadSeq = threadSeq;}private long nextSeq() {return timestamp() * 10000 + nextThreadSeq();}private long timestamp() {long currentTimeMillis = System.currentTimeMillis();if (currentTimeMillis < lastTimestamp) {// Just for considering time-shift-back by Ops or OS. @hanahmily 's suggestion.if (runRandomTimestamp != currentTimeMillis) {lastRandomValue = ThreadLocalRandom.current().nextInt();runRandomTimestamp = currentTimeMillis;}return lastRandomValue;} else {lastTimestamp = currentTimeMillis;return lastTimestamp;}}private short nextThreadSeq() {if (threadSeq == 10000) {threadSeq = 0;}return threadSeq++;}}
}

skywalking的全局ID生成源码相关推荐

  1. 集群高并发情况下如何保证分布式唯一全局ID生成

    作者:轻狂书生 blog.csdn.net/LookForDream_/article/details/109355335 前言 系统唯一ID是我们在设计一个系统的时候常常会遇见的问题,也常常为这个问 ...

  2. 面试官:高并发下,如何保证分布式唯一全局 ID 生成?

    欢迎关注方志朋的博客,回复"666"获面试宝典 前言 系统唯一ID是我们在设计一个系统的时候常常会遇见的问题,也常常为这个问题而纠结. 这篇文章就是给各位看官提供一个生成分布式唯一 ...

  3. 【转】MySQL分库分表环境下全局ID生成方案

    转载一篇博客,里面有很多的知识和思想值得我们去思考. ---------------------------------------------------------------------- 在大 ...

  4. mysql 分表全局id_MySQL分庫分表環境下全局ID生成方案

    摘要: 介紹來自flicker和twitter的兩種解決分布式環境下全局ID生成方案. 目錄: 1. 數據庫自增ID--來自Flicker的解決方案 2. 獨立的應用程序--來自Twitter的解決方 ...

  5. 7 种分布式全局 ID 生成策略,你更爱哪种?

    上了微服务之后,很多原本很简单的问题现在都变复杂了,例如全局 ID 这事! 最近工作中刚好用到这块内容,于是调研了市面上几种常见的全局 ID 生成策略,稍微做了一下对比,供小伙伴们参考. 当数据库分库 ...

  6. 七种分布式全局 ID 生成策略,你更爱哪种?

    上了微服务之后,很多原本很简单的问题现在都变复杂了,例如全局 ID 这事! 最近工作中刚好用到这块内容,于是调研了市面上几种常见的全局 ID 生成策略,稍微做了一下对比,供小伙伴们参考. 当数据库分库 ...

  7. mysql filck_顺序全局id生成方案-flickr(转载)

    全局ID的核心需求: 1.全局唯一 2.趋势递增(ID如果做为索引,有序非常重要) 3.单调递增(下一次调用的id一定要大于本次的id) 在大型互联网应用中,随着用户数的增加,为了提高应用的性能,我们 ...

  8. java wsdl反向生成源码,并使用CXF实现客户端调用代码

    1. 查询对方提供的wsdl链接,通过该链接以及wsdl工具生成源码文件. eg: wsdl地址为:http://sersh.passport.189.cn/UDBAPPInterface/UDBAP ...

  9. 【php】mysql全局ID生成方案

    生产系统随着业务增长总会经历一个业务量由小变大的过程,可扩展性是考量数据库系统高可用性的一个重要指标;在单表/数据库数据量过大,更新量不断飙涨时,MySQL DBA往往会对业务系统提出sharding ...

最新文章

  1. mysql5.6安装
  2. Spring Security 实战:自定义异常处理
  3. ubuntu19.04更改国内镜像源
  4. DBCP针对不同数据库的validationQuery
  5. c#如何wmf图片转换成png图片_每日一学:如何将png图片转换为jpg图片
  6. WPF实现一个彩虹按钮
  7. 3.Your First Machine Learning Model
  8. 求递推序列的第N项(51Nod-1126)
  9. 分布式的Key-Value存储系统Cassandra
  10. win10执行npm出现Error: EBUSY: resource busy or locked 错误解决方法
  11. 联想服务器怎么安装虚拟机,Windows 8里的虚拟机Hyper-V的安装及使用
  12. hsqldb mysql_安装HSQLDB
  13. 人到中年想学一门手艺自己创业好不好
  14. 【Excel从头开始】-2 快速填充/快速分析
  15. vnc远程控制软件,超好用的5款vnc远程控制软件
  16. 前端高级进阶13本经典书籍
  17. 计算机主机号怎么产看,怎么查看笔记本电脑出厂编号
  18. I'm just a baby——8月份英语总结
  19. 方正飞鸿智能信息平台(Fix ES2007)
  20. XP IIS下配置.net的问题总结与简单解决方法

热门文章

  1. 【讲解各种手机所有内存卡知识】
  2. 网络安全实战攻防演练应急处置预案
  3. (转)超级实用且不花哨的js代码大全
  4. 终于有人把IaaS、PaaS、SaaS讲明白了
  5. Kafka消息队列 入门到精通 看这一篇就够了
  6. css3切角文本框_CSS3切角选项卡tabs
  7. 简单的特效--css画圆圈
  8. RayMx RM1135是什么主控,答案就是……
  9. 关于倒序输出的三种方法
  10. 基于opencv的人脸检测(图片、视频、摄像头)