How to generate unique ID in clustering
Is there any clustering API that helps in generating the unique 开发者_如何学GoID across many servers?
Use UUID.
Even though a collision is possible in practice, the odds are soooo astronomically low that everybody uses it, including critical industrial applications.
Here is a discussion about the collision probability.
Hazelcast is super simple, open source clustering solution for java. Here is the quote from documentation.
Hazelcast IdGenerator creates cluster-wide unique IDs. Generated IDs are long type primitive values between 0 and Long.MAX_VALUE . Id generation occurs almost at the speed of AtomicLong.incrementAndGet() . Generated IDs are unique during the life cycle of the cluster. If the entire cluster is restarted, IDs start from 0 again.
import com.hazelcast.core.IdGenerator;
import com.hazelcast.core.Hazelcast;
IdGenerator idGenerator = Hazelcast.getIdGenerator("customer-ids");
long id = idGenerator.newId();
http://www.hazelcast.com/documentation.jsp#IdGenerator
Reserve some IDs
Server 1: 1000000.. 2999999
Server 2: 2000000.. 3999999
...
Server 3: 3000000.. 3999999
You may add some inteligence so that the reservation is dinamic but there is no need.
I'm using hazelcast, a very good clustering framework, providing the unique id and much more.
精彩评论