skywalking的全局ID生成源码
学习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生成源码相关推荐
- 集群高并发情况下如何保证分布式唯一全局ID生成
作者:轻狂书生 blog.csdn.net/LookForDream_/article/details/109355335 前言 系统唯一ID是我们在设计一个系统的时候常常会遇见的问题,也常常为这个问 ...
- 面试官:高并发下,如何保证分布式唯一全局 ID 生成?
欢迎关注方志朋的博客,回复"666"获面试宝典 前言 系统唯一ID是我们在设计一个系统的时候常常会遇见的问题,也常常为这个问题而纠结. 这篇文章就是给各位看官提供一个生成分布式唯一 ...
- 【转】MySQL分库分表环境下全局ID生成方案
转载一篇博客,里面有很多的知识和思想值得我们去思考. ---------------------------------------------------------------------- 在大 ...
- mysql 分表全局id_MySQL分庫分表環境下全局ID生成方案
摘要: 介紹來自flicker和twitter的兩種解決分布式環境下全局ID生成方案. 目錄: 1. 數據庫自增ID--來自Flicker的解決方案 2. 獨立的應用程序--來自Twitter的解決方 ...
- 7 种分布式全局 ID 生成策略,你更爱哪种?
上了微服务之后,很多原本很简单的问题现在都变复杂了,例如全局 ID 这事! 最近工作中刚好用到这块内容,于是调研了市面上几种常见的全局 ID 生成策略,稍微做了一下对比,供小伙伴们参考. 当数据库分库 ...
- 七种分布式全局 ID 生成策略,你更爱哪种?
上了微服务之后,很多原本很简单的问题现在都变复杂了,例如全局 ID 这事! 最近工作中刚好用到这块内容,于是调研了市面上几种常见的全局 ID 生成策略,稍微做了一下对比,供小伙伴们参考. 当数据库分库 ...
- mysql filck_顺序全局id生成方案-flickr(转载)
全局ID的核心需求: 1.全局唯一 2.趋势递增(ID如果做为索引,有序非常重要) 3.单调递增(下一次调用的id一定要大于本次的id) 在大型互联网应用中,随着用户数的增加,为了提高应用的性能,我们 ...
- java wsdl反向生成源码,并使用CXF实现客户端调用代码
1. 查询对方提供的wsdl链接,通过该链接以及wsdl工具生成源码文件. eg: wsdl地址为:http://sersh.passport.189.cn/UDBAPPInterface/UDBAP ...
- 【php】mysql全局ID生成方案
生产系统随着业务增长总会经历一个业务量由小变大的过程,可扩展性是考量数据库系统高可用性的一个重要指标;在单表/数据库数据量过大,更新量不断飙涨时,MySQL DBA往往会对业务系统提出sharding ...
最新文章
- mysql5.6安装
- Spring Security 实战:自定义异常处理
- ubuntu19.04更改国内镜像源
- DBCP针对不同数据库的validationQuery
- c#如何wmf图片转换成png图片_每日一学:如何将png图片转换为jpg图片
- WPF实现一个彩虹按钮
- 3.Your First Machine Learning Model
- 求递推序列的第N项(51Nod-1126)
- 分布式的Key-Value存储系统Cassandra
- win10执行npm出现Error: EBUSY: resource busy or locked 错误解决方法
- 联想服务器怎么安装虚拟机,Windows 8里的虚拟机Hyper-V的安装及使用
- hsqldb mysql_安装HSQLDB
- 人到中年想学一门手艺自己创业好不好
- 【Excel从头开始】-2 快速填充/快速分析
- vnc远程控制软件,超好用的5款vnc远程控制软件
- 前端高级进阶13本经典书籍
- 计算机主机号怎么产看,怎么查看笔记本电脑出厂编号
- I'm just a baby——8月份英语总结
- 方正飞鸿智能信息平台(Fix ES2007)
- XP IIS下配置.net的问题总结与简单解决方法