java web implements a unique solution for order number generation under high concurrency and distribution
- 2020-11-18 06:13:17
- OfStack
Plan 1:
If there is no concurrency, and the order number is only generated in one thread, then because the program is executed sequentially, the generation timestamp of different orders is normally different, so the time stamp + random number (or autoincrement) can be used to distinguish each order. If there is concurrency and the order number is generated by multiple threads in one process, simply adding thread ID to the sequence number ensures that the order number is unique to 1. If concurrency exists and the order number is generated by multiple processes on the same host, simply adding process ID to the serial number ensures that the order number is unique to 1. If concurrency exists and the order number is generated by different hosts, the addition of a host distinguishing number such as the MAC address, IP address, or CPU serial number to the serial number ensures that the order number is unique to 1.
Scheme 2:
Timestamp + user ID+ a few random Numbers + optimistic locks.
Solution 3:
Use redis atom increments to do high availability clusters.
Option 4 (non-pure Numbers) :
java comes with uuid.
The example code
java gets the thread ID
Thread.currentThread().getId()
java gets the 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);
java gets the mac address
InetAddress ia = InetAddress.getLocalHost();
byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();
String macStr = DatatypeConverter.printHexBinary(mac);
conclusion