昨天,我多次启动了两个嵌入式 hazelcast 节点,每次第二个节点启动时,它们都会加入到一个集群中。从今天早上开始,他们不再加入集群。这是启动节点的最少代码:
object BrokenHazelcastAutodiscovery extends App {
val cfg = new Config()
Hazelcast.newHazelcastInstance(cfg)
}
我在 Macbook 上运行了它。我不确定昨晚是否让节点保持运行。假设我已经并且假设他们一直在不时地使用多播发现数据包 ping 公司网络。我想,网络管理员以某种方式关闭了我笔记本电脑上的多播功能。好吧,这是一段代码,用于检查是否可以在本地主机上多播和接收多播数据包:
object WhetherMulticastEnabledCheck {
import java.net._
def main(args: Array[String]): Unit = {
val port = 33766
val addr = new InetSocketAddress(InetAddress.getByName("225.6.7.8"), port)
val netif = NetworkInterface.getByInetAddress(InetAddress.getLocalHost)
val sok = new MulticastSocket(addr.getPort)
sok.joinGroup(addr, netif)
sok.setTimeToLive(0)
val anotherStarted =
try { new ServerSocket(port); false }
catch { case _: BindException => true }
val seq = anotherStarted match {
case true => new Sequence(0xFFFF0000)
case false => new Sequence(0x0000FFFF)
}
new Receiver(sok).start()
new Sender(sok, addr, seq).start()
}
class Sequence(mask: Int) {
private var i = 0
def next: Int = {
i += 1
if (i > 0x0FFF) i = 1
((i << 16) | i) & mask
}
}
private class Sender(sok: DatagramSocket, to: SocketAddress, seq: Sequence) {
private val t = new Thread(() => {
val buf = new Array[Byte](4)
val p = new DatagramPacket(buf, buf.length, to)
println("sending started...")
while (true) {
val t = seq.next
buf.update(0, (t >> 24).toByte)
buf.update(1, (t >> 16).toByte)
buf.update(2, (t >> 8).toByte)
buf.update(3, (t >> 0).toByte)
sok.send(p)
println(s"sent ${t.formatted("%08X").replace('0', '.')}")
Thread.sleep(2500)
}
}, "sending-thread")
def start(): Unit = t.start()
}
private class Receiver(sok: DatagramSocket) {
private val t = new Thread(() => {
val buf = new Array[Byte](4)
val p = new DatagramPacket(buf, buf.length)
println("receiving started...")
while (true) {
sok.receive(p)
val t =
(buf(0) << 24) |
(buf(1) << 16) |
(buf(2) << 8) |
(buf(3) << 0)
println(s"received ${t.formatted("%08X").replace('0', '.')}")
}
}, "receiving-thread")
def start(): Unit = t.start()
}
}
这是输出
proc 1 | proc 2
---------------------|----------------------
receiving started... |
sending started... | sending started...
sent .......1 | receiving started...
received .......1 | received ...1....
received ...1.... | sent ...1....
sent .......2 | received .......2
received .......2 | sent ...2....
received ...2.... | received ...2....
sent .......3 | received .......3
...
我手动将其并排放置,并将第二个进程的输出移动一行,以帮助在视觉上捕捉开始时刻的变化。输出意味着多播在我的笔记本电脑上工作。
那么,你们能建议一下可能会出现什么问题,导致节点停止相互发现并加入集群吗?
我第二次重新启动了笔记本电脑,
BrokenHazelcastAutodiscovery
又开始工作了。 🤷🏼