@Test public void testIntegration() { String zookeeperURL = zkTestServer.getConnectString(); Config config = new Config(); config.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false); config.setProperty("hazelcast.discovery.enabled", "true"); DiscoveryStrategyConfig discoveryStrategyConfig = new DiscoveryStrategyConfig(new ZookeeperDiscoveryStrategyFactory()); discoveryStrategyConfig.addProperty(ZookeeperDiscoveryProperties.ZOOKEEPER_URL.key(), zookeeperURL); config.getNetworkConfig().getJoin().getDiscoveryConfig().addDiscoveryStrategyConfig(discoveryStrategyConfig); HazelcastInstance instance1 = Hazelcast.newHazelcastInstance(config); HazelcastInstance instance2 = Hazelcast.newHazelcastInstance(config); int instance1Size = instance1.getCluster().getMembers().size(); assertEquals(2, instance1Size); int instance2Size = instance2.getCluster().getMembers().size(); assertEquals(2, instance2Size); }
@Test public void testIntegration_urlNotConfigured() { exception.expect(IllegalStateException.class); exception.expectMessage("Zookeeper URL cannot be null."); Config config = new Config(); config.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false); config.setProperty("hazelcast.discovery.enabled", "true"); DiscoveryStrategyConfig discoveryStrategyConfig = new DiscoveryStrategyConfig(new ZookeeperDiscoveryStrategyFactory()); config.getNetworkConfig().getJoin().getDiscoveryConfig().addDiscoveryStrategyConfig(discoveryStrategyConfig); HazelcastInstance instance1 = Hazelcast.newHazelcastInstance(config); HazelcastInstance instance2 = Hazelcast.newHazelcastInstance(config); int instance1Size = instance1.getCluster().getMembers().size(); assertEquals(2, instance1Size); int instance2Size = instance2.getCluster().getMembers().size(); assertEquals(2, instance2Size); }
@Bean public Config hazelcastConfig () { val config = new Config(); config.setProperty("hazelcast.discovery.enabled", "true"); val networkingConfig = config.getNetworkConfig(); networkingConfig.setPort(hazelcastPort); networkingConfig.setPortAutoIncrement(false); val joinConfig = networkingConfig.getJoin(); joinConfig.getMulticastConfig().setEnabled(false); joinConfig.getTcpIpConfig().setEnabled(false); joinConfig.getAwsConfig().setEnabled(false); joinConfig.getDiscoveryConfig() .addDiscoveryStrategyConfig(new DiscoveryStrategyConfig(discoveryStrategyFactory())); return config; }
@Test public void testExternalRegistration(){ EurekaHttpResponse<Applications> response = generateMockResponse(Collections.<InstanceInfo>emptyList()); when(requestHandler.getApplications()).thenReturn(response); Config config = new XmlConfigBuilder().build(); DiscoveryConfig discoveryConfig = config.getNetworkConfig().getJoin().getDiscoveryConfig(); DiscoveryStrategyConfig strategyConfig = discoveryConfig.getDiscoveryStrategyConfigs().iterator().next(); strategyConfig.addProperty("self-registration", "false"); HazelcastInstance hz1 = factory.newHazelcastInstance(config); HazelcastInstance hz2 = factory.newHazelcastInstance(config); assertClusterSizeEventually(2, hz1); assertClusterSizeEventually(2, hz2); verify(requestHandler, after(5000).never()).register(any(InstanceInfo.class)); }
@Test public void testNamespaceRegistration(){ final String appName = "other"; final String namespace = "hz"; configure(namespace, appName); EurekaHttpResponse<Applications> response = generateMockResponse(Collections.<InstanceInfo>emptyList(), appName); when(requestHandler.getApplications()).thenReturn(response); Config config = new XmlConfigBuilder().build(); DiscoveryConfig discoveryConfig = config.getNetworkConfig().getJoin().getDiscoveryConfig(); DiscoveryStrategyConfig strategyConfig = discoveryConfig.getDiscoveryStrategyConfigs().iterator().next(); strategyConfig.addProperty("namespace", namespace); HazelcastInstance hz1 = factory.newHazelcastInstance(config); assertClusterSizeEventually(1, hz1); ArgumentCaptor<InstanceInfo> captor = ArgumentCaptor.forClass(InstanceInfo.class); verify(requestHandler, timeout(5000).atLeastOnce()).register(captor.capture()); String actual = captor.getValue().getAppName().toLowerCase(); assertThat(actual, is(appName)); }
@Test public void discoveryStrategyFactoryTest() { JCloudsDiscoveryStrategyFactory jCloudsDiscoveryStrategyFactory = new JCloudsDiscoveryStrategyFactory(); String xmlFileName = "test-jclouds-config.xml"; InputStream xmlResource = JCloudsDiscoveryFactoryTest.class.getClassLoader().getResourceAsStream(xmlFileName); Config config = new XmlConfigBuilder(xmlResource).build(); JoinConfig joinConfig = config.getNetworkConfig().getJoin(); DiscoveryConfig discoveryConfig = joinConfig.getDiscoveryConfig(); DiscoveryStrategyConfig providerConfig = discoveryConfig.getDiscoveryStrategyConfigs().iterator().next(); assertEquals(jCloudsDiscoveryStrategyFactory.getDiscoveryStrategyType(), JCloudsDiscoveryStrategy.class); assertEquals(JCloudsDiscoveryStrategy.class.getName(), providerConfig.getClassName()); assertEquals(jCloudsDiscoveryStrategyFactory.getConfigurationProperties().size(), providerConfig.getProperties().size()); assertTrue(jCloudsDiscoveryStrategyFactory. newDiscoveryStrategy(null, null, new HashMap<String, Comparable>()) instanceof DiscoveryStrategy); }
@Test public void testIntegration() { String zookeeperURL = zkTestServer.getConnectString(); Config config = new Config(); config.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false); config.setProperty(GroupProperty.DISCOVERY_SPI_ENABLED, "true"); DiscoveryStrategyConfig dicoveryStrategyConfig = new DiscoveryStrategyConfig(new ZookeeperDiscoveryStrategyFactory()); dicoveryStrategyConfig.addProperty(ZookeeperDiscoveryProperties.ZOOKEEPER_URL.key(), zookeeperURL); config.getNetworkConfig().getJoin().getDiscoveryConfig().addDiscoveryProviderConfig(dicoveryStrategyConfig); HazelcastInstance instance1 = Hazelcast.newHazelcastInstance(config); HazelcastInstance instance2 = Hazelcast.newHazelcastInstance(config); int instance1Size = instance1.getCluster().getMembers().size(); assertEquals(2, instance1Size); int instance2Size = instance2.getCluster().getMembers().size(); assertEquals(2, instance2Size); }
private void addZkConfig(Config config) { config.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false); config.setProperty(GroupProperty.DISCOVERY_SPI_ENABLED.getName(), Boolean.TRUE.toString()); DiscoveryStrategyConfig discoveryStrategyConfig = new DiscoveryStrategyConfig(new ZookeeperDiscoveryStrategyFactory()); discoveryStrategyConfig.addProperty(ZookeeperDiscoveryProperties.ZOOKEEPER_URL.key(), zkUrl); discoveryStrategyConfig.addProperty(ZookeeperDiscoveryProperties.ZOOKEEPER_PATH.key(), zkDir); discoveryStrategyConfig.addProperty(ZookeeperDiscoveryProperties.GROUP.key(), HAZELCAST_CLUSTER_NAME); config.getNetworkConfig().getJoin().getDiscoveryConfig().addDiscoveryStrategyConfig(discoveryStrategyConfig); }
private ClientConfig createClientConfig(String zookeeperURL) { DiscoveryStrategyConfig discoveryStrategyConfig = createDiscoveryConfig(zookeeperURL); ClientConfig clientConfig = new ClientConfig(); clientConfig.setProperty("hazelcast.discovery.enabled", "true"); clientConfig.getNetworkConfig().getDiscoveryConfig().addDiscoveryStrategyConfig(discoveryStrategyConfig); return clientConfig; }
private Config createMemberConfig(String zookeeperURL) { Config config = new Config(); config.getNetworkConfig().setPort(HAZELCAST_BASE_PORT); config.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false); config.setProperty("hazelcast.discovery.enabled", "true"); DiscoveryStrategyConfig discoveryStrategyConfig = createDiscoveryConfig(zookeeperURL); config.getNetworkConfig().getJoin().getDiscoveryConfig().addDiscoveryStrategyConfig(discoveryStrategyConfig); return config; }
@Test public void parseDiscoveryStrategyConfigPasses() { final AwsDiscoveryStrategyFactory factory = new AwsDiscoveryStrategyFactory(); final Config config = createConfig("test-aws-config.xml"); final JoinConfig joinConfig = config.getNetworkConfig().getJoin(); assertFalse(joinConfig.getAwsConfig().isEnabled()); assertFalse(joinConfig.getTcpIpConfig().isEnabled()); assertFalse(joinConfig.getMulticastConfig().isEnabled()); final DiscoveryConfig discoveryConfig = joinConfig.getDiscoveryConfig(); assertTrue(discoveryConfig.isEnabled()); assertEquals(1, discoveryConfig.getDiscoveryStrategyConfigs().size()); final DiscoveryStrategyConfig providerConfig = discoveryConfig.getDiscoveryStrategyConfigs().iterator().next(); final Map<String, Comparable> providerProperties = providerConfig.getProperties(); final Collection<PropertyDefinition> factoryConfigProperties = factory.getConfigurationProperties(); assertEquals(factory.getDiscoveryStrategyType(), AwsDiscoveryStrategy.class); assertEquals(AwsDiscoveryStrategy.class.getName(), providerConfig.getClassName()); assertEquals(factoryConfigProperties.size(), providerProperties.size()); for (AwsProperties prop : AwsProperties.values()) { assertTrue(factoryConfigProperties.contains(prop.getDefinition())); } assertEquals("test-access-key", providerProperties.get("access-key")); assertEquals("test-secret-key", providerProperties.get("secret-key")); assertEquals("test-region", providerProperties.get("region")); assertEquals("test-iam-role", providerProperties.get("iam-role")); assertEquals("ec2.test-host-header", providerProperties.get("host-header")); assertEquals("test-security-group-name", providerProperties.get("security-group-name")); assertEquals("test-tag-key", providerProperties.get("tag-key")); assertEquals("test-tag-value", providerProperties.get("tag-value")); assertEquals("10", providerProperties.get("connection-timeout-seconds")); assertEquals("5702", providerProperties.get("hz-port")); }
@Test public void testParsing() { String xmlFileName = "test-jclouds-config.xml"; InputStream xmlResource = JCloudsDiscoveryFactoryTest.class.getClassLoader().getResourceAsStream(xmlFileName); Config config = new XmlConfigBuilder(xmlResource).build(); JoinConfig joinConfig = config.getNetworkConfig().getJoin(); AwsConfig awsConfig = joinConfig.getAwsConfig(); assertFalse(awsConfig.isEnabled()); TcpIpConfig tcpIpConfig = joinConfig.getTcpIpConfig(); assertFalse(tcpIpConfig.isEnabled()); MulticastConfig multicastConfig = joinConfig.getMulticastConfig(); assertFalse(multicastConfig.isEnabled()); DiscoveryConfig discoveryConfig = joinConfig.getDiscoveryConfig(); assertTrue(discoveryConfig.isEnabled()); assertEquals(1, discoveryConfig.getDiscoveryStrategyConfigs().size()); DiscoveryStrategyConfig providerConfig = discoveryConfig.getDiscoveryStrategyConfigs().iterator().next(); assertEquals(12, providerConfig.getProperties().size()); assertEquals("aws-ec2", providerConfig.getProperties().get("provider")); assertEquals("test", providerConfig.getProperties().get("identity")); assertEquals("test", providerConfig.getProperties().get("credential")); assertEquals("zone1,zone2", providerConfig.getProperties().get("zones")); assertEquals("region1,region2", providerConfig.getProperties().get("regions")); assertEquals("zone1,zone2", providerConfig.getProperties().get("zones")); assertEquals("tag1,tag2", providerConfig.getProperties().get("tag-keys")); assertEquals("tagvalue1,tagvalue2", providerConfig.getProperties().get("tag-values")); assertEquals("group", providerConfig.getProperties().get("group")); assertEquals("5702", providerConfig.getProperties().get("hz-port")); assertEquals("myfile.json", providerConfig.getProperties().get("credentialPath")); assertEquals("myRole", providerConfig.getProperties().get("role-name")); assertEquals("http://foo/bar", providerConfig.getProperties().get("endpoint")); }
private DiscoveryStrategyConfig createDiscoveryConfig(String zookeeperURL) { DiscoveryStrategyConfig discoveryStrategyConfig = new DiscoveryStrategyConfig(new ZookeeperDiscoveryStrategyFactory()); discoveryStrategyConfig.addProperty(ZookeeperDiscoveryProperties.ZOOKEEPER_URL.key(), zookeeperURL); return discoveryStrategyConfig; }
public HazelcastInstance newInstance() { String name = NAME.get(); String password = PASS.get(); if (StringUtils.isBlank(name)) { name = hazelcastDao.getGroupName(); } if (StringUtils.isBlank(password)) { password = hazelcastDao.getGroupPassword(); } Config config = new Config(); if (JMX.get()) { config.setProperty("hazelcast.jmx", "true"); } config.setProperty("hazelcast.logging.type", LOGGING.get()); config.setProperty("hazelcast.discovery.enabled", "true"); GroupConfig groupConfig = new GroupConfig(); groupConfig.setName(name); groupConfig.setPassword(password); config.setGroupConfig(groupConfig); DiscoveryStrategyConfig dsc = new DiscoveryStrategyConfig(dbDiscoveryFactory); DiscoveryConfig dc = new DiscoveryConfig(); dc.setDiscoveryStrategyConfigs(Arrays.asList(dsc)); JoinConfig joinConfig = new JoinConfig(); joinConfig.setDiscoveryConfig(dc); joinConfig.getTcpIpConfig().setEnabled(false); joinConfig.getMulticastConfig().setEnabled(false); joinConfig.getAwsConfig().setEnabled(false); NetworkConfig nc = new NetworkConfig(); nc.setPort(DBDiscovery.DEFAULT_PORT.get()); nc.setJoin(joinConfig); nc.setPublicAddress(dbDiscovery.getLocalConfig().getAdvertiseAddress()); config.setNetworkConfig(nc); HazelcastInstance hi = Hazelcast.newHazelcastInstance(config); while (!hi.getPartitionService().isClusterSafe()) { log.info("Waiting for cluster to be in a steady state"); try { Thread.sleep(2000L); } catch (InterruptedException e) { throw new IllegalStateException("Waiting for cluster to be in a steady state", e); } } return hi; }
@Test public void test() { Config config = new XmlConfigBuilder().build(); config.setProperty("hazelcast.discovery.enabled", "true"); JoinConfig joinConfig = config.getNetworkConfig().getJoin(); joinConfig.getMulticastConfig().setEnabled(false); joinConfig.getTcpIpConfig().setEnabled(false); DiscoveryConfig discoveryConfig = joinConfig.getDiscoveryConfig(); DiscoveryStrategyFactory discoveryStrategyFactory = new SomeRestServiceDiscoveryStrategyFactory(); DiscoveryStrategyConfig discoveryStrategyConfig = new DiscoveryStrategyConfig(discoveryStrategyFactory); discoveryConfig.addDiscoveryStrategyConfig(discoveryStrategyConfig); config.getNetworkConfig().setPort(55667); HazelcastInstance hazelcastInstance1 = Hazelcast.newHazelcastInstance(config); config.getNetworkConfig().setPort(1122); HazelcastInstance hazelcastInstance2 = Hazelcast.newHazelcastInstance(config); assertEquals(2, hazelcastInstance1.getCluster().getMembers().size()); assertEquals(2, hazelcastInstance2.getCluster().getMembers().size()); Hazelcast.shutdownAll(); }