Gossip
Gossip protocol is a method for a group of nodes to discover and check the liveliness of a cluster. More information can be found at http://en.wikipedia.org/wiki/Gossip_protocol.
Usage
Maven
<dependency>
<groupId>net.lvsq</groupId>
<artifactId>jgossip</artifactId>
<version>1.3.2</version>
</dependency>
First you need one or more seed members
List<SeedMember> seedNodes = new ArrayLis<>();
SeedMember seed = new SeedMember();
seed.setCluster(cluster);
seed.setIpAddress(ipAddress);
seed.setPort(port);
seedNodes.add(seed);
GossipService
object
Then, instantiation a GossipService gossipService = new GossipService(cluster,ipAddress, port, id, seedNodes, new GossipSettings(), (member, state) -> {});
GossipService
Run gossipService.start();
Stop
gossipService.shutdown();
Settings
- gossipInterval - How often (in milliseconds) to gossip list of members to other node(s). Default is 1000ms
- networkDelay - Network delay in ms. Default is 200ms
- msgService - Which message sync implementation. Default is UDPMsgService.class use UDP protocol to send message, certainly you can extand it.
- deleteThreshold - Delete the deadth node when the sync message is not received more than [deleteThreshold] times. Default is 3
Event Listener
Now, we have three kinds of event
GossipState.UP;
GossipState.DOWN;
GossipState.JOIN;
Example
int gossip_port = 60001;
String cluster = "gossip_cluster";
GossipSettings settings = new GossipSettings();
settings.setGossipInterval(1000);
try {
String myIpAddress = InetAddress.getLocalHost().getHostAddress();
List<SeedMember> seedNodes = new ArrayList<>();
SeedMember seed = new SeedMember();
seed.setCluster(cluster);
seed.setIpAddress(myIpAddress);
seed.setPort(60001);
seedNodes.add(seed);
gossipService = new GossipService(cluster, myIpAddress, gossip_port, null, seedNodes, settings, (member, state) ->System.out.println("member:" + member + " state: " + state));
} catch (Exception e) {
e.printStackTrace();
}
gossipService.start();