本文介绍了如何以编程方式配置多播发现机制的hazelcast?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 如何以编程方式为多播发现机制配置hazelcast?How do you programmatically configure hazelcast for the multicast discovery mechanism?详情: 文档仅提供了TCP / IP的示例,并且已过时:它使用Config .setPort(),它不再存在。The documentation only supplies an example for TCP/IP and is out-of-date: it uses Config.setPort(), which no longer exists.我的配置看起来像这样,但发现不工作(即我得到输出 :1:My configuration looks like this, but discovery does not work (i.e. I get the output "Members: 1": Config cfg = new Config(); NetworkConfig network = cfg.getNetworkConfig(); network.setPort(PORT_NUMBER); JoinConfig join = network.getJoin(); join.getTcpIpConfig().setEnabled(false); join.getAwsConfig().setEnabled(false); join.getMulticastConfig().setEnabled(true); join.getMulticastConfig().setMulticastGroup(MULTICAST_ADDRESS); join.getMulticastConfig().setMulticastPort(PORT_NUMBER); join.getMulticastConfig().setMulticastTimeoutSeconds(200); HazelcastInstance instance = Hazelcast.newHazelcastInstance(cfg); System.out.println("Members: "+hazelInst.getCluster().getMembers().size()); 更新1,考虑asimarslan的回答 如果我不理解MulticastTimeout,我得到Members:1或 Dec 05,2013 8:50:42 PM com.hazelcast.nio.ReadHandler警告: [192.168.0.9]:4446 [dev] hz._hzInstance_1_dev .IO.thread-in-0关闭套接字到端点地址[192.168.0.7]:4446,原因:java.io.EOFException:远程套接字关闭!显示译文显示原文双语对照语言切换进行中,请稍候...分享|更新时间:2008年12月05日08:57:24 PM com.hazelcast.instance.Node SEVERE:[192.168.0.9]:4446 [dev] com.hazelcast.core.HazelcastException:无法在300秒内加入! Dec 05, 2013 8:50:42 PM com.hazelcast.nio.ReadHandler WARNING: [192.168.0.9]:4446 [dev] hz._hzInstance_1_dev.IO.thread-in-0 Closing socket to endpoint Address[192.168.0.7]:4446, Cause:java.io.EOFException: Remote socket closed! Dec 05, 2013 8:57:24 PM com.hazelcast.instance.Node SEVERE: [192.168.0.9]:4446 [dev] Could not join cluster, shutting down! com.hazelcast.core.HazelcastException: Failed to join in 300 seconds! 更新2,用pveentjer的答案关于使用tcp / ip帐户 如果我将配置更改为以下,我仍然只有1个成员:Update 2, taking pveentjer's answer about using tcp/ip into accountIf I change the configuration to the following, I still only get 1 member:Config cfg = new Config();NetworkConfig network = cfg.getNetworkConfig();network.setPort(PORT_NUMBER);JoinConfig join = network.getJoin();join.getMulticastConfig().setEnabled(false);join.getTcpIpConfig().addMember("192.168.0.1").addMember("192.168.0.2").addMember("192.168.0.3").addMember("192.168.0.4").addMember("192.168.0.5").addMember("192.168.0.6").addMember("192.168.0.7").addMember("192.168.0.8").addMember("192.168.0.9").addMember("192.168.0.10").addMember("192.168.0.11").setRequiredMember(null).setEnabled(true);//this sets the allowed connections to the cluster? necessary for multicast, too?network.getInterfaces().setEnabled(true).addInterface("192.168.0.*");HazelcastInstance instance = Hazelcast.newHazelcastInstance(cfg);System.out.println("debug: joined via "+join+" with "+hazelInst.getCluster().getMembers().size()+" members.");更精确地说,此运行产生输出More precisely, this run produces the output debug:通过JoinConfig加入{multicastConfig = MulticastConfig [enabled = false,multicastGroup = 224.2.2.3,multicastPort = 54327, multicastTimeToLive = 32,multicastTimeoutSeconds = 2, trustedInterfaces = []],tcpIpConfig = TcpIpConfig [enabled = true, connectionTimeoutSeconds = 5,members = [192.168.0.1,192.168.0.2, 192.168.0.3,192.168.0.4,192.168。 0.5,192.168.0.6,192.168.0.7,192.168.0.8,192.168.0.9,192.168.0.10,192.168.0.11],requiredMember = null],awsConfig = AwsConfig {enabled = false, region ='us-east -1',securityGroupName ='null',tagKey ='null', tagValue ='null',hostHeader ='ec2.amazonaws.com', connectionTimeoutSeconds = 5}}。 / p> debug: joined via JoinConfig{multicastConfig=MulticastConfig [enabled=false, multicastGroup=224.2.2.3, multicastPort=54327, multicastTimeToLive=32, multicastTimeoutSeconds=2, trustedInterfaces=[]], tcpIpConfig=TcpIpConfig [enabled=true, connectionTimeoutSeconds=5, members=[192.168.0.1, 192.168.0.2, 192.168.0.3, 192.168.0.4, 192.168.0.5, 192.168.0.6, 192.168.0.7, 192.168.0.8, 192.168.0.9, 192.168.0.10, 192.168.0.11], requiredMember=null], awsConfig=AwsConfig{enabled=false, region='us-east-1', securityGroupName='null', tagKey='null', tagValue='null', hostHeader='ec2.amazonaws.com', connectionTimeoutSeconds=5}} with 1 members.我的非hazelcast实现使用UDP多播,工作正常。那么防火墙真的会是这个问题吗?My non-hazelcast-implementation is using UDP multicasts and works fine. So can a firewall really be the problem?由于我没有iptables的权限或安装iperf,我使用 com.hazelcast.examples.TestApp 来检查我的网络是否正常工作,如 Hazelcast入门在第2章显示直走:Since I do not have permissions for iptables or to install iperf, I am using com.hazelcast.examples.TestApp to check whether my network is working, as described in Getting Started With Hazelcast in Chapter 2, Section "Showing Off Straight Away":我调用 java -cp hazelcast-3.1.2.jar com.hazelcast。 example.TestApp 在192.168.0.1上获取输出I call java -cp hazelcast-3.1.2.jar com.hazelcast.examples.TestApp on 192.168.0.1 and get the output...Dec 10, 2013 11:31:21 PM com.hazelcast.instance.DefaultAddressPickerINFO: Prefer IPv4 stack is true.Dec 10, 2013 11:31:21 PM com.hazelcast.instance.DefaultAddressPickerINFO: Picked Address[192.168.0.1]:5701, using socket ServerSocket[addr=/0:0:0:0:0:0:0:0,localport=5701], bind any local is trueDec 10, 2013 11:31:22 PM com.hazelcast.systemINFO: [192.168.0.1]:5701 [dev] Hazelcast Community Edition 3.1.2 (20131120) starting at Address[192.168.0.1]:5701Dec 10, 2013 11:31:22 PM com.hazelcast.systemINFO: [192.168.0.1]:5701 [dev] Copyright (C) 2008-2013 Hazelcast.comDec 10, 2013 11:31:22 PM com.hazelcast.instance.NodeINFO: [192.168.0.1]:5701 [dev] Creating MulticastJoinerDec 10, 2013 11:31:22 PM com.hazelcast.core.LifecycleServiceINFO: [192.168.0.1]:5701 [dev] Address[192.168.0.1]:5701 is STARTINGDec 10, 2013 11:31:24 PM com.hazelcast.cluster.MulticastJoinerINFO: [192.168.0.1]:5701 [dev]Members [1] { Member [192.168.0.1]:5701 this}Dec 10, 2013 11:31:24 PM com.hazelcast.core.LifecycleServiceINFO: [192.168.0.1]:5701 [dev] Address[192.168.0.1]:5701 is STARTED然后我在192.168.0.2上调用 java -cp hazelcast-3.1.2.jar com.hazelcast.examples.TestApp 并获取输出I then call java -cp hazelcast-3.1.2.jar com.hazelcast.examples.TestApp on 192.168.0.2 and get the output...Dec 10, 2013 9:50:22 PM com.hazelcast.instance.DefaultAddressPickerINFO: Prefer IPv4 stack is true.Dec 10, 2013 9:50:22 PM com.hazelcast.instance.DefaultAddressPickerINFO: Picked Address[192.168.0.2]:5701, using socket ServerSocket[addr=/0:0:0:0:0:0:0:0,localport=5701], bind any local is trueDec 10, 2013 9:50:23 PM com.hazelcast.systemINFO: [192.168.0.2]:5701 [dev] Hazelcast Community Edition 3.1.2 (20131120) starting at Address[192.168.0.2]:5701Dec 10, 2013 9:50:23 PM com.hazelcast.systemINFO: [192.168.0.2]:5701 [dev] Copyright (C) 2008-2013 Hazelcast.comDec 10, 2013 9:50:23 PM com.hazelcast.instance.NodeINFO: [192.168.0.2]:5701 [dev] Creating MulticastJoinerDec 10, 2013 9:50:23 PM com.hazelcast.core.LifecycleServiceINFO: [192.168.0.2]:5701 [dev] Address[192.168.0.2]:5701 is STARTINGDec 10, 2013 9:50:23 PM com.hazelcast.nio.SocketConnectorINFO: [192.168.0.2]:5701 [dev] Connecting to /192.168.0.1:5701, timeout: 0, bind-any: trueDec 10, 2013 9:50:23 PM com.hazelcast.nio.TcpIpConnectionManagerINFO: [192.168.0.2]:5701 [dev] 38476 accepted socket connection from /192.168.0.1:5701Dec 10, 2013 9:50:28 PM com.hazelcast.cluster.ClusterServiceINFO: [192.168.0.2]:5701 [dev]Members [2] { Member [192.168.0.1]:5701 Member [192.168.0.2]:5701 this}Dec 10, 2013 9:50:30 PM com.hazelcast.core.LifecycleServiceINFO: [192.168.0.2]:5701 [dev] Address[192.168.0.2]:5701 is STARTED因此,多播发现通常在我的集群上工作,对吧? 5701也是发现的端口?在上一个输出中是 38476 ID还是端口?So multicast discovery is generally working on my cluster, right? Is 5701 also the port for discovery? Is 38476 in the last output an ID or a port?加入仍然无法使用程式配置的自己的程式码:(Joining still does not work for my own code with programmatical configuration :(修改的TestApp提供输出The modified TestApp gives the outputjoinConfig{multicastConfig=MulticastConfig [enabled=true, multicastGroup=224.2.2.3,multicastPort=54327, multicastTimeToLive=32, multicastTimeoutSeconds=2,trustedInterfaces=[]], tcpIpConfig=TcpIpConfig [enabled=false,connectionTimeoutSeconds=5, members=[], requiredMember=null],awsConfig=AwsConfig{enabled=false, region='us-east-1', securityGroupName='null',tagKey='null', tagValue='null', hostHeader='ec2.amazonaws.com', connectionTimeoutSeconds=5}},并在几秒钟后检测其他成员(如果每个实例一次只列出其自身作为成员,如果所有都同时启动)而and does detect other members after a couple of seconds (after each instance once lists only itself as a member if all are started at the same time), whereas myProgram给出了通过JoinConfig连接的输出myProgram gives the outputjoined via JoinConfig{multicastConfig=MulticastConfig [enabled=true, multicastGroup=224.2.2.3, multicastPort=54327, multica\stTimeToLive=32, multicastTimeoutSeconds=2, trustedInterfaces=[]], tcpIpConfig=TcpIpConfig [enabled=false, connectionTimeoutSecond\s=5, members=[], requiredMember=null], awsConfig=AwsConfig{enabled=false, region='us-east-1', securityGroupName='null', tagKey='nu\ll', tagValue='null', hostHeader='ec2.amazonaws.com', connectionTimeoutSeconds=5}} with 1 members.并且在其运行时间内检测不到约1分钟的成员秒)。and does not detect members within its runtime of about 1 minute (I am counting the members about every 5 seconds).但是如果TestApp的至少一个实例在集群上同时运行,则会检测到所有TestApp实例和所有myProgram实例,并且我的程序运行正常。如果我启动TestApp一次,然后myProgram并行两次,TestApp提供以下输出:BUT if at least one instance of TestApp runs concurrently on the cluster, all TestApp instances and all myProgram instances are detected and my program works fine. In case I start TestApp once and then myProgram twice in parallel, TestApp gives the following output:java -cp ~/CaseStudy/jtorx-1.10.0-beta8/lib/hazelcast-3.1.2.jar:. TestAppDec 12, 2013 12:02:15 PM com.hazelcast.instance.DefaultAddressPickerINFO: Prefer IPv4 stack is true.Dec 12, 2013 12:02:15 PM com.hazelcast.instance.DefaultAddressPickerINFO: Picked Address[192.168.180.240]:5701, using socket ServerSocket[addr=/0:0:0:0:0:0:0:0,localport=5701], bind any local is trueDec 12, 2013 12:02:15 PM com.hazelcast.systemINFO: [192.168.180.240]:5701 [dev] Hazelcast Community Edition 3.1.2 (20131120) starting at Address[192.168.180.240]:5701Dec 12, 2013 12:02:15 PM com.hazelcast.systemINFO: [192.168.180.240]:5701 [dev] Copyright (C) 2008-2013 Hazelcast.comDec 12, 2013 12:02:15 PM com.hazelcast.instance.NodeINFO: [192.168.180.240]:5701 [dev] Creating MulticastJoinerDec 12, 2013 12:02:15 PM com.hazelcast.core.LifecycleServiceINFO: [192.168.180.240]:5701 [dev] Address[192.168.180.240]:5701 is STARTINGDec 12, 2013 12:02:21 PM com.hazelcast.cluster.MulticastJoinerINFO: [192.168.180.240]:5701 [dev]Members [1] { Member [192.168.180.240]:5701 this}Dec 12, 2013 12:02:22 PM com.hazelcast.core.LifecycleServiceINFO: [192.168.180.240]:5701 [dev] Address[192.168.180.240]:5701 is STARTEDDec 12, 2013 12:02:22 PM com.hazelcast.management.ManagementCenterServiceINFO: [192.168.180.240]:5701 [dev] Hazelcast will connect to Management Center on address: http://localhost:8080/mancenter-3.1.2/Join: JoinConfig{multicastConfig=MulticastConfig [enabled=true, multicastGroup=224.2.2.3, multicastPort=54327, multicastTimeToLive=32, multicastTimeoutSeconds=2, trustedInterfaces=[]], tcpIpConfig=TcpIpConfig [enabled=false, connectionTimeoutSeconds=5, members=[], requiredMember=null], awsConfig=AwsConfig{enabled=false, region='us-east-1', securityGroupName='null', tagKey='null', tagValue='null', hostHeader='ec2.amazonaws.com', connectionTimeoutSeconds=5}}Dec 12, 2013 12:02:22 PM com.hazelcast.partition.PartitionServiceINFO: [192.168.180.240]:5701 [dev] Initializing cluster partition table first arrangement...hazelcast[default] > Dec 12, 2013 12:03:27 PM com.hazelcast.nio.SocketAcceptorINFO: [192.168.180.240]:5701 [dev] Accepting socket connection from /192.168.0.8:38764Dec 12, 2013 12:03:27 PM com.hazelcast.nio.TcpIpConnectionManagerINFO: [192.168.180.240]:5701 [dev] 5701 accepted socket connection from /192.168.0.8:38764Dec 12, 2013 12:03:27 PM com.hazelcast.nio.SocketAcceptorINFO: [192.168.180.240]:5701 [dev] Accepting socket connection from /192.168.0.7:54436Dec 12, 2013 12:03:27 PM com.hazelcast.nio.TcpIpConnectionManagerINFO: [192.168.180.240]:5701 [dev] 5701 accepted socket connection from /192.168.0.7:54436Dec 12, 2013 12:03:32 PM com.hazelcast.partition.PartitionServiceINFO: [192.168.180.240]:5701 [dev] Re-partitioning cluster data... Migration queue size: 181Dec 12, 2013 12:03:32 PM com.hazelcast.cluster.ClusterServiceINFO: [192.168.180.240]:5701 [dev]Members [3] { Member [192.168.180.240]:5701 this Member [192.168.0.8]:5701 Member [192.168.0.7]:5701}Dec 12, 2013 12:03:43 PM com.hazelcast.partition.PartitionServiceINFO: [192.168.180.240]:5701 [dev] Re-partitioning cluster data... Migration queue size: 181Dec 12, 2013 12:03:45 PM com.hazelcast.partition.PartitionServiceINFO: [192.168.180.240]:5701 [dev] All migration tasks has been completed, queues are empty.Dec 12, 2013 12:03:46 PM com.hazelcast.nio.TcpIpConnectionINFO: [192.168.180.240]:5701 [dev] Connection [Address[192.168.0.8]:5701] lost. Reason: Socket explicitly closedDec 12, 2013 12:03:46 PM com.hazelcast.cluster.ClusterServiceINFO: [192.168.180.240]:5701 [dev] Removing Member [192.168.0.8]:5701Dec 12, 2013 12:03:46 PM com.hazelcast.cluster.ClusterServiceINFO: [192.168.180.240]:5701 [dev]Members [2] { Member [192.168.180.240]:5701 this Member [192.168.0.7]:5701}Dec 12, 2013 12:03:48 PM com.hazelcast.partition.PartitionServiceINFO: [192.168.180.240]:5701 [dev] Partition balance is ok, no need to re-partition cluster data...Dec 12, 2013 12:03:48 PM com.hazelcast.nio.TcpIpConnectionINFO: [192.168.180.240]:5701 [dev] Connection [Address[192.168.0.7]:5701] lost. Reason: Socket explicitly closedDec 12, 2013 12:03:48 PM com.hazelcast.cluster.ClusterServiceINFO: [192.168.180.240]:5701 [dev] Removing Member [192.168.0.7]:5701Dec 12, 2013 12:03:48 PM com.hazelcast.cluster.ClusterServiceINFO: [192.168.180.240]:5701 [dev]Members [1] { Member [192.168.180.240]:5701 this}Dec 12, 2013 12:03:48 PM com.hazelcast.partition.PartitionServiceINFO: [192.168.180.240]:5701 [dev] Partition balance is ok, no need to re-partition cluster data...我在TestApp的配置中看到的唯一区别是The only difference I see in TestApp's configuration isconfig.getManagementCenterConfig().setEnabled(true); config.getManagementCenterConfig().setUrl("http://localhost:8080/mancenter-"+version); for(int k=1;k<= LOAD_EXECUTORS_COUNT;k++){ config.addExecutorConfig(new ExecutorConfig("e"+k).setPoolSize(k)); }所以我在绝望的尝试中将它添加到myProgram中。但它不能解决问题 - 仍然每个实例在整个运行期间只检测自己为成员。so I added it in a desperate attempt into myProgram, too. But it does not solve the problem - still each instance only detects itself as member during the whole run.是不是程序运行时间不够长(如pveentjer所说)?Could it be that the program is not running long enough (as pveentjer put it)?我的实验似乎证实了这一点:$ b $ b如果时间 t 在 Hazelcast.newHazelcastInstance(cfg); 并初始化 cleanUp()(即不再通过榛树进行交流,而不再检查会员数量)My experiments seem to confirm this:If the time t between Hazelcast.newHazelcastInstance(cfg); and initializing cleanUp() (i.e. no longer communicating via hazelcast and no longer checking the number of members) is 少于30秒,无通信和成员:1 超过30秒:找到所有成员,奇怪似乎发生的时间比 t - 30秒)。less than 30 seconds, no communication and members: 1more than 30 seconds: all members are found and communication happens (which weirdly seems to be happening for much longer than t - 30 seconds).朦胧群集需要的现实时间跨度是30秒,还是有什么奇怪的事情发生?这是一个来自4个myPrograms的日志同时运行(查找hazelcast成员重叠30秒的实例1和实例3):Is 30 seconds a realistic time span that a hazelcast cluster needs, or is there something strange going on? Here is a log from 4 myPrograms running concurrently (looking for hazelcast-members overlaps 30 seconds for instance 1 and instance 3):instance 1: 2013-12-19T12:39:16.553+0100 LOG 0 (START) engine startedlooking for members between 2013-12-19T12:39:21.973+0100 and 2013-12-19T12:40:27.863+01002013-12-19T12:40:28.205+0100 LOG 35 (Torx-Explorer) Model SymToSim is about to\ exitinstance 2: 2013-12-19T12:39:16.592+0100 LOG 0 (START) engine startedlooking for members between 2013-12-19T12:39:22.192+0100 and 2013-12-19T12:39:28.429+01002013-12-19T12:39:28.711+0100 LOG 52 (Torx-Explorer) Model SymToSim is about to\ exitinstance 3: 2013-12-19T12:39:16.593+0100 LOG 0 (START) engine startedlooking for members between 2013-12-19T12:39:22.145+0100 and 2013-12-19T12:39:52.425+01002013-12-19T12:39:52.639+0100 LOG 54 (Torx-Explorer) Model SymToSim is about to\ exitINSTANCE 4: 2013-12-19T12:39:16.885+0100 LOG 0 (START) engine startedlooking for members between 2013-12-19T12:39:21.478+0100 and 2013-12-19T12:39:35.980+01002013-12-19T12:39:36.024+0100 LOG 34 (Torx-Explorer) Model SymToSim is about to\ exit如何在混合集群中有足够的成员之后才能最好地启动我的实际分布式算法?我可以以编程方式设置 hazelcast.initial.min.cluster.size 吗? https://groups.google.com/forum/#!topic/hazelcast/sa-lmpEDa6A 听起来像这样会阻塞 Hazelcast.newHazelcastInstance(cfg); ,直到达到initial.min.cluster.size。正确?How do I best start my actual distributed algorithm only after enough members are present in the hazelcast cluster? Can I set hazelcast.initial.min.cluster.size programmatically? https://groups.google.com/forum/#!topic/hazelcast/sa-lmpEDa6A sounds like this would block Hazelcast.newHazelcastInstance(cfg); until the initial.min.cluster.size is reached. Correct? How synchronously (within which time span) will the different instances unblock?推荐答案问题显然是集群开始(并停止),并且不会等到群集中有足够的成员。您可以设置hazelcast.initial.min.cluster.size属性,以防止这种情况发生。The problem appearently is that the cluster starts (and stops) and doesn't wait till enough members are in the cluster. You can set the hazelcast.initial.min.cluster.size property, to prevent this from happening.您可以设置'hazelcast.initial.min.cluster.size'以编程方式使用:You Can set 'hazelcast.initial.min.cluster.size' programmatically using:Config config = new Config();config.setProperty("hazelcast.initial.min.cluster.size","3"); 这篇关于如何以编程方式配置多播发现机制的hazelcast?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!