我正在尝试为Hazelcast集群编写单元测试- 根据这份文件 https://hazelcast.zendesk.com/hc/en-us/articles/115004425243-How-do-I-test-my-Hazelcast-cluster-
我以这种方式编写了我的代码-
public void test(){
HazelcastInstance instance = Hazelcast.newHazelcastInstance();
HazelcastInstance instance2 = Hazelcast.newHazelcastInstance();
}
我的控制台显示-
Members [1] {
Member [MY IP]:5701 - this
}
和
Members [1] {
Member [MY IP]:5702 - this
}
端口号更改,IP保持不变,但它们不在同一群集中。如何使它们成为同一群集的一部分(在单个JVM上)? 更新
更多控制台数据-
Jul 31, 2018 3:21:00 PM com.hazelcast.config.XmlConfigLocator
INFO: Loading 'hazelcast-default.xml' from classpath.
Jul 31, 2018 3:21:01 PM com.hazelcast.instance.DefaultAddressPicker
INFO: [LOCAL] [dev] [3.7.4] Prefer IPv4 stack is true.
Jul 31, 2018 3:21:01 PM com.hazelcast.instance.DefaultAddressPicker
INFO: [LOCAL] [dev] [3.7.4] Picked [MY_IP]:5701, using socket ServerSocket[addr=/0:0:0:0:0:0:0:0,localport=5701], bind any local is true
Jul 31, 2018 3:21:01 PM com.hazelcast.system
INFO: [MY_IP]:5701 [dev] [3.7.4] Hazelcast 3.7.4 (20161209 - 3df1bb5) starting at [MY_IP]:5701
Jul 31, 2018 3:21:01 PM com.hazelcast.system
INFO: [MY_IP]:5701 [dev] [3.7.4] Copyright (c) 2008-2016, Hazelcast, Inc. All Rights Reserved.
Jul 31, 2018 3:21:01 PM com.hazelcast.system
INFO: [MY_IP]:5701 [dev] [3.7.4] Configured Hazelcast Serialization version : 1
Jul 31, 2018 3:21:01 PM com.hazelcast.spi.impl.operationservice.impl.BackpressureRegulator
INFO: [MY_IP]:5701 [dev] [3.7.4] Backpressure is disabled
Jul 31, 2018 3:21:01 PM com.hazelcast.instance.Node
INFO: [MY_IP]:5701 [dev] [3.7.4] Creating MulticastJoiner
Jul 31, 2018 3:21:01 PM com.hazelcast.core.LifecycleService
INFO: [MY_IP]:5701 [dev] [3.7.4] [MY_IP]:5701 is STARTING
Jul 31, 2018 3:21:02 PM com.hazelcast.spi.impl.operationexecutor.impl.OperationExecutorImpl
INFO: [MY_IP]:5701 [dev] [3.7.4] Starting 4 partition threads
Jul 31, 2018 3:21:02 PM com.hazelcast.spi.impl.operationexecutor.impl.OperationExecutorImpl
INFO: [MY_IP]:5701 [dev] [3.7.4] Starting 3 generic threads (1 dedicated for priority tasks)
Jul 31, 2018 3:21:02 PM com.hazelcast.nio.tcp.nonblocking.NonBlockingIOThreadingModel
INFO: [MY_IP]:5701 [dev] [3.7.4] TcpIpConnectionManager configured with Non Blocking IO- threading model: 3 input threads and 3 output threads
Jul 31, 2018 3:21:05 PM com.hazelcast.internal.cluster.impl.MulticastJoiner
INFO: [MY_IP]:5701 [dev] [3.7.4]
Members [1] {
Member [MY_IP]:5701 -
this
}
Jul 31, 2018 3:21:05 PM com.hazelcast.core.LifecycleService
INFO: [MY_IP]:5701 [dev] [3.7.4] [MY_IP]:5701 is
STARTED
Jul 31, 2018 3:21:05 PM com.hazelcast.config.XmlConfigLocator
INFO: Loading 'hazelcast-default.xml' from classpath.
Jul 31, 2018 3:21:05 PM com.hazelcast.instance.DefaultAddressPicker
INFO: [LOCAL] [dev] [3.7.4] Prefer IPv4 stack is true.
Jul 31, 2018 3:21:05 PM com.hazelcast.instance.DefaultAddressPicker
INFO: [LOCAL] [dev] [3.7.4] Picked [MY_IP]:5702, using socket
ServerSocket[addr=/0:0:0:0:0:0:0:0,localport=5702], bind any local is
true
Jul 31, 2018 3:21:05 PM com.hazelcast.system
INFO: [MY_IP]:5702 [dev] [3.7.4] Hazelcast 3.7.4 (20161209 -
3df1bb5) starting at [MY_IP]:5702
Jul 31, 2018 3:21:05 PM com.hazelcast.system
INFO: [MY_IP]:5702 [dev] [3.7.4] Copyright (c) 2008-2016,
Hazelcast, Inc. All Rights Reserved.
Jul 31, 2018 3:21:05 PM com.hazelcast.system
INFO: [MY_IP]:5702 [dev] [3.7.4] Configured Hazelcast
Serialization version : 1
Jul 31, 2018 3:21:05 PM
com.hazelcast.spi.impl.operationservice.impl.BackpressureRegulator
INFO: [MY_IP]:5702 [dev] [3.7.4] Backpressure is disabled
Jul 31, 2018 3:21:05 PM com.hazelcast.instance.Node
INFO: [MY_IP]:5702 [dev] [3.7.4] Creating MulticastJoiner
Jul 31, 2018 3:21:05 PM com.hazelcast.core.LifecycleService
INFO: [MY_IP]:5702 [dev] [3.7.4] [MY_IP]:5702 is
Jul 31, 2018 3:21:05 PM
com.hazelcast.spi.impl.operationexecutor.impl.OperationExecutorImpl
INFO: [MY_IP]:5702 [dev] [3.7.4] Starting 4 partition threads
Jul 31, 2018 3:21:05 PM
com.hazelcast.spi.impl.operationexecutor.impl.OperationExecutorImpl
INFO: [MY_IP]:5702 [dev] [3.7.4] Starting 3 generic threads (1
dedicated for priority tasks)
Jul 31, 2018 3:21:05 PM
com.hazelcast.nio.tcp.nonblocking.NonBlockingIOThreadingModel
INFO: [MY_IP]:5702 [dev] [3.7.4] TcpIpConnectionManager
configured with Non Blocking IO-threading model: 3 input threads and 3
output threads
Jul 31, 2018 3:21:08 PM
com.hazelcast.internal.cluster.impl.MulticastJoiner
INFO: [MY_IP]:5702 [dev] [3.7.4]
Members [1] {
Member [MY_IP]:5702 -
this
}
Jul 31, 2018 3:21:08 PM com.hazelcast.instance.Node
WARNING: [MY_IP]:5702 [dev] [3.7.4] Config seed port is 5701
and cluster size is 1. Some of the ports seem occupied!
Jul 31, 2018 3:21:08 PM com.hazelcast.core.LifecycleService
INFO: [MY_IP]:5702 [dev] [3.7.4] [MY_IP]:5702 is
STARTED
答案 0 :(得分:1)
如您所述,您正在使用默认的Hazelcast配置,如日志中所示
INFO: Loading 'hazelcast-default.xml' from classpath.
默认的Hazelcast配置用于多播。这是快速启动多台计算机的理想选择,而不是一台计算机那么好。
第一个Hazelcast.newHazelcastInstance()
将在端口5701上启动多播组。
第二个Hazelcast.newHazelcastInstance()
将发现正在使用的端口5701,将占用下一个端口5702,并在5702上启动多播组。
换句话说,第二个实例可以在同一端口上,因此不能在同一组中(因为该组基于端口)。因此,您最终得到了两个不需要的簇。
有四个步骤可以实现您想要的。
hazelcast.xml
文件。 关闭多播不会打开TCP。您可以选择不进行发现(对某些测试有用)。
所以您需要一个看起来像这样的hazelcast.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<hazelcast xsi:schemaLocation="http://www.hazelcast.com/schema/config hazelcast-config-3.10.xsd"
xmlns="http://www.hazelcast.com/schema/config"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<network>
<join>
<multicast enabled="false"/>
<tcp-ip enabled="true">
<interface>127.0.0.1</interface>
</tcp-ip>
</join>
</network>
</hazelcast>
答案 1 :(得分:1)
对于单个JVM或同一台机器上的多个JVM中的多个实例,默认配置将正常工作。
错误 Dim myTime = DateTime.Parse(Read24HFormatTime())
Dim name = "Emplyee"
Console.WriteLine($"{vbCrLf}Hello, {name}, at {myTime:t}")
Console.ReadKey(True)
是 Red Herring 。
在同一台计算机上启动多个实例时,它将选择一个不同的端口。但是,以上错误确实表明您的实例之间存在通信问题。
如果没有通讯问题,您将在日志中看到不同的输出。 辅助实例在启动时将输出类似这样的信息(注意它仍在使用5702)。
Config seed port is 5701 and cluster size is 1. Some of the ports seem occupied!
就我而言,解决此问题就像在本地计算机上禁用防火墙一样简单,因为我绑定了路由器分配的DHCP地址。如果绑定到回送地址,则需要确保OS上的回送可以正确地与多播一起使用。当通过环回接口使用多播时,OSX存在一些已知的Java问题。 https://bugs.java.com/bugdatabase/view_bug.do?bug_id=7122846
答案 2 :(得分:1)
经历了这些事情之后: https://docs.hazelcast.org/docs/1.9/manual/html/ch16s02.html
我得出的结论是,我只需要一个更好的配置即可告诉Hazelcast将2个实例威胁为1个群集。
private val hazelcastOnNode1 = newHazelcastInstance("hazelcastInstance-cluster-node1")
private val hazelcastOnNode2 = newHazelcastInstance("hazelcastInstance-cluster-node2")
@BeforeAll
fun setupTest() {
log.info("hazelcastOnNode1: ${hazelcastOnNode1.name} is part of a cluster: ${hazelcastOnNode1.cluster}")
log.info("hazelcastOnNode2: ${hazelcastOnNode2.name} is part of a cluster: ${hazelcastOnNode2.cluster}")
}
private fun newHazelcastInstance(instanceName: String): HazelcastInstance {
val cfg = Config()
cfg.instanceName = instanceName
cfg.groupConfig.name = "hazel-cluster"
cfg.networkConfig.join.multicastConfig.isEnabled = false
cfg.networkConfig.join.tcpIpConfig.isEnabled = true
cfg.networkConfig.join.tcpIpConfig.members = listOf("127.0.0.1")
return Hazelcast.newHazelcastInstance(cfg)
}