Thursday, May 11, 2023

Curator Kullanımı

Giriş
Curator kelimesi sorumlu, idareci anlamına gelir. Açıklaması şöyle. Yani Zookeeper ile çalışmayı kolaylaştırır
CuratorFramework is a high-level framework/library provided by Apache Curator for working with Apache ZooKeeper, a distributed coordination service. It provides an abstraction layer and utilities to simplify the usage of ZooKeeper in Java applications.

ZooKeeper is a centralized service that enables distributed systems to coordinate and synchronize with each other. It provides features such as distributed locks, distributed queues, leader election, and more. However, directly interacting with the ZooKeeper API can be complex and low-level.

CuratorFramework aims to simplify the development of ZooKeeper-based applications by providing a higher-level API with various utility methods and patterns. It handles low-level ZooKeeper interactions and provides higher-level constructs, making it easier to build robust distributed systems.

Some key features and benefits of using CuratorFramework include:

Connection management: CuratorFramework manages the underlying connection to ZooKeeper, handling connection loss and reconnection automatically.

Retry policy: It provides configurable retry policies for handling connection and operation failures, allowing for resilient operations in the face of temporary issues.

Utility methods: CuratorFramework offers a set of utility methods for common distributed coordination tasks, such as creating and deleting nodes, acquiring locks, managing distributed queues, and more.

Recipes and abstractions: It provides higher-level abstractions and recipes that encapsulate complex coordination patterns, making it easier to implement common distributed algorithms
CuratorFramework Arayüzü
Şöyle yaparız
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;


public static void main(String[] args) {
  // Create a CuratorFramework instance
  CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient(
    "localhost:2181", // ZooKeeper server(s) connection string
    new ExponentialBackoffRetry(1000, 3) // Retry policy
  );

  // Start the CuratorFramework client
  curatorFramework.start();

  // Perform operations using CuratorFramework

  // Shutdown the CuratorFramework client
  curatorFramework.close();
}
InterProcessMutex Sınıfı

Örnek - Distributed Lock
Elimizde şöyle bir kod olsun
import org.apache.zookeeper.ZooKeeper;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;

public class DistributedLock {
  private CuratorFramework client;
  private InterProcessMutex lock;

  public DistributedLock(String zkConnectionString, String lockPath) {
    client = CuratorFrameworkFactory.newClient(zkConnectionString, new ExponentialBackoffRetry(1000, 3));
    client.start();
    lock = new InterProcessMutex(client, lockPath);
  }
  public boolean acquire(long waitTime, TimeUnit timeUnit) {
    try {
      return lock.acquire(waitTime, timeUnit);
    } catch (Exception e) {
      return false;
    }
  }
  public void release() {
    try {
      lock.release();
    } catch (Exception e) {}
  }
public void close() {
    client.close();
  }
}
Şöyle yaparız
public static void main(String[] args) {
  String zkConnectionString = "127.0.0.1:2181";
  String lockPath = "/my_resource_lock";

  DistributedLock lock = new DistributedLock(zkConnectionString, lockPath);

  // Acquire the lock
  if (lock.acquire(100, TimeUnit.MILLISECONDS)) {
    // Access the shared resource
    // Perform your operations here

    // Release the lock
    lock.release();
  }

  // Close the ZooKeeper connection
  lock.close();
}





No comments:

Post a Comment

kafka-consumer-groups.sh komutu

Giriş Bir topic'i dinleyen consumer'ları gösterir. Aynı topic'i dinleyen consumer group'ları olabilir. Her topic farklı part...