前言
我们经常会遇到这样的场景,需要生成一个唯一的序列号来表明某一个数据的唯一性,在单节点的应用中我们可以简单地使用一个自增的整型来实现实现,但是在分布式情况下这个方式却存在冲突的可能性,那么有什么办法我们可以生成一个唯一的序列号呢,并且如果想使得这个序列号也能展示一些业务信息呢?
正文
UUID实现唯一标识码
UUID 的目的是让分布式系统中的所有元素,都能有唯一的辨识资讯,而不需要透过中央控制端来做辨识资讯的指定。如此一来,每个人都可以建立不与其它人冲突的 UUID
(通用唯一标识码)。
UUID的组成:
- 当前日期和时间。
- 时钟序列(数值 按其发生的先后顺序而排列的数列)
- 全局唯一的
IEEE
机器识别号。
UUID虽然可以保证全局唯一,但是它占用32位(十进制),而且是无序的,入库时性能比较差。这是因为,关系型数据库的索引都是B+
树结构, 如果我们按照ID
递增的顺序,新的结点会插入到最后一个结点后面去,当最后一个结点满了,会裂变出新的结点。但如果是插入无序的,不仅会导致中间结点的裂变,还会产生很多不饱和节点,导致性能降低。
UUIDStringUtils:生成UUID
唯一标示码的工具类
public class UUIDStringUtils {
public static String randomUUID() {
UUID uuid = UUID.randomUUID();
return uuid.toString().replace("-", "").toUpperCase();
}
}
验证方法
@org.junit.Test
public void test(){
System.out.println("通过UUID的方式生成的唯一序列号"+UUIDStringUtils.randomUUID());
}
运行结果如下:
SnowFlake实现唯一标识码
SnowFlake算法:分布式系统中生成全局唯一且趋势递增的Id
.
总共64位2进制,换成十进制为18位
- 第一部分: 1位,始终为0.
- 第二部分:41位,精确为毫秒的时间戳。
- 第三部分:10位,机器码
- 第四部分:12位,序列号
snowflake的优点:
- 是按
ID
递增,易于插入到数据库 - 不依赖数据库,在内存中生成,性能好
SnowflakeIdWorker:生成SnowFlake
唯一标识码的工具类
public class SnowflakeIdWorker {
// ==============================Fields===========================================
private final long twepoch = 1420041600000L;
private final long workerIdBits = 5L;
private final long datacenterIdBits = 5L;
private final long maxWorkerId = -1L ^ (-1L << workerIdBits);
private final long maxDatacenterId = -1L ^ (-1L << datacenterIdBits);
private final long sequenceBits = 12L;
private final long workerIdShift = sequenceBits;
private final long datacenterIdShift = sequenceBits + workerIdBits;
private final long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits;
private final long sequenceMask = -1L ^ (-1L << sequenceBits);
private long workerId;
private long datacenterId;
private long sequence = 0L;
private long lastTimestamp = -1L;
//==============================Constructors=====================================
public SnowflakeIdWorker(long workerId, long datacenterId) {
if (workerId > maxWorkerId || workerId < 0) {
throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId));
}
if (datacenterId > maxDatacenterId || datacenterId < 0) {
throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId));
}
this.workerId = workerId;
this.datacenterId = datacenterId;
}
// ==============================Methods==========================================
public synchronized long nextId() {
long timestamp = timeGen();
//如果当前时间小于上一次ID生成的时间戳,说明系统时钟回退过这个时候应当抛出异常
if (timestamp < lastTimestamp) {
throw new RuntimeException(
String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp));
}
//如果是同一时间生成的,则进行毫秒内序列
if (lastTimestamp == timestamp) {
sequence = (sequence + 1) & sequenceMask;
//毫秒内序列溢出
if (sequence == 0) {
//阻塞到下一个毫秒,获得新的时间戳
timestamp = tilNextMillis(lastTimestamp);
}
}
//时间戳改变,毫秒内序列重置
else {
sequence = 0L;
}
//上次生成ID的时间截
lastTimestamp = timestamp;
//移位并通过或运算拼到一起组成64位的ID
return ((timestamp - twepoch) << timestampLeftShift) //
| (datacenterId << datacenterIdShift) //
| (workerId << workerIdShift) //
| sequence;
}
protected long tilNextMillis(long lastTimestamp) {
long timestamp = timeGen();
while (timestamp <= lastTimestamp) {
timestamp = timeGen();
}
return timestamp;
}
protected long timeGen() {
return System.currentTimeMillis();
}
//==============================Test=============================================
public static void main(String[] args) {
SnowflakeIdWorker idWorker = new SnowflakeIdWorker(0, 0);
for (int i = 0; i < 1000; i++) {
long id = idWorker.nextId();
//转换成二进制
System.out.println(Long.toBinaryString(id));
System.out.println(id);
}
}
}
验证方法
@org.junit.Test
public void test2(){
SnowflakeIdWorker idWorker = new SnowflakeIdWorker(0, 0);
long id = idWorker.nextId();
System.out.println("通过SnowFlake的方式生成的唯一序列号"+Long.toBinaryString(id) );
}
运行结果如下:
通过时间工具生成带有业务标示的唯一标识码
事实上,很多情况下在高并发没有那么多的情况,并且我们需要通过唯一标识码获取一些简单的业务信息时,可以使用Java
自带的时间工具类生成一个带有业务标示的唯一标识码。
OrderNoUtils:生成带有业务标示的唯一标识码的工具类
public class OrderNoUtils {
private OrderNoUtils() {
throw new IllegalStateException("Utility class");
}
public static String createOrderNo(String productNo) {
return DateTimeUtils.getTodayChar14() + productNo
+ RandomNumberUtil.createRandomNumber(6);
}
public static String createVipBizNo(String productNo) {
return "VIP" + DateTimeUtils.getTodayChar14() + productNo
+ RandomNumberUtil.createRandomNumber(4);
}
public static String createRequestNo(String identifier,int randomLength) {
return DateTimeUtils.getTodayChar17() + identifier
+ RandomNumberUtil.createRandomNumber(randomLength);
}
}
RandomNumberUtil:生成随机数的工具类
public class RandomNumberUtil {
private RandomNumberUtil() {
}
public static String createRandomNumber(int length) {
StringBuilder strBuffer = new StringBuilder();
Random rd = new Random();
for (int i = 0; i < length; i++) {
strBuffer.append(rd.nextInt(10));
}
return strBuffer.toString();
}
}
DateTimeUtils:时间工具类
public class DateTimeUtils {
// private static Logger logger = LoggerFactory.getLogger(DateTimeUtils.class);
private static List<String> dateFormatPattern = new ArrayList<String>();
private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
static {
dateFormatPattern.add("yyyy-MM-dd HH:mm:ss");
dateFormatPattern.add("yyyy-MM-dd HH:mm:ss.S z");
dateFormatPattern.add("yyyy-MM-dd G HH:mm:ss.S z");
dateFormatPattern.add("yyyy-MM-dd HH:mm:ss.S 'UTC'");
dateFormatPattern.add("yyyy-MM-dd G HH:mm:ss.S 'UTC'");
dateFormatPattern.add("yyyy-MM-dd HH:mm:ss.S z");
dateFormatPattern.add("yyyy-MM-dd HH:mm:ss.S a");
dateFormatPattern.add("yyyy-MM-dd HH:mm:ssz");
dateFormatPattern.add("yyyy-MM-dd HH:mm:ss z");
dateFormatPattern.add("yyyy-MM-dd HH:mm:ss 'UTC'");
dateFormatPattern.add("yyyy-MM-dd'T'HH:mm:ss.SX");
dateFormatPattern.add("yyyy-MM-dd'T'HH:mm:ssX");
dateFormatPattern.add("yyyy-MM-dd'T'HH:mmX");
dateFormatPattern.add("yyyy-MM-dd HH:mm:ssa");
dateFormatPattern.add("yyyy/MM/dd");
dateFormatPattern.add("yyyy/M/d");
dateFormatPattern.add("yyyy-MM-dd");
dateFormatPattern.add("yyyy-M-d");
dateFormatPattern.add("yyyy/M/d");
dateFormatPattern.add("yyyy年M月d日");
dateFormatPattern.add("yyyy年MM月dd日");
dateFormatPattern.add("yyyy-MM-dd'T'HH:mm:ss.SSS+0800");
}
// 格式:中文星期
private final static String[] FORMAT_WEEK_CHINESE_SIMPLE = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
// 格式:中文星期
private final static String[] FORMAT_WEEK_CHINESE = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
// 格式:英文格式简写
private final static String[] FORMAT_WEEK_ENGLISH_SIMPLE = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
// 格式:英文全称
private final static String[] FORMAT_WEEK_ENGLISH = {"Sun", " Mon", " Tue", " Wed", " Thu", " Fri", " Sat"};
public static String getCurrentYear() {
return DateFormatUtils.format(new Date(), "yyyy");
}
public static String getCurrentMonth() {
return DateFormatUtils.format(new Date(), "MM");
}
public static String getTodayDay() {
return DateFormatUtils.format(new Date(), "dd");
}
public static String getTodayChar6() {
return DateFormatUtils.format(new Date(), "yyyyMM");
}
public static String getTodayChar8() {
return DateFormatUtils.format(new Date(), "yyyyMMdd");
}
public static String getTodayChar12() {
return DateFormatUtils.format(new Date(), "yyyyMMddHHmm");
}
public static String getTodayChar14() {
return DateFormatUtils.format(new Date(), "yyyyMMddHHmmss");
}
public static String getTodayChar17() {
String dateString = DateFormatUtils.format(new Date(), "yyyyMMddHHmmssS");
int length = dateString.length();
if (length < 17) {
String endStr = dateString.substring(14, length);
int len = endStr.length();
for (int i = 0; i < 3 - len; i++) {
endStr = "0" + endStr;
}
dateString = dateString.substring(0, 14) + endStr;
}
return dateString;
}
public static long getSysCurrentTimeMillis() {
return System.currentTimeMillis();
}
public static String convertTimeFormat(long timeMillis, String format) {
return DateFormatUtils.format(timeMillis, format);
}
public static String getSystemTime() {
Calendar theCa = Calendar.getInstance();
theCa.setTime(new Date());
return DateFormatUtils.format(theCa.getTime(), "yyyy-MM-dd HH:mm:ss");
}
public static String getWeekName(int type) {
String strResult = " ";
try {
Calendar calendar = Calendar.getInstance();
int intWeekNum = calendar.get(Calendar.DAY_OF_WEEK);
intWeekNum = intWeekNum - 1;
if (type == 1) {
strResult = FORMAT_WEEK_CHINESE_SIMPLE[intWeekNum];
} else if (type == 2) {
strResult = FORMAT_WEEK_CHINESE[intWeekNum];
} else if (type == 3) {
strResult = FORMAT_WEEK_ENGLISH_SIMPLE[intWeekNum];
} else if (type == 4) {
strResult = FORMAT_WEEK_ENGLISH[intWeekNum];
} else {
strResult = FORMAT_WEEK_CHINESE_SIMPLE[intWeekNum];
}
} catch (Exception ex) {
strResult = " ";
}
return strResult;
}
public static int getCurrentMonthDays() {
Calendar a = Calendar.getInstance();
a.set(Calendar.DATE, 1);// 把日期设置为当月第一天
a.roll(Calendar.DATE, -1);// 日期回滚一天,也就是最后一天
return a.get(Calendar.DATE);
}
public static boolean isBetweenTwoTimes(String startTime, String endTime) {
//当前时间
long nowTime = Long.parseLong(DateFormatUtils.format(new Date(), "yyyyMMddHHmmss"));
if (StringUtils.isBlank(startTime) || StringUtils.isBlank(endTime)) {
return false;
}
if (Long.parseLong(startTime) < nowTime && nowTime < Long.parseLong(endTime)) {
return true;
}
return false;
}
public static boolean isBetweenTwoTimes17(String startTime, String endTime) {
//当前时间
long nowTime = Long.parseLong(DateFormatUtils.format(new Date(), "yyyyMMddHHmmssS"));
if (StringUtils.isBlank(startTime) || StringUtils.isBlank(endTime)) {
return false;
}
if (Long.parseLong(startTime) < nowTime && nowTime < Long.parseLong(endTime)) {
return true;
}
return false;
}
public static long getTodayExpireTime() {
Calendar calendar = Calendar.getInstance();
String date = DateTimeUtils.getTodayChar8();
calendar.set(Calendar.YEAR, Integer.parseInt(date.substring(0, 4)));
calendar.set(Calendar.MONTH, Integer.parseInt(date.substring(4, 6)) - 1);
calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(date.substring(6, 8)) + 1);
calendar.set(Calendar.HOUR_OF_DAY, 00);
calendar.set(Calendar.MINUTE, 05);
calendar.set(Calendar.SECOND, 00);
Calendar ccalendar = Calendar.getInstance();// 当前时间
ccalendar.setTime(new Date());
return calendar.getTimeInMillis() - ccalendar.getTimeInMillis();
}
public static long getMonthExpireTime() {
Calendar calendar = Calendar.getInstance();
String date = DateTimeUtils.getTodayChar8();
calendar.set(Calendar.YEAR, Integer.parseInt(date.substring(0, 4)));
calendar.set(Calendar.MONTH, Integer.parseInt(date.substring(4, 6)) - 1);
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
Calendar ccalendar = Calendar.getInstance();// 当前时间
ccalendar.setTime(new Date());
return calendar.getTimeInMillis() - ccalendar.getTimeInMillis();
}
public static long getOneYearExpireTime() {
Calendar calendar = Calendar.getInstance();
String date = DateTimeUtils.getTodayChar8();
calendar.set(Calendar.YEAR, Integer.parseInt(date.substring(0, 4)) + 1);
calendar.set(Calendar.MONTH, Integer.parseInt(date.substring(4, 6)) - 1);
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
calendar.set(Calendar.HOUR_OF_DAY, 00);
calendar.set(Calendar.MINUTE, 00);
calendar.set(Calendar.SECOND, 00);
Calendar ccalendar = Calendar.getInstance();// 当前时间
ccalendar.setTime(new Date());
return calendar.getTimeInMillis() - ccalendar.getTimeInMillis();
}
public static Date formatDate(String date){
Date result = null;
fp : for (String formatPattern : dateFormatPattern) {
try {
simpleDateFormat.applyPattern(formatPattern);
result = simpleDateFormat.parse(date);
if (result != null) {
break fp;
}
} catch (ParseException e) {
// e.printStackTrace();
//logger.info(date + " format fail");
}
}
return result;
}
}
验证方法
@org.junit.Test
public void test3(){
System.out.println("通过时间工具的方式生成的唯一序列号:"+ OrderNoUtils.createOrderNo("13012345678"));
}
运行结果如下:
到此这篇关于浅谈Java生成唯一标识码的三种方式的文章就介绍到这了,更多相关Java生成唯一标识码内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!