Distributed globally unique ID and increment sequence

ID chronological comprising

This scenario is the simplest implementation, is to use twitter Snowflake's algorithm.
Total length of 64-bit ID, 1 bit is unavailable, 41 denotes a time stamp, showing forming machine 10 id, 12 represents the sequence number.

  • Why the first is not available? The first bit is 0, you can ensure data type in java ID long been a positive integer increments
  • That same timestamp milliseconds, the number of ID can produce? 2 ^ 12 = 4096 ID [0 ~ 4095]
  • Uniqueness? ID has previously been done by a machine a space isolation, then do a time stamp by isolation, and finally achieved a certain degree by counting only the time stamp
  • high performance? You can ease the load pressure when the stand-alone high concurrency by increasing IDWorker
  • Shortcoming? Time is limited, 41 can represent 69 years (but you can reduce the time the machine level to increase the number of bits)

Since increasing sequence

principle

The Distributed Lock key acquisition, acquisition number after obtaining the lock, and the offset of the offset configuration, replace the original number, and then release the lock.

Based achieve zookeeper

Based zookeeper be quickly accomplished service increment sequence, the apache introduced curator package zookeeper client.

1
2
3
4
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
</dependency>

Zookeeper establish connections, open zkclient, if repeated use, it can be placed in the global map, for unified management.

address: zookeeper address
RetryNTimes: reconnection policy (reconnection retry count, reconnection of milliseconds)

1
2
CuratorFramework zkClient =  CuratorFrameworkFactory.newClient(address,new RetryNTimes(10, 5000));
zkClient.start();

Get distributed lock
according to the service logic, select the appropriate lock, as used herein, it is reentrant shared lock, i.e., at the same time a client has locks can request acquisition again.

Big Box   distributed globally unique ID and increment sequence = "gutter">
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
InterProcessMutex lock = new InterProcessMutex(zkClient, lockPath);
try {
if (lock.acquire(time, unit)) {
consumer.accept(lockPath);
return true;
}
return false;
} catch (Exception e) {
logger.error(e.getMessage(), e);
} finally {
try {
lock.release();
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}
return false;

Get the next sequence number
to determine whether the sequence name already exists, create if does not exist, there is increased step and write

creatingParentsIfNeeded: zk when the parent node does not exist, create iterative
sequenceName: Sequence Name
step: increment step

1
2
3
4
5
6
7
8
9
String path = "/seq/"+sequenceName;
boolean exists = zkClient.checkExists().forPath(path) != null;
if (!exists) {
zkClient.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath(path, (step + "").getBytes());
} else {
byte[] bytes = zkClient.getData().forPath(path);
Long seq = Long.valueOf(new String(bytes));
zkClient.setData().forPath(path, (seq + step + "").getBytes());
}

Because of the need to modify real-time node information zookeeper, you can consider the establishment of the pool sequences, such as direct removal of 10,000 sequences from various internal services to generate your own specific implementation depends to CAS, compareAndSet to achieve through the interior of the stand-alone incremented to avoid lock abuse

Guess you like

Origin www.cnblogs.com/lijianming180/p/12014177.html