com.netflix.discovery.DefaultEurekaClientConfig Java Examples

The following examples show how to use com.netflix.discovery.DefaultEurekaClientConfig. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EurekaHandler.java    From riposte with Apache License 2.0 6 votes vote down vote up
public void register() {
    if (isEurekaDisabled()) {
        logger.info("Eureka is disabled, skipping instance's eureka registration.");
        return;
    }

    if (!registered.compareAndSet(false, true)) {
        logger.info("Eureka handler already registered, skipping registration.");
        return;
    }

    EurekaInstanceConfig eurekaInstanceConfig = createEurekaInstanceConfig();

    initDiscoveryManager(eurekaInstanceConfig, new DefaultEurekaClientConfig(eurekaClientNamespace));
    setEurekaInstanceStatus(InstanceInfo.InstanceStatus.UP);
}
 
Example #2
Source File: EurekaHandlerTest.java    From riposte with Apache License 2.0 6 votes vote down vote up
@Test
public void register_uses_createEurekaInstanceConfig_then_calls_initDiscoveryManager_with_it_and_sets_instance_status_UP() {
    // given
    EurekaInstanceConfig instanceConfigMock = mock(EurekaInstanceConfig.class);
    doReturn(instanceConfigMock).when(handlerSpy).createEurekaInstanceConfig();
    assertThat(handlerSpy.registered.get()).isFalse();

    // when
    handlerSpy.register();

    // then
    assertThat(handlerSpy.registered.get()).isTrue();
    verify(handlerSpy).createEurekaInstanceConfig();

    ArgumentCaptor<EurekaClientConfig> clientConfigCaptor = ArgumentCaptor.forClass(EurekaClientConfig.class);
    verify(handlerSpy).initDiscoveryManager(eq(instanceConfigMock),
                                            clientConfigCaptor.capture());
    EurekaClientConfig clientConfigUsed = clientConfigCaptor.getValue();
    assertThat(clientConfigUsed).isInstanceOf(DefaultEurekaClientConfig.class);
    assertThat(Whitebox.getInternalState(clientConfigUsed, "namespace")).isEqualTo(handlerSpy.eurekaClientNamespace);

    verify(handlerSpy).setEurekaInstanceStatus(InstanceStatus.UP);
}
 
Example #3
Source File: EurekaDynamicServerListLoadBalancerTest.java    From ribbon with Apache License 2.0 6 votes vote down vote up
private EurekaClient setUpEurekaClientMock(List<InstanceInfo> servers) {
    final EurekaClient eurekaClientMock = PowerMock.createMock(EurekaClient.class);

    EasyMock.expect(eurekaClientMock.getEurekaClientConfig()).andReturn(new DefaultEurekaClientConfig()).anyTimes();

    EasyMock
            .expect(eurekaClientMock.getInstancesByVipAddress(EasyMock.anyString(), EasyMock.anyBoolean(), EasyMock.anyString()))
            .andReturn(servers.subList(0, initialServerListSize)).times(1)
            .andReturn(servers.subList(initialServerListSize, servers.size())).anyTimes();

    EasyMock
            .expectLastCall();

    EasyMock
            .expect(eurekaClientMock.unregisterEventListener(EasyMock.isA(EurekaEventListener.class)))
            .andReturn(true).anyTimes();

    return eurekaClientMock;
}
 
Example #4
Source File: MockedDiscoveryServerListTest.java    From ribbon with Apache License 2.0 6 votes vote down vote up
@Before
public void setupMock(){
    List<InstanceInfo> instances = getDummyInstanceInfo("dummy", getMockServerList());
    PowerMock.mockStatic(DiscoveryManager.class);
    PowerMock.mockStatic(DiscoveryClient.class);

    DiscoveryClient mockedDiscoveryClient = createMock(DiscoveryClient.class);
    DiscoveryManager mockedDiscoveryManager = createMock(DiscoveryManager.class);

    expect(mockedDiscoveryClient.getEurekaClientConfig()).andReturn(new DefaultEurekaClientConfig()).anyTimes();
    expect(DiscoveryManager.getInstance()).andReturn(mockedDiscoveryManager).anyTimes();
    expect(mockedDiscoveryManager.getDiscoveryClient()).andReturn(mockedDiscoveryClient).anyTimes();

    expect(mockedDiscoveryClient.getInstancesByVipAddress(getVipAddress(), false, null)).andReturn(instances).anyTimes();

    replay(DiscoveryManager.class);
    replay(DiscoveryClient.class);
    replay(mockedDiscoveryManager);
    replay(mockedDiscoveryClient);
}
 
Example #5
Source File: EurekaModule.java    From staash with Apache License 2.0 6 votes vote down vote up
@Override
protected void configure() {
    LOG.info("Configuring EurekaModule");
    
    // Initialize eureka
    // TODO: Move this to a bootstrap thingy
    DiscoveryManager.getInstance().initComponent(
            new CloudInstanceConfig(),
            new DefaultEurekaClientConfig());

    // Eureka - Astyanax integration
    MapBinder<String, HostSupplierProvider> hostSuppliers = MapBinder.newMapBinder(binder(), String.class, HostSupplierProvider.class);
    hostSuppliers.addBinding("eureka").to(EurekaAstyanaxHostSupplier.class);
    
    //bind(ClusterDiscoveryService.class).to(EurekaClusterDiscoveryService.class).asEagerSingleton();

}
 
Example #6
Source File: InitializeServletListener.java    From s2g-zuul with MIT License 5 votes vote down vote up
private void registerEureka() {
	DynamicBooleanProperty eurekaEnabled = DynamicPropertyFactory.getInstance().getBooleanProperty("eureka.enabled",
			true);
	if (!eurekaEnabled.get())
		return;

	EurekaInstanceConfig eurekaInstanceConfig = new PropertiesInstanceConfig() {
	};
       ConfigurationManager.getConfigInstance().setProperty("eureka.statusPageUrl","http://"+ getTurbineInstance());

	DiscoveryManager.getInstance().initComponent(eurekaInstanceConfig, new DefaultEurekaClientConfig());

	final DynamicStringProperty serverStatus = DynamicPropertyFactory.getInstance()
			.getStringProperty("server." + IPUtil.getLocalIP() + ".status", "up");
	DiscoveryManager.getInstance().getDiscoveryClient().registerHealthCheckCallback(new HealthCheckCallback() {
		@Override
		public boolean isHealthy() {
			return serverStatus.get().toLowerCase().equals("up");
		}
	});

	String version = String.valueOf(System.currentTimeMillis());
	String group = ConfigurationManager.getConfigInstance().getString("server.group", "default");
	String dataCenter = ConfigurationManager.getConfigInstance().getString("server.data-center", "default");

	Map<String, String> metadata = new HashMap<String, String>();
	metadata.put("version", version);
	metadata.put("group", group);
	metadata.put("dataCenter", dataCenter);

	String turbineInstance = getTurbineInstance();
	if (turbineInstance != null) {
		metadata.put("turbine.instance", turbineInstance);
	}

	ApplicationInfoManager.getInstance().registerAppMetadata(metadata);
}
 
Example #7
Source File: EurekaRegistry.java    From oneplatform with Apache License 2.0 5 votes vote down vote up
private void initEurekaClient(Properties properties) throws Exception{
	
	properties.setProperty("eureka.metadataMap.nodeId", NodeNameHolder.getNodeId());
	ConfigurationManager.loadProperties(properties);
	//ConfigurationManager.loadPropertiesFromResources("eureka.properties");
	
	//DynamicPropertyFactory configInstance = com.netflix.config.DynamicPropertyFactory.getInstance();
	
	MyDataCenterInstanceConfig instanceConfig = new MyDataCenterInstanceConfig(){
		@Override
		public String getHostName(boolean refresh) {
			String hostName = super.getHostName(refresh);
			if(ResourceUtils.getBoolean("eureka.preferIpAddress")){
				hostName = IpUtils.getLocalIpAddr();
			}
			return hostName;
		}

		@Override
		public String getIpAddress() {
			return IpUtils.getLocalIpAddr();
		}
		
		
	};
	InstanceInfo instanceInfo = new EurekaConfigBasedInstanceInfoProvider(instanceConfig).get();
       applicationInfoManager = new ApplicationInfoManager(instanceConfig, instanceInfo);
       
       DefaultEurekaClientConfig clientConfig = new DefaultEurekaClientConfig();

       eurekaClient = new DiscoveryClient(applicationInfoManager, clientConfig);
       
       instanceId = instanceInfo.getInstanceId();
}
 
Example #8
Source File: EurekaModule.java    From staash with Apache License 2.0 5 votes vote down vote up
@Override
protected void configure() {
    LOG.info("Configuring EurekaModule");
    
            DiscoveryManager.getInstance().initComponent(
            new CloudInstanceConfig(),
            new DefaultEurekaClientConfig());

    // Eureka - Astyanax integration
    MapBinder<String, HostSupplierProvider> hostSuppliers = MapBinder.newMapBinder(binder(), String.class, HostSupplierProvider.class);
    hostSuppliers.addBinding("eureka").to(EurekaAstyanaxHostSupplier.class).asEagerSingleton();
    
    //bind(ClusterDiscoveryService.class).to(EurekaClusterDiscoveryService.class).asEagerSingleton();

}
 
Example #9
Source File: EurekaComponents.java    From kork with Apache License 2.0 4 votes vote down vote up
@Bean
@DependsOn("environmentBackedConfig")
EurekaClientConfig eurekaClientConfig(
    EurekaConfigurationProperties eurekaConfigurationProperties) {
  return new DefaultEurekaClientConfig(eurekaConfigurationProperties.getClient().getNamespace());
}
 
Example #10
Source File: EurekaConfiguration.java    From chassis with Apache License 2.0 4 votes vote down vote up
/***
 * Initializes Eureka Client Library (aka DiscoveryManager)
 *
 * @return discovery manager bean
 */
@Bean(destroyMethod = "shutdownComponent")
public DiscoveryManager eurekaDiscoveryManager(MetadataCollector metadataCollector) {
    final DiscoveryManager bean = DiscoveryManager.getInstance();
    if (!disableEureka) {
        // set eureka.port via http.port if not already set
        int httpPort = ConfigurationManager.getConfigInstance().getInt("http.port",-1);
        int httpsPort = ConfigurationManager.getConfigInstance().getInt("https.port",-1);
        int eurekaPort = ConfigurationManager.getConfigInstance().getInt("eureka.port",-1);
        int eurekaSecurePort = ConfigurationManager.getConfigInstance().getInt("eureka.securePort",-1);
        boolean httpPortEnabled = ConfigurationManager.getConfigInstance().getBoolean("http.enabled", false);
        boolean httpsPortEnabled = ConfigurationManager.getConfigInstance().getBoolean("https.enabled", false);
        if (httpPort != -1 && eurekaPort == -1) {
            ConfigurationManager.getConfigInstance().setProperty("eureka.port", httpPort);
            ConfigurationManager.getConfigInstance().setProperty("eureka.port.enabled", httpPortEnabled);
        }
        if(httpsPort != -1 && eurekaSecurePort == -1){
            ConfigurationManager.getConfigInstance().setProperty("eureka.securePort", httpsPort);
            ConfigurationManager.getConfigInstance().setProperty("eureka.securePort.enabled", httpsPortEnabled);
        }

        // set eureka.name and eureka.vipAddress with @SpringApp name if not already set
        String appName = ConfigurationManager.getConfigInstance().getString("app.name",null);
        String eurekaName = ConfigurationManager.getConfigInstance().getString("eureka.name",null);
        String eurekaVip = ConfigurationManager.getConfigInstance().getString("eureka.vipAddress",null);
        String eurekaSecureVipAddress = ConfigurationManager.getConfigInstance().getString("eureka.secureVipAddress",null);
        if (appName != null && eurekaName == null) {
            ConfigurationManager.getConfigInstance().setProperty("eureka.name", appName);
        }
        if (appName != null && eurekaVip == null) {
            ConfigurationManager.getConfigInstance().setProperty("eureka.vipAddress", appName);
        }
        if (appName != null && eurekaSecureVipAddress == null) {
            ConfigurationManager.getConfigInstance().setProperty("eureka.secureVipAddress", appName);
        }

        // initialize DiscoveryManager if it hasn't already been done
        if (ApplicationInfoManager.getInstance().getInfo() == null) {
            EurekaInstanceConfig config;
            switch (datacenter.toLowerCase()) {
                case "amazon":
                case "cloud":
                    config = new KixeyeCloudInstanceConfig(metadataCollector);
                    break;
                default:
                    config = new KixeyeMyDataCenterInstanceConfig(metadataCollector);
                    break;
            }
            bean.initComponent(config, new DefaultEurekaClientConfig());
        }
    }
    return bean;
}
 
Example #11
Source File: LoadBalancerTestUtils.java    From ribbon with Apache License 2.0 4 votes vote down vote up
static DiscoveryClient mockDiscoveryClient()
{
    DiscoveryClient mockedDiscoveryClient = createMock(DiscoveryClient.class);
    expect(mockedDiscoveryClient.getEurekaClientConfig()).andReturn(new DefaultEurekaClientConfig()).anyTimes();
    return mockedDiscoveryClient;
}