Java – how to generate a unique ID on a web server cluster

In the following scenarios:

1 database 4 web servers

How does the web server generate unique IDs for databases to make them unique? Yes, you can use automatic increment, but it's easy to grab / guess / etc Therefore, automatic increment is not an option at present

Solution

Use UUID( http://www.ietf.org/rfc/rfc4122.txt ). conflicts are unlikely, and they can be handled by regenerating a new UUID, or they can be blocked by connecting to the unique ID (such as MAC address) of each server: –

StringBuilder sb = new StringBuilder(UUID.randomUUID());
InetAddress address = InetAddress.getLocalHost();
String uid = sb.append(NetworkInterface.getByInetAddress(address).getHardwareAddress());
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>