Java Code Examples for org.springframework.cloud.netflix.eureka.EurekaInstanceConfigBean

The following examples show how to use org.springframework.cloud.netflix.eureka.EurekaInstanceConfigBean. These examples are extracted from open source projects. 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
private EurekaInstanceConfigBean registration(
		Map.Entry<StubConfiguration, Integer> entry) {
	EurekaInstanceConfigBean config = new EurekaInstanceConfigBean(this.inetUtils);
	String appName = name(entry.getKey());
	config.setInstanceEnabledOnit(true);
	InetAddress address = this.inetUtils.findFirstNonLoopbackAddress();
	config.setIpAddress(address.getHostAddress());
	config.setHostname(StringUtils.hasText(hostName(entry)) ? hostName(entry)
			: address.getHostName());
	config.setAppname(appName);
	config.setVirtualHostName(appName);
	config.setSecureVirtualHostName(appName);
	int port = port(entry);
	config.setNonSecurePort(port);
	config.setInstanceId(address.getHostAddress() + ":"
			+ entry.getKey().getArtifactId() + ":" + port);
	config.setLeaseRenewalIntervalInSeconds(1);
	return config;
}
 
Example 2
@Bean
public EurekaInstanceConfigBean eurekaInstanceConfigBean() {
	if (!StringUtils.isEmpty(registrationMethod)) {
		LOGGER.info("Eureka registration method: " + registrationMethod);

		if (ROUTE_REGISTRATION_METHOD.equals(registrationMethod)) {
			return getRouteRegistration();
		}

		if (DIRECT_REGISTRATION_METHOD.equals(registrationMethod)) {
			return getDirectRegistration();
		}
	}

	return getDefaultRegistration();
}
 
Example 3
@Bean
public EurekaInstanceConfigBean eurekaInstanceConfigBean() {
	if (!StringUtils.isEmpty(registrationMethod)) {
		LOGGER.info("Eureka registration method: " + registrationMethod);

		if (ROUTE_REGISTRATION_METHOD.equals(registrationMethod)) {
			return getRouteRegistration();
		}

		if (DIRECT_REGISTRATION_METHOD.equals(registrationMethod)) {
			return getDirectRegistration();
		}
	}

	return getDefaultRegistration();
}
 
Example 4
@Test
public void testDefaultRegistration() {
	contextRunner.run(context -> {
		assertThat(context).hasSingleBean(EurekaInstanceConfigBean.class);
		EurekaInstanceConfigBean eurekaInstanceConfigBean = context.getBean(EurekaInstanceConfigBean.class);
		assertEquals(HOSTNAME + ":" + INSTANCE_ID, eurekaInstanceConfigBean.getInstanceId());
		assertEquals(HOSTNAME, eurekaInstanceConfigBean.getHostname());
		assertEquals(80, eurekaInstanceConfigBean.getNonSecurePort());
		assertEquals(443, eurekaInstanceConfigBean.getSecurePort());
		assertTrue(eurekaInstanceConfigBean.getSecurePortEnabled());

		Map<String, String> metadata = eurekaInstanceConfigBean.getMetadataMap();
		assertEquals(INSTANCE_GUID, metadata.get("cfAppGuid"));
		assertEquals(INSTANCE_INDEX, metadata.get("cfInstanceIndex"));
		assertEquals(INSTANCE_ID, metadata.get("instanceId"));
		assertEquals(ZONE, metadata.get("zone"));
	});
}
 
Example 5
@Override
public ManagementMetadata get(EurekaInstanceConfigBean instance, int serverPort,
		String serverContextPath, String managementContextPath,
		Integer managementPort) {
	if (isRandom(managementPort)) {
		return null;
	}
	if (managementPort == null && isRandom(serverPort)) {
		return null;
	}
	String healthCheckUrl = getHealthCheckUrl(instance, serverPort, serverContextPath,
			managementContextPath, managementPort, false);
	String statusPageUrl = getStatusPageUrl(instance, serverPort, serverContextPath,
			managementContextPath, managementPort);

	ManagementMetadata metadata = new ManagementMetadata(healthCheckUrl,
			statusPageUrl, managementPort == null ? serverPort : managementPort);
	if (instance.isSecurePortEnabled()) {
		metadata.setSecureHealthCheckUrl(getHealthCheckUrl(instance, serverPort,
				serverContextPath, managementContextPath, managementPort, true));
	}
	return metadata;
}
 
Example 6
@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 7
@Before
public void setup() {
	eurekaHttpClient = new WebClientTransportClientFactory()
			.newClient(new DefaultEndpoint(serviceUrl));

	EurekaInstanceConfigBean config = new EurekaInstanceConfigBean(inetUtils);

	String appname = "customapp";
	config.setIpAddress("127.0.0.1");
	config.setHostname("localhost");
	config.setAppname(appname);
	config.setVirtualHostName(appname);
	config.setSecureVirtualHostName(appname);
	config.setNonSecurePort(4444);
	config.setSecurePort(8443);
	config.setInstanceId("127.0.0.1:customapp:4444");

	info = new EurekaConfigBasedInstanceInfoProvider(config).get();
}
 
Example 8
@Before
public void setup() {
	eurekaHttpClient = new RestTemplateTransportClientFactory()
			.newClient(new DefaultEndpoint(serviceUrl));

	EurekaInstanceConfigBean config = new EurekaInstanceConfigBean(inetUtils);

	String appname = "customapp";
	config.setIpAddress("127.0.0.1");
	config.setHostname("localhost");
	config.setAppname(appname);
	config.setVirtualHostName(appname);
	config.setSecureVirtualHostName(appname);
	config.setNonSecurePort(4444);
	config.setSecurePort(8443);
	config.setInstanceId("127.0.0.1:customapp:4444");

	info = new EurekaConfigBasedInstanceInfoProvider(config).get();
}
 
Example 9
@RequestMapping(path = "/register", method = POST)
public String register() {
	EurekaInstanceConfigBean config = new EurekaInstanceConfigBean(inetUtils);
	String appname = "customapp";
	config.setIpAddress("127.0.0.1");
	config.setHostname("localhost");
	config.setAppname(appname);
	config.setVirtualHostName(appname);
	config.setSecureVirtualHostName(appname);
	config.setNonSecurePort(4444);
	config.setInstanceId("127.0.0.1:customapp:4444");

	this.registration = EurekaRegistration.builder(config)
			.with(this.clientConfig, this.context).build();

	this.serviceRegistry.register(this.registration);
	return config.getInstanceId();
}
 
Example 10
@Test
public void notOverridingMetamapSettings() {
	new ApplicationContextRunner()
			.withConfiguration(AutoConfigurations.of(
					EurekaClientConfigServerAutoConfiguration.class,
					ConfigServerProperties.class, EurekaInstanceConfigBean.class))
			.withPropertyValues("spring.cloud.config.server.prefix=/config")
			.withPropertyValues(
					"eureka.instance.metadataMap.configPath=/differentpath")
			.run(c -> {
				assertThat(c.getBeanNamesForType(EurekaInstanceConfig.class).length)
						.isEqualTo(1);
				EurekaInstanceConfig instance = c.getBean(EurekaInstanceConfig.class);
				assertThat(instance.getMetadataMap().get("configPath"))
						.isEqualTo("/differentpath");
			});
}
 
Example 11
Source Project: Moss   Source File: EurekaAutoRegistrationCustomizer.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public ManagementMetadata get(EurekaInstanceConfigBean instance, int serverPort, String serverContextPath, String managementContextPath, Integer managementPort) {
    customizeMetadata(instance);
    ManagementMetadata managementMetadata = super.get(instance,
            serverPort,
            serverContextPath,
            managementContextPath,
            managementPort);

    return managementMetadata;
}
 
Example 12
Source Project: Moss   Source File: EurekaAutoRegistrationCustomizer.java    License: Apache License 2.0 5 votes vote down vote up
private String instanceId(EurekaInstanceConfigBean instance) {
    String processId = metaDataProvider.getProcessId();
    return new StringBuilder(processId) // [email protected]:port
                    .append('@')
                    .append(instance.getHostName(false))
                    .append(':')
                    .append(metaDataProvider.getServerPort())
                    .toString();

}
 
Example 13
Source Project: Moss   Source File: EurekaAutoRegistrationCustomizer.java    License: Apache License 2.0 5 votes vote down vote up
private void customizeMetadata(EurekaInstanceConfigBean instance) {
    instance.setInstanceId(instanceId(instance));
    Map<String, String> metadata = instance.getMetadataMap();
    String processId = metaDataProvider.getProcessId();
    if(metadata != null) {
        metadata.put(MANAGEMENT_PORT, metaDataProvider.getManagementPort() + "");
        metadata.put(PID, processId);
    }
}
 
Example 14
Source Project: Moss   Source File: EurekaAutoRegistrationCustomizer.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public ManagementMetadata get(EurekaInstanceConfigBean instance, int serverPort, String serverContextPath, String managementContextPath, Integer managementPort) {
    customizeMetadata(instance);
    ManagementMetadata managementMetadata = super.get(instance,
            serverPort,
            serverContextPath,
            managementContextPath,
            managementPort);

    return managementMetadata;
}
 
Example 15
Source Project: Moss   Source File: EurekaAutoRegistrationCustomizer.java    License: Apache License 2.0 5 votes vote down vote up
private String instanceId(EurekaInstanceConfigBean instance) {

        String processId = metaDataProvider.getProcessId();
        return new StringBuilder(processId) // [email protected]:port
                        .append('@')
                        .append(instance.getHostName(false))
                        .append(':')
                        .append(metaDataProvider.getServerPort())
                        .toString();

    }
 
Example 16
Source Project: Moss   Source File: EurekaAutoRegistrationCustomizer.java    License: Apache License 2.0 5 votes vote down vote up
private void customizeMetadata(EurekaInstanceConfigBean instance) {
    instance.setInstanceId(instanceId(instance));
    Map<String, String> metadata = instance.getMetadataMap();
    String processId = metaDataProvider.getProcessId();
    if(metadata != null) {
        metadata.put(MANAGEMENT_PORT, metaDataProvider.getManagementPort() + "");
        metadata.put(PID, processId);
    }
}
 
Example 17
@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 18
@Bean
@ConditionalOnMissingBean(value = EurekaInstanceConfig.class, search = SearchStrategy.CURRENT)
public EurekaInstanceConfigBean eurekaInstanceConfigBean(
        InetUtils inetUtils, ManagementMetadataProvider managementMetadataProvider) {
    EurekaInstanceConfigBean instance = new EurekaClientAutoConfiguration(env)
            .eurekaInstanceConfigBean(inetUtils, managementMetadataProvider);

    String brpcPort = env.getProperty(ENV_PORT_KEY);
    if (StringUtils.isNoneBlank(brpcPort)) {
        instance.getMetadataMap().put(META_DATA_PORT_KEY, brpcPort);
    }
    return instance;
}
 
Example 19
@Bean(initMethod = "registerStubs")
public StubsRegistrar stubsRegistrar(StubRunning stubRunning,
		ServiceRegistry<EurekaRegistration> serviceRegistry,
		ApplicationContext context, StubMapperProperties stubMapperProperties,
		InetUtils inetUtils, EurekaInstanceConfigBean eurekaInstanceConfigBean,
		EurekaClientConfigBean eurekaClientConfigBean) {
	return new EurekaStubsRegistrar(stubRunning, serviceRegistry,
			stubMapperProperties, inetUtils, eurekaInstanceConfigBean,
			eurekaClientConfigBean, context);
}
 
Example 20
public EurekaStubsRegistrar(StubRunning stubRunning,
		ServiceRegistry<EurekaRegistration> serviceRegistry,
		StubMapperProperties stubMapperProperties, InetUtils inetUtils,
		EurekaInstanceConfigBean eurekaInstanceConfigBean,
		EurekaClientConfigBean eurekaClientConfigBean, ApplicationContext context) {
	this.stubRunning = stubRunning;
	this.stubMapperProperties = stubMapperProperties;
	this.serviceRegistry = serviceRegistry;
	this.inetUtils = inetUtils;
	this.eurekaInstanceConfigBean = eurekaInstanceConfigBean;
	this.eurekaClientConfigBean = eurekaClientConfigBean;
	this.context = context;
}
 
Example 21
@Test
public void testDefaultRegistration() {
	EurekaInstanceConfigBean eurekaInstanceConfigBean = eurekaInstanceAutoConfiguration.eurekaInstanceConfigBean();
	assertEquals(HOSTNAME + ":" + INSTANCE_ID, eurekaInstanceConfigBean.getInstanceId());
	assertEquals(HOSTNAME, eurekaInstanceConfigBean.getHostname());
	assertEquals(80, eurekaInstanceConfigBean.getNonSecurePort());
	assertEquals(443, eurekaInstanceConfigBean.getSecurePort());
	assertTrue(eurekaInstanceConfigBean.getSecurePortEnabled());

	Map<String, String> metadata = eurekaInstanceConfigBean.getMetadataMap();
	assertEquals(INSTANCE_GUID, metadata.get("cfAppGuid"));
	assertEquals(INSTANCE_INDEX, metadata.get("cfInstanceIndex"));
	assertEquals(INSTANCE_ID, metadata.get("instanceId"));
	assertEquals(ZONE, metadata.get("zone"));
}
 
Example 22
@Test
public void testDirectRegistration() {
	eurekaInstanceAutoConfiguration.setRegistrationMethod(DIRECT_REGISTRATION_METHOD);
	EurekaInstanceConfigBean eurekaInstanceConfigBean = eurekaInstanceAutoConfiguration.eurekaInstanceConfigBean();
	assertTrue(eurekaInstanceConfigBean.isPreferIpAddress());
	assertEquals(IP + ":" + INSTANCE_ID, eurekaInstanceConfigBean.getInstanceId());
	assertEquals(IP, eurekaInstanceConfigBean.getHostname());
	assertEquals(PORT, eurekaInstanceConfigBean.getNonSecurePort());
	assertFalse(eurekaInstanceConfigBean.getSecurePortEnabled());
}
 
Example 23
@Test
public void testDirectRegistration() {
	contextRunner.withPropertyValues("spring.cloud.services.registrationMethod=" + DIRECT_REGISTRATION_METHOD)
			.run(context -> {
				assertThat(context).hasSingleBean(EurekaInstanceConfigBean.class);
				EurekaInstanceConfigBean eurekaInstanceConfigBean = context.getBean(EurekaInstanceConfigBean.class);
				assertTrue(eurekaInstanceConfigBean.isPreferIpAddress());
				assertEquals(IP + ":" + INSTANCE_ID, eurekaInstanceConfigBean.getInstanceId());
				assertEquals(IP, eurekaInstanceConfigBean.getHostname());
				assertEquals(PORT, eurekaInstanceConfigBean.getNonSecurePort());
				assertFalse(eurekaInstanceConfigBean.getSecurePortEnabled());
			});
}
 
Example 24
@Test
public void testEmptyDefaultZoneUri() {
	contextRunner.withPropertyValues("eureka.client.serviceUrl.defaultZone=").run(context -> {
		assertThat(context).hasSingleBean(EurekaInstanceConfigBean.class);
		EurekaInstanceConfigBean eurekaInstanceConfigBean = context.getBean(EurekaInstanceConfigBean.class);
		assertEquals(UNKNOWN_ZONE, eurekaInstanceConfigBean.getMetadataMap().get("zone"));
	});
}
 
Example 25
@Test
public void testShortDefaultZoneUri() {
	contextRunner.withPropertyValues("eureka.client.serviceUrl.defaultZone=https://funkylocaldomainname/eureka/")
			.run(context -> {
				assertThat(context).hasSingleBean(EurekaInstanceConfigBean.class);
				EurekaInstanceConfigBean eurekaInstanceConfigBean = context.getBean(EurekaInstanceConfigBean.class);
				assertEquals(UNKNOWN_ZONE, eurekaInstanceConfigBean.getMetadataMap().get("zone"));
			});
}
 
Example 26
@Test
public void testMalformedDefaultZoneUri() {
	contextRunner.withPropertyValues("eureka.client.serviceUrl.defaultZone=:").run(context -> {
		assertThat(context).hasSingleBean(EurekaInstanceConfigBean.class);
		EurekaInstanceConfigBean eurekaInstanceConfigBean = context.getBean(EurekaInstanceConfigBean.class);
		assertEquals(UNKNOWN_ZONE, eurekaInstanceConfigBean.getMetadataMap().get("zone"));
	});
}
 
Example 27
protected String getHealthCheckUrl(EurekaInstanceConfigBean instance, int serverPort,
		String serverContextPath, String managementContextPath,
		Integer managementPort, boolean isSecure) {
	String healthCheckUrlPath = instance.getHealthCheckUrlPath();
	String healthCheckUrl = getUrl(instance, serverPort, serverContextPath,
			managementContextPath, managementPort, healthCheckUrlPath, isSecure);
	log.debug("Constructed eureka meta-data healthcheckUrl: " + healthCheckUrl);
	return healthCheckUrl;
}
 
Example 28
public String getStatusPageUrl(EurekaInstanceConfigBean instance, int serverPort,
		String serverContextPath, String managementContextPath,
		Integer managementPort) {
	String statusPageUrlPath = instance.getStatusPageUrlPath();
	String statusPageUrl = getUrl(instance, serverPort, serverContextPath,
			managementContextPath, managementPort, statusPageUrlPath, false);
	log.debug("Constructed eureka meta-data statusPageUrl: " + statusPageUrl);
	return statusPageUrl;
}
 
Example 29
private String getUrl(EurekaInstanceConfigBean instance, int serverPort,
		String serverContextPath, String managementContextPath,
		Integer managementPort, String urlPath, boolean isSecure) {
	managementContextPath = refineManagementContextPath(serverContextPath,
			managementContextPath, managementPort);
	if (managementPort == null) {
		managementPort = serverPort;
	}
	String scheme = isSecure ? "https" : "http";
	return constructValidUrl(scheme, instance.getHostname(), managementPort,
			managementContextPath, urlPath);
}
 
Example 30
@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());
}