Commit 3dfb8b5d by Spencer Gibb Committed by Dave Syer

fix support for registering random ports with eureka.

fixes gh-570 ... again
parent 31e82a75
...@@ -71,8 +71,7 @@ public class DiscoveryClientConfiguration { ...@@ -71,8 +71,7 @@ public class DiscoveryClientConfiguration {
@ConditionalOnMissingBean(value = EurekaClient.class, search = SearchStrategy.CURRENT) @ConditionalOnMissingBean(value = EurekaClient.class, search = SearchStrategy.CURRENT)
@SneakyThrows @SneakyThrows
public EurekaClient eurekaClient(ApplicationInfoManager applicationInfoManager, public EurekaClient eurekaClient(ApplicationInfoManager applicationInfoManager,
EurekaClientConfig config, EurekaInstanceConfig instance) { EurekaClientConfig config) {
applicationInfoManager.initComponent(instance);
return new CloudEurekaClient(applicationInfoManager, config, optionalArgs, this.context); return new CloudEurekaClient(applicationInfoManager, config, optionalArgs, this.context);
} }
} }
...@@ -93,7 +92,6 @@ public class DiscoveryClientConfiguration { ...@@ -93,7 +92,6 @@ public class DiscoveryClientConfiguration {
@org.springframework.cloud.context.config.annotation.RefreshScope @org.springframework.cloud.context.config.annotation.RefreshScope
public EurekaClient eurekaClient(ApplicationInfoManager applicationInfoManager, public EurekaClient eurekaClient(ApplicationInfoManager applicationInfoManager,
EurekaClientConfig config, EurekaInstanceConfig instance) { EurekaClientConfig config, EurekaInstanceConfig instance) {
applicationInfoManager.initComponent(instance);
return new CloudEurekaClient(applicationInfoManager, config, optionalArgs, this.context); return new CloudEurekaClient(applicationInfoManager, config, optionalArgs, this.context);
} }
......
...@@ -90,7 +90,7 @@ public class EurekaClientAutoConfiguration { ...@@ -90,7 +90,7 @@ public class EurekaClientAutoConfiguration {
@Bean @Bean
@ConditionalOnMissingBean(InstanceInfo.class) @ConditionalOnMissingBean(InstanceInfo.class)
public MutableInstanceInfo instanceInfo(EurekaInstanceConfig config) { public InstanceInfo instanceInfo(EurekaInstanceConfig config) {
return new InstanceInfoFactory().create(config); return new InstanceInfoFactory().create(config);
} }
......
...@@ -16,9 +16,11 @@ ...@@ -16,9 +16,11 @@
package org.springframework.cloud.netflix.eureka; package org.springframework.cloud.netflix.eureka;
import java.lang.reflect.Field;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import lombok.SneakyThrows;
import lombok.extern.apachecommons.CommonsLog; import lombok.extern.apachecommons.CommonsLog;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -46,9 +48,11 @@ import org.springframework.core.Ordered; ...@@ -46,9 +48,11 @@ import org.springframework.core.Ordered;
import com.netflix.appinfo.ApplicationInfoManager; import com.netflix.appinfo.ApplicationInfoManager;
import com.netflix.appinfo.EurekaInstanceConfig; import com.netflix.appinfo.EurekaInstanceConfig;
import com.netflix.appinfo.HealthCheckHandler; import com.netflix.appinfo.HealthCheckHandler;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.appinfo.InstanceInfo.InstanceStatus; import com.netflix.appinfo.InstanceInfo.InstanceStatus;
import com.netflix.discovery.EurekaClient; import com.netflix.discovery.EurekaClient;
import com.netflix.discovery.EurekaClientConfig; import com.netflix.discovery.EurekaClientConfig;
import org.springframework.util.ReflectionUtils;
/** /**
* @author Dave Syer * @author Dave Syer
...@@ -86,14 +90,14 @@ public class EurekaDiscoveryClientConfiguration implements SmartLifecycle, Order ...@@ -86,14 +90,14 @@ public class EurekaDiscoveryClientConfiguration implements SmartLifecycle, Order
private EurekaClient eurekaClient; private EurekaClient eurekaClient;
@Autowired @Autowired
private MutableInstanceInfo instanceInfo; private InstanceInfo instanceInfo;
@Override @Override
public void start() { public void start() {
// only set the port if the nonSecurePort is 0 and this.port != 0 // only set the port if the nonSecurePort is 0 and this.port != 0
if (this.port.get() != 0 && this.instanceConfig.getNonSecurePort() == 0) { if (this.port.get() != 0 && this.instanceConfig.getNonSecurePort() == 0) {
this.instanceConfig.setNonSecurePort(this.port.get()); this.instanceConfig.setNonSecurePort(this.port.get());
instanceInfo.setPort(this.port.get()); setInstanceInfoPort();
} }
// only initialize if nonSecurePort is greater than 0 and it isn't already running // only initialize if nonSecurePort is greater than 0 and it isn't already running
...@@ -120,6 +124,13 @@ public class EurekaDiscoveryClientConfiguration implements SmartLifecycle, Order ...@@ -120,6 +124,13 @@ public class EurekaDiscoveryClientConfiguration implements SmartLifecycle, Order
} }
} }
@SneakyThrows
private void setInstanceInfoPort() {
Field port = ReflectionUtils.findField(InstanceInfo.class, "port");
ReflectionUtils.makeAccessible(port);
port.setInt(this.instanceInfo, this.port.get());
}
@Override @Override
public void stop() { public void stop() {
if (this.applicationInfoManager.getInfo() != null) { if (this.applicationInfoManager.getInfo() != null) {
......
...@@ -31,7 +31,7 @@ import com.netflix.appinfo.LeaseInfo; ...@@ -31,7 +31,7 @@ import com.netflix.appinfo.LeaseInfo;
@CommonsLog @CommonsLog
public class InstanceInfoFactory { public class InstanceInfoFactory {
public MutableInstanceInfo create(EurekaInstanceConfig config) { public InstanceInfo create(EurekaInstanceConfig config) {
LeaseInfo.Builder leaseInfoBuilder = LeaseInfo.Builder.newBuilder() LeaseInfo.Builder leaseInfoBuilder = LeaseInfo.Builder.newBuilder()
.setRenewalIntervalInSecs(config.getLeaseRenewalIntervalInSeconds()) .setRenewalIntervalInSecs(config.getLeaseRenewalIntervalInSeconds())
.setDurationInSecs(config.getLeaseExpirationDurationInSeconds()); .setDurationInSecs(config.getLeaseExpirationDurationInSeconds());
...@@ -87,6 +87,6 @@ public class InstanceInfoFactory { ...@@ -87,6 +87,6 @@ public class InstanceInfoFactory {
InstanceInfo instanceInfo = builder.build(); InstanceInfo instanceInfo = builder.build();
instanceInfo.setLeaseInfo(leaseInfoBuilder.build()); instanceInfo.setLeaseInfo(leaseInfoBuilder.build());
return new MutableInstanceInfo(instanceInfo); return instanceInfo;
} }
} }
/*
* Copyright 2013-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.netflix.eureka;
import com.netflix.appinfo.InstanceInfo;
/**
* @author Spencer Gibb
*/
class MutableInstanceInfo extends InstanceInfo {
private Integer port;
public MutableInstanceInfo(InstanceInfo ii) {
super(ii);
}
@Override
public int getPort() {
if (this.port != null) {
return this.port;
}
return super.getPort();
}
public void setPort(int port) {
this.port = port;
}
}
...@@ -20,20 +20,17 @@ import static org.junit.Assert.assertEquals; ...@@ -20,20 +20,17 @@ import static org.junit.Assert.assertEquals;
import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.times; import static org.mockito.Mockito.times;
import javax.annotation.PostConstruct;
import org.junit.After; import org.junit.After;
import org.junit.Test; import org.junit.Test;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.test.EnvironmentTestUtils; import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.cloud.config.client.ConfigClientProperties; import org.springframework.cloud.config.client.ConfigClientProperties;
import org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration; import org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration;
import org.springframework.cloud.netflix.eureka.EurekaDiscoveryClientConfiguration; import org.springframework.cloud.netflix.eureka.EurekaDiscoveryClientConfiguration;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment;
import com.netflix.appinfo.ApplicationInfoManager; import com.netflix.appinfo.ApplicationInfoManager;
import com.netflix.appinfo.InstanceInfo; import com.netflix.appinfo.InstanceInfo;
...@@ -46,11 +43,6 @@ public class DiscoveryClientConfigServiceAutoConfigurationTests { ...@@ -46,11 +43,6 @@ public class DiscoveryClientConfigServiceAutoConfigurationTests {
private AnnotationConfigApplicationContext context; private AnnotationConfigApplicationContext context;
private EurekaClient client = Mockito.mock(EurekaClient.class);
private InstanceInfo info = InstanceInfo.Builder.newBuilder().setAppName("app")
.setHostName("foo").setHomePageUrl("/", null).build();
@After @After
public void close() { public void close() {
if (this.context != null) { if (this.context != null) {
...@@ -63,14 +55,16 @@ public class DiscoveryClientConfigServiceAutoConfigurationTests { ...@@ -63,14 +55,16 @@ public class DiscoveryClientConfigServiceAutoConfigurationTests {
@Test @Test
public void onWhenRequested() throws Exception { public void onWhenRequested() throws Exception {
given(this.client.getNextServerFromEureka("CONFIGSERVER", false)) setup("spring.cloud.config.discovery.enabled=true",
.willReturn(this.info); "eureka.instance.metadataMap.foo:bar",
setup("spring.cloud.config.discovery.enabled=true"); "eureka.instance.nonSecurePort:7001",
"eureka.instance.hostname:foo");
assertEquals(1, this.context.getBeanNamesForType( assertEquals(1, this.context.getBeanNamesForType(
DiscoveryClientConfigServiceAutoConfiguration.class).length); DiscoveryClientConfigServiceAutoConfiguration.class).length);
Mockito.verify(this.client, times(2)).getNextServerFromEureka("CONFIGSERVER", EurekaClient eurekaClient = this.context.getParent().getBean(EurekaClient.class);
Mockito.verify(eurekaClient, times(2)).getNextServerFromEureka("CONFIGSERVER",
false); false);
Mockito.verify(this.client, times(1)).shutdown(); Mockito.verify(eurekaClient, times(1)).shutdown();
ConfigClientProperties locator = this.context ConfigClientProperties locator = this.context
.getBean(ConfigClientProperties.class); .getBean(ConfigClientProperties.class);
assertEquals("http://foo:7001/", locator.getRawUri()); assertEquals("http://foo:7001/", locator.getRawUri());
...@@ -82,8 +76,6 @@ public class DiscoveryClientConfigServiceAutoConfigurationTests { ...@@ -82,8 +76,6 @@ public class DiscoveryClientConfigServiceAutoConfigurationTests {
private void setup(String... env) { private void setup(String... env) {
AnnotationConfigApplicationContext parent = new AnnotationConfigApplicationContext(); AnnotationConfigApplicationContext parent = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(parent, env); EnvironmentTestUtils.addEnvironment(parent, env);
parent.getDefaultListableBeanFactory().registerSingleton("eurekaClient",
this.client);
parent.register(PropertyPlaceholderAutoConfiguration.class, parent.register(PropertyPlaceholderAutoConfiguration.class,
DiscoveryClientConfigServiceBootstrapConfiguration.class, DiscoveryClientConfigServiceBootstrapConfiguration.class,
EnvironmentKnobbler.class, ConfigClientProperties.class); EnvironmentKnobbler.class, ConfigClientProperties.class);
...@@ -100,13 +92,12 @@ public class DiscoveryClientConfigServiceAutoConfigurationTests { ...@@ -100,13 +92,12 @@ public class DiscoveryClientConfigServiceAutoConfigurationTests {
@Configuration @Configuration
protected static class EnvironmentKnobbler { protected static class EnvironmentKnobbler {
@Autowired @Bean
private ConfigurableEnvironment environment; public EurekaClient eurekaClient(InstanceInfo info) {
EurekaClient client = Mockito.mock(EurekaClient.class);
@PostConstruct given(client.getNextServerFromEureka("CONFIGSERVER", false))
public void init() { .willReturn(info);
EnvironmentTestUtils.addEnvironment(this.environment, return client;
"eureka.instance.metadataMap.foo:bar");
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment