views:

156

answers:

4

Hi!

I currently participate in a project where we/the applicaition need to be able to discover other instances of the application with the same application name running on a LAN (henceforth called Node).

Prerequisites: All Nodes know their own IP address and TCP port number All Nodes have a name All Nodes have access to the LAN

What I need:

All Nodes needs to know the IP address and TCP port of each other Node. If a Node goes down, I will have to be programmatically notified of this. If a new Node comes up, I will have to be programmatically notified of this. It's ESSENTIAL that no master server or other application is needed, it has to be an API that I can integrate into the current application. Also, it has to be open source and preferrably MIT or ApacheV2 licensed.

That's all!

The application is JVM-based so any API will do. I've been looking at ZooKeeper but it seems to be quite a big dependency for the little functionality we need.

And, if you don't know any API, but have some good links to share as how to achieve this by writing it myself (white papers, blogs, books, whatnot), I'd more than gladly accept things like that aswell.

So the question is, how do I do this?

A: 

If you're all on the same subnet, a simple solution would be to use multicast sockets. Pick an address (well, make it configurable) and then when a new instance starts up it sends a multicast "ping". This notifies the other servers.

Of course this won't tell you when an instance goes down. You have two choices for that:

  1. Ping often enough that when you fail to receive a ping for a certain amount of time you assume the instance is down; or
  2. When you are notified of a new instance you open a TCP connection to that instance. You have to ping that socket far less often to keep it open (inactivity will still kill it eventually) and you can be notified of it being closed by the other side or the TCP semantics for socket timeout.

You will need 1-2 threads to monitor this and trigger appropriate events.

cletus
Absolutely Cletus, the thing is that building it from scratch with primitive building blocks (handrolled heartbeats etc) will probably increase the risk that the solution has edge-cases and reliability perhaps suffers. That's why I was hoping to leverage standing on the soulders of some giant :)
Viktor Klang
I know what you mean but it seems that every time I go looking for a library to solve a small yet complex problem you just trade one set of corner cases for a set of corner cases specific to the library.
cletus
@Cletus - have a look at ZeroConf - it is what you describe done right and of production quality.
Thorbjørn Ravn Andersen
+1  A: 

There are a bunch of options for this sort of thing, my person favourite is hazelcast it's Apache licensed and has zero dependencies and a minimum of configuration.

A more established option is jgroups this licensed under the LGPL.

Gareth Davis
Gid, this looks interesting, thanks for the tip.
Viktor Klang
I knew about jgroups and I'm using it, but thanks to your answer I just discovered hazelcast and it looks very interesting...
pgras
+1  A: 

I'd definitely recommend JGroups which is pretty nice and very actively worked on and supports any type of discovery (IP multicast, fixed list, external lookup service etc) over any transport.

Another option would be Apache MINA but JGroups is IMO better for discovery (because it supports any type of discovery).

Pascal Thivent
JGroups looks nice, and I also found Apache Tribes through another channel. Thanks a bunch! : )
Viktor Klang
+1  A: 

The absolute easiest way - and simplest in my opinion - to do this is with ZeroConf!

Each node announce themselves, and listen to when others show up and dissappear. No central registry, just multicast DNS. Works exceedingly well - just look at OS X.

The portable approach is to use jmdns - http://jmdns.sourceforge.net/ - the OS X way is to use the Apple Java facade.

Thorbjørn Ravn Andersen