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.
<dependency>
<groupId>net.lvsq</groupId>
<artifactId>jgossip</artifactId>
<version>1.5.0</version>
</dependency>
List<SeedMember> seedNodes = new ArrayLis<>();
SeedMember seed = new SeedMember();
seed.setCluster(cluster);
seed.setIpAddress(ipAddress);
seed.setPort(port);
seedNodes.add(seed);
GossipService
objectGossipService gossipService = new GossipService(cluster,ipAddress, port, id, seedNodes, new GossipSettings(), (member, state) -> {
//Do anything what you want
});
GossipService
gossipService.start();
gossipService.shutdown();
gossipService.getGossipManager().getDeadMembers();
gossipService.getGossipManager().getLiveMembers();
Currently, jgossip has four events
GossipState.UP;
GossipState.DOWN;
GossipState.JOIN;
GossipState.RCV;
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.
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.
Вы можете оставить комментарий после Вход в систему
Неприемлемый контент может быть отображен здесь и не будет показан на странице. Вы можете проверить и изменить его с помощью соответствующей функции редактирования.
Если вы подтверждаете, что содержание не содержит непристойной лексики/перенаправления на рекламу/насилия/вульгарной порнографии/нарушений/пиратства/ложного/незначительного или незаконного контента, связанного с национальными законами и предписаниями, вы можете нажать «Отправить» для подачи апелляции, и мы обработаем ее как можно скорее.
Комментарии ( 0 )