# 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
```xml
<dependency>
<groupId>net.lvsq</groupId>
<artifactId>jgossip</artifactId>
<version>1.5.0</version>
</dependency>
```
#### First you need one or more seed members
```java
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
```java
GossipService gossipService = new GossipService(cluster,ipAddress, port, id, seedNodes, new GossipSettings(), (member, state) -> {
//Do anything what you want
});
```
#### Run `GossipService`
```java
gossipService.start();
```
#### Stop
```java
gossipService.shutdown();
```
#### Get offline nodes
```java
gossipService.getGossipManager().getDeadMembers();
```
#### Get online nodes
```java
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
```java
GossipState.UP;
GossipState.DOWN;
GossipState.JOIN;
GossipState.RCV;
```
# Example
```java
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:
```java
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 <html><span style="color: green">"Hello World"</span><html> 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.
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
一个 Java 版的 Gossip 协议实现。 Gossip 算法又被称为反熵(Anti-Entropy),熵是物理学上的一个概念,代表杂乱无章,而反熵就是在杂乱无章中寻求一致,这充分说明了 Gossip 的特点:在一个有界网络中,每个节点都随机地与其他节点通信,经过一番杂乱无章的通信,最终所有节点的状态都会达成一致。每个节点可能知道所有其他节点,也可能仅知道几个邻居节点,只要这些节可以通过网络连通,最终他们的状态都是一致的,Gossip 天然具有分布式容错的优点。 jgossip 使用很简单,配置方便,支持 JOIN、UP、DOWN 三种事件, 使用 UDP 协议进行数据同步。
资源推荐
资源详情
资源评论
收起资源包目录
一个 Java 版的 Gossip 协议实现.rar (39个子文件)
一个 Java 版的 Gossip 协议实现
新建文本文档.txt 19B
jgossip-master
pom.xml 5KB
src
test
java
net
lvsq
jgossip
TestGossipService.java 1KB
TestGossipMember.java 907B
TestGossipDigestSyncMessage.java 2KB
TestAckMessage.java 2KB
TestRegularMessage.java 1KB
main
java
net
lvsq
jgossip
handler
Ack2MessageHandler.java 1KB
SyncMessageHandler.java 4KB
MessageHandler.java 954B
AckMessageHandler.java 3KB
RegularMessageHandler.java 2KB
ShutdownMessageHandler.java 1KB
net
udp
UDPMsgService.java 4KB
MsgService.java 1KB
event
GossipListener.java 909B
core
CustomSerializer.java 1KB
GossipSettings.java 3KB
MessageManager.java 358B
GossipMessageFactory.java 2KB
VersionHelper.java 1KB
CustomDeserializer.java 1KB
GossipService.java 3KB
Serializer.java 2KB
InMemMessageManager.java 954B
GossipManager.java 22KB
model
CandidateMemberState.java 2KB
AckMessage.java 2KB
MessageType.java 974B
Ack2Message.java 2KB
HeartbeatState.java 2KB
SyncMessage.java 2KB
GossipDigest.java 3KB
GossipState.java 896B
SeedMember.java 3KB
RegularMessage.java 2KB
GossipMember.java 3KB
LICENSE 11KB
README.md 4KB
共 39 条
- 1
资源评论
野生的狒狒
- 粉丝: 3387
- 资源: 2436
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功