NOTE

ZooKeeper as a Service Registry

ZooKeeper service-registry implementation and its principle using ephemeral sequential nodes and child-node change watches.

ZooKeeperCreated Updated 1 min readhistorical

This is a historical learning note and may contain outdated or incomplete understanding.

2. ZooKeeper Service-Registry Implementation

2.1. pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.zsk</groupId>
    <artifactId>test_zk</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.20.RELEASE</version>
    </parent>

    <dependencies>
        <!--zookeeper-->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.5.6</version>
        </dependency>
        <!--test-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>1.5.20.RELEASE</version>
                <configuration>
                    <executable>true</executable>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

2.2. Code

public class RegistryCenter
{
    private final String REGISTRY_PATH = "/registry/";

    public static void main(String[] args)
    {

    }

    public ZooKeeper connect() throws Exception
    {
        // Execute asynchronously. Use CountDownLatch for synchronization and wait until the connection is created before continuing.
        CountDownLatch countDownLatch = new CountDownLatch(1);
        ZooKeeper zooKeeper = new ZooKeeper("127.0.0.1:2181", 5000, new Watcher()
        {
            @Override
            public void process(WatchedEvent watchedEvent)
            {
                countDownLatch.countDown();
            }
        });
        countDownLatch.await();
        System.out.println(machineName + " connected to ZooKeeper successfully");
        return zooKeeper;
    }

    public void register(ZooKeeper zooKeeper, String ip)
    {
        zooKeeper.create(REGISTRY_PATH + ip, "".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL, new AsyncCallback.StringCallback()
        {
            @Override
            public void processResult(int rc, String path, Object ctx, String name)
            {
                if (rc == KeeperException.Code.OK.intValue() )
                {
                    System.out.println(ip + " online");
                    try
                    {
                        List<String> children = zooKeeper.getChildren(REGISTRY_PATH, new Watcher()
                        {
                            @Override
                            public void process(WatchedEvent event)
                            {
                                if (event.getType() == Event.EventType.NodeChildrenChanged)
                                {

                                }
                            }
                        });
                    }
                    catch (Exception e)
                    {
                        e.printStackTrace();
                    }
                }
                else
                {
                    System.out.println("abnormal state");
                }
            }
        },"ctx_data");

    }
}

1. ZooKeeper Service-Registry Principle

ZooKeeper Use Case - Service Registry

Create ephemeral sequential nodes + watch child-node change events on the parent node.

Discussion

Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub