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

The following examples show how to use org.springframework.cloud.netflix.eureka.EurekaInstanceConfigBean. 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: EurekaClientConfigServerAutoConfigurationTests.java    From spring-cloud-netflix with Apache License 2.0 6 votes vote down vote up
@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 #2
Source File: EurekaInstanceAutoConfiguration.java    From spring-cloud-services-starters with Apache License 2.0 6 votes vote down vote up
@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
Source File: EurekaInstanceAutoConfigurationTest.java    From spring-cloud-services-starters with Apache License 2.0 6 votes vote down vote up
@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 #4
Source File: DefaultManagementMetadataProvider.java    From spring-cloud-netflix with Apache License 2.0 6 votes vote down vote up
@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 #5
Source File: EurekaInstanceAutoConfiguration.java    From spring-cloud-services-connector with Apache License 2.0 6 votes vote down vote up
@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 #6
Source File: EurekaStubsRegistrar.java    From spring-cloud-contract with Apache License 2.0 6 votes vote down vote up
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 #7
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 #8
Source File: WebClientEurekaHttpClientTest.java    From spring-cloud-netflix with Apache License 2.0 6 votes vote down vote up
@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 #9
Source File: RestTemplateEurekaHttpClientTest.java    From spring-cloud-netflix with Apache License 2.0 6 votes vote down vote up
@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 #10
Source File: EurekaSampleApplication.java    From spring-cloud-netflix with Apache License 2.0 6 votes vote down vote up
@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 #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: 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 #13
Source File: DefaultManagementMetadataProvider.java    From spring-cloud-netflix with Apache License 2.0 5 votes vote down vote up
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 #14
Source File: DefaultManagementMetadataProvider.java    From spring-cloud-netflix with Apache License 2.0 5 votes vote down vote up
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 #15
Source File: DefaultManagementMetadataProvider.java    From spring-cloud-netflix with Apache License 2.0 5 votes vote down vote up
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 #16
Source File: EurekaClientConfigServerAutoConfigurationTests.java    From spring-cloud-netflix with Apache License 2.0 5 votes vote down vote up
@Test
public void offByDefault() {
	new ApplicationContextRunner().withConfiguration(
			AutoConfigurations.of(EurekaClientConfigServerAutoConfiguration.class))
			.run(c -> {
				assertThat(
						c.getBeanNamesForType(EurekaInstanceConfigBean.class).length)
								.isEqualTo(0);
			});
}
 
Example #17
Source File: EurekaInstanceAutoConfigurationTest.java    From spring-cloud-services-starters with Apache License 2.0 5 votes vote down vote up
@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 #18
Source File: EurekaInstanceAutoConfigurationTest.java    From spring-cloud-services-starters with Apache License 2.0 5 votes vote down vote up
@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 #19
Source File: EurekaInstanceAutoConfigurationTest.java    From spring-cloud-services-starters with Apache License 2.0 5 votes vote down vote up
@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 #20
Source File: EurekaInstanceAutoConfigurationTest.java    From spring-cloud-services-starters with Apache License 2.0 5 votes vote down vote up
@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 #21
Source File: EurekaClientConfigServerAutoConfigurationTests.java    From spring-cloud-netflix with Apache License 2.0 5 votes vote down vote up
@Test
public void onWhenRequested() {
	new ApplicationContextRunner()
			.withConfiguration(AutoConfigurations.of(
					EurekaClientConfigServerAutoConfiguration.class,
					ConfigServerProperties.class, EurekaInstanceConfigBean.class))
			.withPropertyValues("spring.cloud.config.server.prefix=/config")
			.run(c -> {
				assertThat(c.getBeanNamesForType(EurekaInstanceConfig.class).length)
						.isEqualTo(1);
				EurekaInstanceConfig instance = c.getBean(EurekaInstanceConfig.class);
				assertThat(instance.getMetadataMap().get("configPath"))
						.isEqualTo("/config");
			});
}
 
Example #22
Source File: EurekaAutoRegistrationCustomizer.java    From Moss with 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 #23
Source File: EurekaAutoRegistrationCustomizer.java    From Moss with Apache License 2.0 5 votes vote down vote up
private String instanceId(EurekaInstanceConfigBean instance) {
    String processId = metaDataProvider.getProcessId();
    return new StringBuilder(processId) // pid@ip:port
                    .append('@')
                    .append(instance.getHostName(false))
                    .append(':')
                    .append(metaDataProvider.getServerPort())
                    .toString();

}
 
Example #24
Source File: EurekaAutoRegistrationCustomizer.java    From Moss with 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 #25
Source File: EurekaInstanceAutoConfigurationTest.java    From spring-cloud-services-connector with Apache License 2.0 5 votes vote down vote up
@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 #26
Source File: EurekaAutoRegistrationCustomizer.java    From Moss with 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 #27
Source File: EurekaInstanceAutoConfigurationTest.java    From spring-cloud-services-connector with Apache License 2.0 5 votes vote down vote up
@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 #28
Source File: EurekaStubsRegistrar.java    From spring-cloud-contract with Apache License 2.0 5 votes vote down vote up
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 #29
Source File: StubRunnerSpringCloudEurekaAutoConfiguration.java    From spring-cloud-contract with Apache License 2.0 5 votes vote down vote up
@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 #30
Source File: EurekaAutoRegistrationCustomizer.java    From Moss with Apache License 2.0 5 votes vote down vote up
private String instanceId(EurekaInstanceConfigBean instance) {

        String processId = metaDataProvider.getProcessId();
        return new StringBuilder(processId) // pid@ip:port
                        .append('@')
                        .append(instance.getHostName(false))
                        .append(':')
                        .append(metaDataProvider.getServerPort())
                        .toString();

    }