UUID雪花算法,自动生成ID方法
//创建包名uuid,包下3个实体类
//IdWorker
/******************************************************************************
* Copyright (C) 2018-2022 Yantai HaiYi Software Co., Ltd
* All Rights Reserved.
* Developed by Yantai Haiyi Software . Without the prior written consent of the company hereto,
* No party may use, copy, modify or release this software.
*****************************************************************************/
package com.ruoyi.system.uuid;
import java.net.InetAddress;
import java.net.UnknownHostException;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
/**
* twitter的SnowFlake算法获取18位的数字id
*
* @author liulei
*
*<p>Modification History:</p>
*<p>Date Author Description</p>
*<p>------------------------------------------------------------------</p>
*<p>2018年8月23日 liulei 新建</p>
*/
public class IdWorker {
private static Long p1;
private static Long p2;
private static SnowflakeIdWorker snowflakeIdWorker;
private IdWorker(){}
/**
* 获取一个id
* @return
* @author liulei - 2018年8月23日 :
*/
public static String getId() {
if(snowflakeIdWorker == null) {
if( p1 == null || p2 == null ){
getP1andP2();
}
snowflakeIdWorker = new SnowflakeIdWorker(p1, p2);
}
return snowflakeIdWorker.nextId();
}
private static void getP1andP2(){
try{
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();