hazelcast编程配置不允许在集群中添加成员

ee7vknir  于 2021-07-09  发布在  Java
关注(0)|答案(2)|浏览(373)

以下是文档中给出的hazelcast编程配置,但它无法在hazelcast集群中添加成员。

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集群,请告诉我

j13ufse2

j13ufse21#

添加此行以关闭多播以支持tcp, join.getMulticastConfig().setEnabled(false); 把这条线移到尽头, Hazelcast.newHazelcastInstance(cfg); 您应该在构建示例之前完成配置。

eit6fx6z

eit6fx6z2#

在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]

相关问题