Ejemplo n.º 1
0
  /**
   * 通过加载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);
  }
Ejemplo n.º 2
0
 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 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;
  }