Skip to content

jcasbin/zookeeper-watcher

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ZooKeeper Watcher

GitHub Actions License Javadoc Maven Central Release Discord

ZooKeeper Watcher is an Apache ZooKeeper watcher for jCasbin.

Installation

For Maven

<dependency>
    <groupId>org.casbin</groupId>
    <artifactId>jcasbin-zookeeper-watcher</artifactId>
    <version>1.0.0</version>
</dependency>

How It Works

ZooKeeper Watcher uses Apache ZooKeeper's watch mechanism to synchronize policy changes across multiple jCasbin instances. When one instance updates its policy (via addPolicy, removePolicy, etc.), it updates a ZooKeeper node, which triggers watches on all other instances subscribed to that node. These instances then receive notifications and reload their policies.

Creating a ZooKeeperWatcher will:

  1. Connect to ZooKeeper server
  2. Create/ensure the watched node exists
  3. Set up a watch on the node for data changes
  4. Start a background thread to monitor changes

When you call e.setWatcher(zooKeeperWatcher), it sets a default updateCallback that calls e.LoadPolicy() to reload the policy from the adapter.

Simple Example

If you have two Casbin instances A and B:

A: Producer

// Initialize ZooKeeper Watcher
String connectString = "localhost:2181";
int sessionTimeout = 5000;
String nodePath = "/casbin/policy";

ZooKeeperWatcher watcher = new ZooKeeperWatcher(connectString, sessionTimeout, nodePath);

Enforcer enforcer = new SyncedEnforcer("examples/rbac_model.conf", "examples/rbac_policy.csv");
enforcer.setWatcher(watcher);

// The following code is not necessary and generally does not need to be written unless you understand what you want to do
/*
Runnable updateCallback = () -> {
    // Custom behavior
};
watcher.setUpdateCallback(updateCallback);
*/

// Modify policy, it will notify B
enforcer.addPolicy(...);

B: Consumer

// Initialize ZooKeeper Watcher with same node path
String connectString = "localhost:2181";
int sessionTimeout = 5000;
String nodePath = "/casbin/policy";

ZooKeeperWatcher watcher = new ZooKeeperWatcher(connectString, sessionTimeout, nodePath);

Enforcer enforcer = new SyncedEnforcer("examples/rbac_model.conf", "examples/rbac_policy.csv");
enforcer.setWatcher(watcher);

// B sets watcher and subscribes to the node path, then it will receive the notification from A
// and call LoadPolicy to reload policy

Configuration

The ZooKeeperWatcher constructor accepts three parameters:

  • connectString: ZooKeeper connection string (e.g., "localhost:2181" or "host1:2181,host2:2181,host3:2181")
  • sessionTimeout: Session timeout in milliseconds (recommended: 5000)
  • nodePath: ZooKeeper node path to watch (e.g., "/casbin/policy")

Advanced Usage

Custom Update Callback

You can set a custom callback that will be invoked when policy updates are detected:

ZooKeeperWatcher watcher = new ZooKeeperWatcher(connectString, sessionTimeout, nodePath);

// Using Runnable callback
watcher.setUpdateCallback(() -> {
    System.out.println("Policy updated!");
    // Custom logic here
});

// Using Consumer callback for detailed information
watcher.setUpdateCallback((message) -> {
    System.out.println("Received update: " + message);
    // Custom logic here
});

Cleanup

When you're done with the watcher, remember to close it:

watcher.close();

Requirements

  • Java 8 or higher
  • Apache ZooKeeper 3.8.x or higher
  • jCasbin 1.22.2 or higher

Getting Help

License

This project is under Apache 2.0 License. See the LICENSE file for the full license text.