To use the unique-client
module, you need to include the unique-client-0.1.0.jar
file and its dependencies in the classpath.
If you are using Maven just add the following dependency to your pom.xml:
<dependency>
<groupId>io.leego</groupId>
<artifactId>unique-client</artifactId>
<version>0.1.0</version>
</dependency>
There are two kinds of clients: SimpleUniqueClient
and CachedUniqueClient
.
Notice that the SimpleUniqueClient
's performance is terrible in cluster mode, please use the CachedUniqueClient
instead of the SimpleUniqueClient
.
Assume there is an instance of the UniqueService
.
UniqueClient simpleClient = new SimpleUniqueClient(uniqueService);
UniqueClient cachedClient = new CachedUniqueClient(uniqueService);
Use the UniqueClients
.
// SimpleUniqueClient
UniqueClient simpleClient = UniqueClients.newSimple(uniqueService);
// CachedUniqueClient
Integer cacheSize = 100;
Duration timeout = Duration.ofSeconds(3L);
UniqueClient cachedClient = UniqueClients.newCached(uniqueService, cacheSize, timeout);
Set with URL
.
UniqueClient client = UniqueClients.builder()
.url("https://localhost:8080")
.timeout(Duration.ofSeconds(3L))
.cached()
.cacheSize(100)
.build();
Set with host
, port
and SSL
.
UniqueClient client = UniqueClients.builder()
.host("localhost")
.port(8080)
.ssl()
.timeout(Duration.ofSeconds(3L))
.cached()
.cacheSize(100)
.build();
Obtain one sequence named test-key
.
// It is equivalent to the cURL command line:
// curl --location --request GET 'https://localhost:8080/sequences/test-key' --header 'Content-Type: application/json'
long value = client.next("test-key");
// Outputs result
System.out.println(value);
Obtain several sequences named test-key
.
// It is equivalent to the cURL command line:
// curl --location --request GET 'https://localhost:8080/sequences/test-key/segments?size=10' --header 'Content-Type: application/json'
LinkedList<Long> values = client.next("test-key", 10);
// Outputs result
while (!values.isEmpty()) {
System.out.println(values.remove());
}