javaapache-zookeeperservice-discoveryapache-curator

How to register a service too ZooKeeper using Curator.x.discovery


I am trying to register a simple REST service on int port, to ZooKeeper server at localhost:2181. I checked path ls / using zooClient too. Any ideas?

private static void registerInZookeeper(int port) throws Exception {
CuratorFramework curatorFramework = CuratorFrameworkFactory
        .newClient("localhost:2181", new RetryForever(5));

curatorFramework.start();

ServiceInstance<Object> serviceInstance = ServiceInstance.builder()
        .address("localhost")
        .port(port)
        .name("worker")
        .uriSpec(new UriSpec("{scheme}://{address}:{port}"))
        .build();

ServiceDiscoveryBuilder.builder(Object.class)
        .basePath("myNode")
        .client(curatorFramework)
        .thisInstance(serviceInstance)
        .build()
        .start();

Optional.ofNullable(curatorFramework.checkExists().forPath("/zookeeper")).ifPresent(System.out::println);
Optional.ofNullable(curatorFramework.checkExists().forPath("/myNode")).ifPresent(System.out::println);
}

Solution

  • I kept receiving Received packet at server of unknown type 15 from Zoo Server, because of compatibility issues