Plan 1:
If there is no concurrency and the order number is only generated in one thread, then since the program is executed sequentially, the generation time stamps of different orders are normally different. Therefore, the timestamp + random number (or self-incremental number) can be used to distinguish each order. If concurrency exists and the order number is generated by multiple threads in a process, the order number can be guaranteed to be unique by adding the thread ID to the sequence number. If there is concurrency and the order number is generated by multiple processes in the same host, the order number can be guaranteed to be unique by adding the process ID to the sequence number. If there is concurrency and the order number is generated by different hosts, then the number that can distinguish the host, such as the MAC address, IP address, or CPU serial number, can ensure that the order number is unique to the serial number.
Plan 2:
Timestamp + user ID + several random numbers + optimistic lock.
Plan 3:
Use redis atomic increment to build a high-availability cluster.
Solution 4 (non-pure numbers):
Java comes with uuid.
Case code
Java gets thread ID
Thread.currentThread().getId()
Java get process ID
// get name representing the running Java virtual machine. String name = ManagementFactory.getRuntimeMXBean().getName(); System.out.println(name); // get pid String pid = name.split("@")[0]; System.out.println("Pid is:" + pid);Get mac address by java
InetAddress ia = InetAddress.getLocalHost(); byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress(); String macStr = DatatypeConverter.printHexBinary(mac);
Summarize
The above is the only solution for java web to achieve order number generation in high concurrency and distributed placing java web. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!