2017-01-03 48 views
0

下面是文檔中給出的HazelCast編程配置,但無法在HazelCast羣集中添加成員。HazelCast Tcp-IP的編程配置沒有在羣集中添加成員

Config cfg = new Config(); 
    Hazelcast.newHazelcastInstance(cfg); 

    cfg.setProperty("hazelcast.initial.min.cluster.size","3"); 
    cfg.getGroupConfig().setName("DEV").setPassword("DEV-pass"); 
    NetworkConfig network = cfg.getNetworkConfig(); 
    JoinConfig join = network.getJoin(); 
    TcpIpConfig tcpipConfig=join.getTcpIpConfig(); 
    tcpipConfig.addMember("172.17.153.87").addMember("10.45.67.100") 
    .setRequiredMember("192.168.10.100").setEnabled(true); 
    network.getInterfaces().setEnabled(true).addInterface("10.45.67.*"); 
    System.out.println(tcpipConfig.isEnabled()); 
    System.out.println(tcpipConfig.getMembers()); 

    MapConfig mapCfg = new MapConfig(); 
    mapCfg.setName("testMap"); 
    mapCfg.setBackupCount(2); 
    mapCfg.getMaxSizeConfig().setSize(10000); 
    mapCfg.setTimeToLiveSeconds(300); 

    MapStoreConfig mapStoreCfg = new MapStoreConfig(); 
    mapStoreCfg.setClassName("com.hazelcast.examples.DummyStore").setEnabled(true); 
    mapCfg.setMapStoreConfig(mapStoreCfg); 

    NearCacheConfig nearCacheConfig = new NearCacheConfig(); 
    nearCacheConfig.setMaxSize(1000).setMaxIdleSeconds(120).setTimeToLiveSeconds(300); 
    mapCfg.setNearCacheConfig(nearCacheConfig); 

    cfg.addMapConfig(mapCfg); 

請看看代碼,讓我,如果任何事情進一步修改才能添加成員hazelcast集羣

回答

1

添加此行關閉組播支持TCP的,

join.getMulticastConfig().setEnabled(false);

移動此線到最後,

Hazelcast.newHazelcastInstance(cfg);

您應該在構建實例之前完成配置。

+0

由於它的工作 –

0

在Windows下,Hazelcast的開箱即用配置(空的Config)剛剛工作。
我只需要並行啓動我的Java程序多次並且Hazelcast節點互相發現。

在Linux下,它是更棘手:下面是一個工作示例 - 只要運行它的多個實例並行。第三個執行後

import java.util.*; 
import com.hazelcast.config.*; 
import com.hazelcast.core.*; 

public class NewClass { 

    public static void main(String[] args) { 
     Config config = new Config(); 
     config.getNetworkConfig().setPublicAddress("127.0.0.1") 
       .setPort(7771).setPortAutoIncrement(true); 
     JoinConfig join = config.getNetworkConfig().getJoin(); 
     join.getMulticastConfig().setEnabled(false); 
     join.getAwsConfig().setEnabled(false); 
     join.getTcpIpConfig().setEnabled(true).setMembers(
       Arrays.asList(
        "127.0.0.1:7771", 
        "127.0.0.1:7772", 
        "127.0.0.1:7773")); 

     HazelcastInstance h = Hazelcast.newHazelcastInstance(config); 
    } 
} 

輸出:

[snip] 

Members [3] { 
    Member [127.0.0.1]:7771 - 18f5aada-6f00-4077-814e-337517d5c1eb 
    Member [127.0.0.1]:7772 - e9e2e7fd-e2fe-4c56-80c5-6b499d07b2b9 
    Member [127.0.0.1]:7773 - 14fd377c-69fd-4c69-a9b8-086dd1cd7857 this 
} 

[snip]