1. Introduction :
1. The composition of the primary key: the current time of the system is taken to milliseconds (15 digits) + the machine IP (6 digits) + the atomic self-increasing number (3 digits), a total of 24 digits
2. Support to generate 1000*1000 per second
3. Scalability: You can expand the number of self-increasing digits, and at this time, the number of primary keys generated per second will also increase automatically.
2. Code :
import org.apache.commons.lang.StringUtils; import java.net.InetAddress; import java.net.UnknownHostException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.locks.ReentrantLock; /** * @describe: 主键工厂 * @author:houkai * @Date: 2018/3/5 9:47 */ public class IdFactoryUtil { private static HashMap<String, AtomicInteger> cache = new HashMap<String, AtomicInteger>(1); private static final ReentrantLock lock = new ReentrantLock(); private static final SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmssSSS"); /** * 获取主键 * @return * @throws Exception */ public static String getId() throws UnknownHostException { String ip = null; ip = InetAddress.getLocalHost().getHostAddress(); String[] ipArray = ip.split("\\."); final String lastTwoPhaseIp = StringUtils. rightPad (ipArray [ 2 ], 3 , '0' ) + StringUtils. leftPad (ipArray [ 3 ], 3 , '0' ); String tss = getTimeStampSequence(); String id = tss + lastTwoPhaseIp; return id; } /** * Get the specified number of ids * @param amount : the number of ids */ public static List<String> getIds( int amount) throws UnknownHostException { //Get the host IP, this block may throw exceptions, String ip = ip = InetAddress.getLocalHost (). getHostAddress (); String[] ipArray = ip .split( " \\ ." ); //The last six digits of the IP address final String lastTwoPhaseIp = StringUtils. rightPad (ipArray[ 2 ], 3 , '0' ) + StringUtils. leftPad (ipArray[ 3 ], 3 , '0' ); List<String> list = new ArrayList<>(amount); for (int i = 0; i < amount; i++){ String tss = getTimeStampSequence(); String id = tss + lastTwoPhaseIp; list.add(id); } return list; } /** * Get the system time and format it as String, AtomicInteger guarantees non-repetitive self-increment within milliseconds * @return */ private static String getTimeStampSequence() { String timestamp = null; String inc = null; lock.lock(); try { timestamp = sdf.format(new Date()); AtomicInteger value = cache.get(timestamp); if(value == null) { cache.clear(); int defaultStartValue = 0; cache.put(timestamp, new AtomicInteger(defaultStartValue)); inc = String.valueOf(defaultStartValue); } else { inc = String.valueOf(value.addAndGet(1)); } } finally { lock.unlock(); } return timestamp + StringUtils.leftPad(inc, 4, '0'); } }
3. Maven dependencies required by the code
<dependency>
<groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>