1

I have many services connected to zookeeper, and I want that service A can get service B's IP, when service B connected to zookeeper, is there any API can do that? Or I have to use other config file to write down all services's IP?

Endlesscode
  • 23
  • 1
  • 3

1 Answers1

2

Take a look if this solves your problem: http://curator.apache.org/curator-x-discovery/

Zookeeper doesn't provide service discovery out of the box, but it is easy to implement it yourself.

You won't be able to get the IP addresses of other connected clients (services, in your case) straight from the Zookeeper API. In order to get other services connected to the cluster, each service has to individually create an ephemeral znode under a specific path, e.g. /services, and set the necessary addressing and naming info as znode's data (IP, port, etc). This way, you can list that path and discover active services, or watch the /services path for any changes in your service configuration.

Since services are creating ephemeral nodes, they will automatically be removed once they are disconnected and their session expires. Of course, once you start doing something like this, you will see that there are many small details and design decisions you have to make, ergo the already mentioned Curator recipe.

igorbel
  • 1,376
  • 8
  • 14
  • Thank you for your answer :), currently, I did what you said, each service create an ephemeral znode under a specific path by group, except that I didn't put service ip into znode data, just wonder whether zookeeper api can help me get the ip or I have to set ip adresss to znode data myself ? If zookeeper can offer me the znode ip, deployment would be more easier :), because I don't have to set the IP address in my config file, or get the IP adresss by code. – Endlesscode Apr 02 '15 at 02:27
  • Plus, I need IP address because service A and service B have to make a tcp connection to communicate. – Endlesscode Apr 02 '15 at 02:31
  • As far as I know, Zookeeper doesn't provide this, you cannot get the IP of a client that created a znode. You really need to put this in the znode data yourself. Not sure what your specific problems with configuration are, but in general every service should publish its own addressing info when it is registering itself to Zookeper, be it IP or cname or something else. – igorbel Apr 02 '15 at 10:13