Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Discovery mechanics API for Java-based app

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?

like image 257
Viktor Klang Avatar asked Dec 09 '09 09:12

Viktor Klang


3 Answers

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).

like image 58
Pascal Thivent Avatar answered Nov 09 '22 00:11

Pascal Thivent


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.

like image 2
cletus Avatar answered Nov 08 '22 23:11

cletus


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.

like image 1
Gareth Davis Avatar answered Nov 08 '22 23:11

Gareth Davis