@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());
 }
  /**
   * 通过加载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);
  }
示例#3
0
 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;
 }