org.springframework.cloud.netflix.eureka.CloudEurekaClient Java Examples

The following examples show how to use org.springframework.cloud.netflix.eureka.CloudEurekaClient. 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: EurekaRegistration.java    From spring-cloud-netflix with Apache License 2.0 6 votes vote down vote up
public EurekaRegistration build() {
	Assert.notNull(instanceConfig, "instanceConfig may not be null");

	if (this.applicationInfoManager == null) {
		InstanceInfo instanceInfo = new InstanceInfoFactory()
				.create(this.instanceConfig);
		this.applicationInfoManager = new ApplicationInfoManager(
				this.instanceConfig, instanceInfo);
	}
	if (this.eurekaClient == null) {
		Assert.notNull(this.clientConfig,
				"if eurekaClient is null, EurekaClientConfig may not be null");
		Assert.notNull(this.publisher,
				"if eurekaClient is null, ApplicationEventPublisher may not be null");

		this.eurekaClient = new CloudEurekaClient(this.applicationInfoManager,
				this.clientConfig, this.publisher);
	}
	return new EurekaRegistration(instanceConfig, eurekaClient,
			applicationInfoManager, healthCheckHandler);
}
 
Example #2
Source File: EurekaServiceRegistryTests.java    From spring-cloud-netflix with Apache License 2.0 6 votes vote down vote up
@Test
public void eurekaClientNotShutdownInDeregister() {
	EurekaServiceRegistry registry = new EurekaServiceRegistry();

	CloudEurekaClient eurekaClient = mock(CloudEurekaClient.class);
	ApplicationInfoManager applicationInfoManager = mock(
			ApplicationInfoManager.class);

	when(applicationInfoManager.getInfo()).thenReturn(mock(InstanceInfo.class));

	EurekaRegistration registration = EurekaRegistration
			.builder(new EurekaInstanceConfigBean(
					new InetUtils(new InetUtilsProperties())))
			.with(eurekaClient).with(applicationInfoManager)
			.with(new EurekaClientConfigBean(), mock(ApplicationEventPublisher.class))
			.build();

	registry.deregister(registration);

	verifyNoInteractions(eurekaClient);
}
 
Example #3
Source File: MossInstanceDiscoveryListener.java    From Moss with Apache License 2.0 5 votes vote down vote up
@EventListener
public void onParentHeartbeat(ParentHeartbeatEvent event) {
    HeartbeatMonitor heartbeatMonitor= getHeartbeatMonitorByClient((CloudEurekaClient) event.getSource());
    threadLocalmonitor.set(heartbeatMonitor);
    discoverIfNeeded((CloudEurekaClient) event.getSource(),event.getValue());
    threadLocalmonitor.remove();
}
 
Example #4
Source File: MossInstanceDiscoveryListener.java    From Moss with Apache License 2.0 5 votes vote down vote up
@EventListener
public void onApplicationEvent(HeartbeatEvent event) {
    HeartbeatMonitor heartbeatMonitor= getHeartbeatMonitorByClient((CloudEurekaClient) event.getSource());
    threadLocalmonitor.set(heartbeatMonitor);
    discoverIfNeeded((CloudEurekaClient) event.getSource(),event.getValue());
    threadLocalmonitor.remove();
}
 
Example #5
Source File: MossInstanceDiscoveryListener.java    From Moss with Apache License 2.0 5 votes vote down vote up
private void discoverIfNeeded(CloudEurekaClient source,Object value) {
    if (threadLocalmonitor.get().update(value)) {
        cloudEurekaClient.set(source);
         discover();
        cloudEurekaClient.remove();
    }
}
 
Example #6
Source File: EurekaClientWrapper.java    From summerframework with Apache License 2.0 5 votes vote down vote up
public EurekaClientWrapper(CloudEurekaClient cloudEurekaClient, ConfigurableEnvironment environment,
    ApplicationEventPublisher publisher) {
    super(cloudEurekaClient.getApplicationInfoManager(), cloudEurekaClient.getEurekaClientConfig(), publisher);
    this.environment = environment;
    this.eurekaRuleCache = EurekaRuleCache.getInstance();
    this.initReferenceCache();
    this.registerEventListener(new EurekaClientEventListener(publisher));
}
 
Example #7
Source File: EurekaApplicationContextInitializer.java    From summerframework with Apache License 2.0 5 votes vote down vote up
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
    ConfigurableEnvironment environment = applicationContext.getEnvironment();
    applicationContext.getBeanFactory().addBeanPostProcessor(new InstantiationAwareBeanPostProcessorAdapter() {

        @Override
        public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
            if (bean instanceof CloudEurekaClient) {
                CloudEurekaClient eurekaClient = (CloudEurekaClient)bean;
                try {
                    Field filedPublisher = ReflectionUtils.findField(CloudEurekaClient.class, "publisher",
                        ApplicationEventPublisher.class);
                    ReflectionUtils.makeAccessible(filedPublisher);
                    ApplicationEventPublisher publisher =
                        (ApplicationEventPublisher)ReflectionUtils.getField(filedPublisher, eurekaClient);
                    return new EurekaClientWrapper(eurekaClient, environment, publisher);
                } finally {
                    Method method = ReflectionUtils.findMethod(CloudEurekaClient.class, "cancelScheduledTasks");
                    ReflectionUtils.makeAccessible(method);
                    ReflectionUtils.invokeMethod(method, eurekaClient);
                    eurekaClient = null;
                }
            } else if (bean instanceof EurekaServiceRegistry) {
                EurekaServiceRegistry eurekaServiceRegistry = (EurekaServiceRegistry)bean;
                return new EurekaServiceRegistryWrapper(eurekaServiceRegistry, environment);
            } else if (bean instanceof EurekaInstanceConfigBean) {
                EurekaInstanceConfigBean instanceConfig = (EurekaInstanceConfigBean)bean;
                instanceConfig.setPreferIpAddress(true);
                return bean;
            } else {
                return bean;
            }
        }
    });
}
 
Example #8
Source File: DubboServiceDiscoveryAutoConfiguration.java    From spring-cloud-alibaba with Apache License 2.0 5 votes vote down vote up
/**
 * Compare {@link Applications#getAppsHashCode() apps hash code} between last
 * {@link Applications} and current.
 *
 * @see Applications#getAppsHashCode()
 * @return HeartbeatEvent Predicate
 */
@Bean
public Predicate<HeartbeatEvent> heartbeatEventChangedPredicate() {
	return event -> {
		String oldAppsHashCode = appsHashCodeCache.get();
		CloudEurekaClient cloudEurekaClient = (CloudEurekaClient) event
				.getSource();
		Applications applications = cloudEurekaClient.getApplications();
		String appsHashCode = applications.getAppsHashCode();
		return appsHashCodeCache.compareAndSet(oldAppsHashCode, appsHashCode)
				&& !Objects.equals(oldAppsHashCode, appsHashCode);
	};
}
 
Example #9
Source File: EurekaRegistration.java    From spring-cloud-netflix with Apache License 2.0 5 votes vote down vote up
public CloudEurekaClient getEurekaClient() {
	if (this.cloudEurekaClient.get() == null) {
		try {
			this.cloudEurekaClient.compareAndSet(null,
					getTargetObject(eurekaClient, CloudEurekaClient.class));
		}
		catch (Exception e) {
			log.error("error getting CloudEurekaClient", e);
		}
	}
	return this.cloudEurekaClient.get();
}
 
Example #10
Source File: EurekaServiceRegistryTests.java    From spring-cloud-netflix with Apache License 2.0 5 votes vote down vote up
@Test
public void eurekaClientGetStatus() {
	EurekaServiceRegistry registry = new EurekaServiceRegistry();

	EurekaInstanceConfigBean config = new EurekaInstanceConfigBean(
			new InetUtils(new InetUtilsProperties()));
	config.setAppname("myapp");
	config.setInstanceId("1234");

	InstanceInfo local = InstanceInfo.Builder.newBuilder().setAppName("myapp")
			.setInstanceId("1234").setStatus(DOWN).build();

	InstanceInfo remote = InstanceInfo.Builder.newBuilder().setAppName("myapp")
			.setInstanceId("1234").setStatus(DOWN).setOverriddenStatus(OUT_OF_SERVICE)
			.build();

	CloudEurekaClient eurekaClient = mock(CloudEurekaClient.class);
	when(eurekaClient.getInstanceInfo(local.getAppName(), local.getId()))
			.thenReturn(remote);

	ApplicationInfoManager applicationInfoManager = mock(
			ApplicationInfoManager.class);
	when(applicationInfoManager.getInfo()).thenReturn(local);

	EurekaRegistration registration = EurekaRegistration.builder(config)
			.with(eurekaClient).with(applicationInfoManager)
			.with(new EurekaClientConfigBean(), mock(ApplicationEventPublisher.class))
			.build();

	Object status = registry.getStatus(registration);

	assertThat(registration.getInstanceId()).isEqualTo("1234");

	assertThat(status).isInstanceOf(Map.class);

	Map<Object, Object> map = (Map<Object, Object>) status;

	assertThat(map).hasSize(2).containsEntry("status", DOWN.toString())
			.containsEntry("overriddenStatus", OUT_OF_SERVICE.toString());
}
 
Example #11
Source File: EurekaServiceRegistryTests.java    From spring-cloud-netflix with Apache License 2.0 5 votes vote down vote up
@Test
public void eurekaClientGetStatusNoInstance() {
	EurekaServiceRegistry registry = new EurekaServiceRegistry();

	EurekaInstanceConfigBean config = new EurekaInstanceConfigBean(
			new InetUtils(new InetUtilsProperties()));
	config.setAppname("myapp");
	config.setInstanceId("1234");

	CloudEurekaClient eurekaClient = mock(CloudEurekaClient.class);

	when(eurekaClient.getInstanceInfo("myapp", "1234")).thenReturn(null);

	ApplicationInfoManager applicationInfoManager = mock(
			ApplicationInfoManager.class);
	when(applicationInfoManager.getInfo()).thenReturn(mock(InstanceInfo.class));

	EurekaRegistration registration = EurekaRegistration.builder(config)
			.with(eurekaClient).with(applicationInfoManager)
			.with(new EurekaClientConfigBean(), mock(ApplicationEventPublisher.class))
			.build();

	Object status = registry.getStatus(registration);

	assertThat(registration.getInstanceId()).isEqualTo("1234");

	assertThat(status).isInstanceOf(Map.class);

	Map<Object, Object> map = (Map<Object, Object>) status;

	assertThat(map).hasSize(1).containsEntry("status", UNKNOWN.toString());
}
 
Example #12
Source File: MossInstanceDiscoveryListener.java    From Moss with Apache License 2.0 4 votes vote down vote up
private String getCodeByClient(CloudEurekaClient client) {
    return multRegisterCenter.getMultEurekaCodeMap().get(client);
}
 
Example #13
Source File: MossInstanceDiscoveryListener.java    From Moss with Apache License 2.0 4 votes vote down vote up
private HeartbeatMonitor getHeartbeatMonitorByClient(CloudEurekaClient client) {
    return multRegisterCenter.getMultHeartbeatMonitorMap().get(client);
}
 
Example #14
Source File: RefreshEurekaSampleApplication.java    From spring-cloud-netflix with Apache License 2.0 4 votes vote down vote up
@Bean
public EurekaClient getClient() {
	return mock(CloudEurekaClient.class);
}
 
Example #15
Source File: EurekaConfigServerBootstrapConfigurationTests.java    From spring-cloud-netflix with Apache License 2.0 4 votes vote down vote up
@Bean
public EurekaClient getClient() {
	return mock(CloudEurekaClient.class);
}