Git Product home page Git Product logo

jgossip's Introduction

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.5.0</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);

Then, instantiation a GossipService object

GossipService gossipService = new GossipService(cluster,ipAddress, port, id, seedNodes, new GossipSettings(), (member, state) -> {
        //Do anything what you want
    });

Run GossipService

gossipService.start();

Stop

gossipService.shutdown();

Get offline nodes

gossipService.getGossipManager().getDeadMembers();

Get online nodes

gossipService.getGossipManager().getLiveMembers();

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

Currently, jgossip has four events

GossipState.UP;
GossipState.DOWN;
GossipState.JOIN;
GossipState.RCV;

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, payload) -> {
        if (state == GossipState.RCV) {
            System.out.println("member:" + member + "  state: " + state + " payload: " + payload);
        }
        if (state == GossipState.DOWN) {
            System.out.println("[[[[[[[[[member:" + member + "  was down!!! ]]]]]]]]]");
        }});
} catch (Exception e) {
    e.printStackTrace();
}
gossipService.start();
        

Run the above code in each application to create a cluster based on the Gossip protocol. You can provide a meaningful GossipListener as the last parameter of GossipService. When state of a node changes, you can capture this change and make some responses.

Publish Messages

If you want to send messages to gossip cluster:

gossipService.getGossipManager().publish("Hello World");

If a node in cluster received messages, it will trigger the GossipState.RCV event, and handler predefined by GossipListener can consume these messages, such as "Hello World" above.

The type of message is arbitrary, but only if it can be serialized. jgossip will try its best to deliver to every node. By default, messages will stay in memory for a while, and then jgossip will automatically delete them. So the best scenario for this feature is to send some simple messages regularly.

Please pay attention, if you need to send mass messages in a short time, which will consume a lot of resources, this requires you to weigh the actual situation.

jgossip's People

Contributors

monkeymq avatar dependabot[bot] avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.