private Address getRequiredMemberAddress() {
   TcpIpConfig tcpIpConfig = config.getNetworkConfig().getJoin().getTcpIpConfig();
   String host = tcpIpConfig.getRequiredMember();
   try {
     AddressHolder addressHolder =
         AddressUtil.getAddressHolder(host, config.getNetworkConfig().getPort());
     if (AddressUtil.isIpAddress(addressHolder.getAddress())) {
       return new Address(addressHolder.getAddress(), addressHolder.getPort());
     }
     InterfacesConfig interfaces = config.getNetworkConfig().getInterfaces();
     if (interfaces.isEnabled()) {
       InetAddress[] inetAddresses = InetAddress.getAllByName(addressHolder.getAddress());
       if (inetAddresses.length > 1) {
         for (InetAddress inetAddress : inetAddresses) {
           if (AddressUtil.matchAnyInterface(
               inetAddress.getHostAddress(), interfaces.getInterfaces())) {
             return new Address(inetAddress, addressHolder.getPort());
           }
         }
       } else if (AddressUtil.matchAnyInterface(
           inetAddresses[0].getHostAddress(), interfaces.getInterfaces())) {
         return new Address(addressHolder.getAddress(), addressHolder.getPort());
       }
     } else {
       return new Address(addressHolder.getAddress(), addressHolder.getPort());
     }
   } catch (final Exception e) {
     logger.warning(e);
   }
   return null;
 }
 @Test
 public void testNetworkConfig() {
   NetworkConfig networkConfig = config.getNetworkConfig();
   assertNotNull(networkConfig);
   assertEquals(5700, networkConfig.getPort());
   assertFalse(networkConfig.isPortAutoIncrement());
   final Collection<String> allowedPorts = networkConfig.getOutboundPortDefinitions();
   assertEquals(2, allowedPorts.size());
   Iterator portIter = allowedPorts.iterator();
   assertEquals("35000-35100", portIter.next());
   assertEquals("36000,36100", portIter.next());
   assertFalse(networkConfig.getJoin().getMulticastConfig().isEnabled());
   assertEquals(networkConfig.getJoin().getMulticastConfig().getMulticastTimeoutSeconds(), 8);
   assertEquals(networkConfig.getJoin().getMulticastConfig().getMulticastTimeToLive(), 16);
   assertFalse(networkConfig.getInterfaces().isEnabled());
   assertEquals(1, networkConfig.getInterfaces().getInterfaces().size());
   assertEquals("10.10.1.*", networkConfig.getInterfaces().getInterfaces().iterator().next());
   TcpIpConfig tcp = networkConfig.getJoin().getTcpIpConfig();
   assertNotNull(tcp);
   assertTrue(tcp.isEnabled());
   assertTrue(networkConfig.getSymmetricEncryptionConfig().isEnabled());
   final List<String> members = tcp.getMembers();
   assertEquals(members.toString(), 2, members.size());
   assertEquals("127.0.0.1:5700", members.get(0));
   assertEquals("127.0.0.1:5701", members.get(1));
   assertEquals("127.0.0.1:5700", tcp.getRequiredMember());
   AwsConfig aws = networkConfig.getJoin().getAwsConfig();
   assertFalse(aws.isEnabled());
   assertEquals("sample-access-key", aws.getAccessKey());
   assertEquals("sample-secret-key", aws.getSecretKey());
   assertEquals("sample-region", aws.getRegion());
   assertEquals("sample-group", aws.getSecurityGroupName());
   assertEquals("sample-tag-key", aws.getTagKey());
   assertEquals("sample-tag-value", aws.getTagValue());
 }
 public static Collection<String> getConfigurationMembers(Config config) {
   final TcpIpConfig tcpIpConfig = config.getNetworkConfig().getJoin().getTcpIpConfig();
   final Collection<String> configMembers = tcpIpConfig.getMembers();
   final Set<String> possibleMembers = new HashSet<String>();
   for (String member : configMembers) {
     // split members defined in tcp-ip configuration by comma(,) semi-colon(;) space( ).
     String[] members = member.split("[,; ]");
     Collections.addAll(possibleMembers, members);
   }
   return possibleMembers;
 }
 protected NetworkConfig getLocalhostTcpIpNetworkConfig(int port) {
   NetworkConfig networkConfig = new NetworkConfig();
   networkConfig.setPort(port);
   networkConfig.getJoin().getMulticastConfig().setEnabled(false);
   TcpIpConfig tcpIpConfig = networkConfig.getJoin().getTcpIpConfig();
   tcpIpConfig.setEnabled(true);
   tcpIpConfig.addMember("127.0.0.1");
   InterfacesConfig interfacesConfig = networkConfig.getInterfaces();
   interfacesConfig.setEnabled(true);
   interfacesConfig.setInterfaces(Collections.singleton("127.0.0.*"));
   return networkConfig;
 }
  public void doJoin(AtomicBoolean joined) {
    int tryCount = 0;
    long joinStartTime = Clock.currentTimeMillis();
    long maxJoinMillis = node.getGroupProperties().MAX_JOIN_SECONDS.getInteger() * 1000;

    while (node.isActive()
        && !joined.get()
        && (Clock.currentTimeMillis() - joinStartTime < maxJoinMillis)) {
      String msg = "Joining to master node: " + node.getMasterAddress();
      logger.log(Level.FINEST, msg);
      systemLogService.logJoin(msg);

      final Address masterAddressNow;
      if (targetAddress == null) {
        masterAddressNow = findMasterWithMulticast();
      } else {
        // if target address is set explicitly, try to join target address first.
        masterAddressNow = targetAddress;
        targetAddress = null;
      }

      node.setMasterAddress(masterAddressNow);
      if (masterAddressNow != null) {
        systemLogService.logJoin("Setting master address to " + masterAddressNow);
      }
      if (node.getMasterAddress() == null || node.address.equals(node.getMasterAddress())) {
        TcpIpConfig tcpIpConfig = config.getNetworkConfig().getJoin().getTcpIpConfig();
        if (tcpIpConfig != null && tcpIpConfig.isEnabled()) {
          doTCP(joined);
        } else {
          node.setAsMaster();
        }
        return;
      }
      if (++tryCount > 49) {
        failedJoiningToMaster(true, tryCount);
      }
      if (!node.getMasterAddress().equals(node.address)) {
        connectAndSendJoinRequest(node.getMasterAddress());
      } else {
        node.setMasterAddress(null);
        tryCount = 0;
      }
      try {
        //noinspection BusyWait
        Thread.sleep(500L);
      } catch (InterruptedException ignored) {
      }
    }
  }
  public Config getConfig() {

    final Config cfg = new Config();
    cfg.setInstanceName(instanceName);

    final Properties props = new Properties();
    props.put("hazelcast.rest.enabled", false);
    props.put("hazelcast.logging.type", "slf4j");
    props.put("hazelcast.connect.all.wait.seconds", 45);
    props.put("hazelcast.operation.call.timeout.millis", 30000);

    // group configuration
    cfg.setGroupConfig(new GroupConfig(Constants.HC_GROUP_NAME, Constants.HC_GROUP_PASSWORD));
    // network configuration initialization
    final NetworkConfig netCfg = new NetworkConfig();
    netCfg.setPortAutoIncrement(true);
    netCfg.setPort(Constants.HC_PORT);
    // multicast
    final MulticastConfig mcCfg = new MulticastConfig();
    mcCfg.setEnabled(false);
    // tcp
    final TcpIpConfig tcpCfg = new TcpIpConfig();
    tcpCfg.addMember("127.0.0.1");
    tcpCfg.setEnabled(true);
    // network join configuration
    final JoinConfig joinCfg = new JoinConfig();
    joinCfg.setMulticastConfig(mcCfg);
    joinCfg.setTcpIpConfig(tcpCfg);
    netCfg.setJoin(joinCfg);
    // ssl
    netCfg.setSSLConfig(new SSLConfig().setEnabled(false));

    // Adding mapstore
    final MapConfig mapCfg = cfg.getMapConfig(storeType);

    final MapStoreConfig mapStoreCfg = new MapStoreConfig();
    mapStoreCfg.setImplementation(store);
    mapStoreCfg.setWriteDelaySeconds(1);
    // to load all map at same time
    mapStoreCfg.setInitialLoadMode(MapStoreConfig.InitialLoadMode.EAGER);
    mapCfg.setMapStoreConfig(mapStoreCfg);
    cfg.addMapConfig(mapCfg);
    return cfg;
  }
  private void initNetworkConfig(
      NetworkConfig networkConfig, int basePort, int portSeed, boolean multicast, int nodeCount) {

    networkConfig.setPortAutoIncrement(false);
    networkConfig.setPort(basePort + portSeed);

    JoinConfig join = networkConfig.getJoin();

    MulticastConfig multicastConfig = join.getMulticastConfig();
    multicastConfig.setEnabled(multicast);
    multicastConfig.setMulticastTimeoutSeconds(5);

    TcpIpConfig tcpIpConfig = join.getTcpIpConfig();
    tcpIpConfig.setEnabled(!multicast);
    tcpIpConfig.setConnectionTimeoutSeconds(5);

    List<String> members = new ArrayList<String>(nodeCount);
    for (int i = 0; i < nodeCount; i++) {
      members.add("127.0.0.1:" + (basePort + i));
    }
    Collections.sort(members);
    tcpIpConfig.setMembers(members);
  }
  /**
   * 通过加载HZ的配置文件,动态创建HZ集群
   *
   * @param configPath hz的配置文件
   */
  public HazelcastStoreManager(HazelcastProperties hazelcastProperties) {
    //        Properties prop = new Properties();
    //        try {
    //            prop = loadConf(null);
    //        } catch (IOException e) {
    //            LOGGER.warn("load conf error,use default config");
    //        }
    Config cfg = new Config();
    cfg.getGroupConfig().setName(hazelcastProperties.getGroupUserName());
    cfg.getGroupConfig().setPassword(hazelcastProperties.getGroupPassword());

    cfg.setInstanceName(hazelcastProperties.getInstanceName());

    // cfg.getQueueConfig(EVENT_QUEUE).addItemListenerConfig(new
    // ItemListenerConfig(this.hazelcastQueueItemListener,true));
    if (hazelcastProperties.getMancenter().isEnable()
        && StringUtils.isNotBlank(hazelcastProperties.getMancenter().getUrl())) {
      cfg.getManagementCenterConfig().setEnabled(true);
      cfg.getManagementCenterConfig().setUrl(hazelcastProperties.getMancenter().getUrl());
    }
    System.setProperty("hazelcast.socket.bind.any", "false");
    String ip = "127.0.0.1";
    try {
      ip = InetAddress.getLocalHost().getHostAddress();
    } catch (UnknownHostException e) {
      LOGGER.warn("get ip error, {}", e.getMessage());
    }
    LOGGER.info("local memchine ip: {}", ip);
    cfg.getProperties(); // .getGroupProperties().SOCKET_SERVER_BIND_ANY
    cfg.getNetworkConfig().getInterfaces().addInterface(ip);
    cfg.getNetworkConfig().getInterfaces().setEnabled(true);

    JoinConfig join = cfg.getNetworkConfig().getJoin();
    TcpIpConfig tcpIpConfig = join.getTcpIpConfig();
    tcpIpConfig.addMember(hazelcastProperties.getMembers());
    if (!tcpIpConfig.getMembers().contains("127.0.0.1")) {
      tcpIpConfig.addMember("127.0.0.1");
    }
    LOGGER.info("hazelcast members in prop:{}", tcpIpConfig.getMembers());
    tcpIpConfig.setEnabled(true);
    this.hazelcast = Hazelcast.newHazelcastInstance(cfg);
    this.cacheManager = new HazelcastCacheManager(this.hazelcast);
  }