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 {
@ConditionalOnMissingBean(value = EurekaClient.class, search = SearchStrategy.CURRENT)
@SneakyThrows
public EurekaClient eurekaClient(ApplicationInfoManager applicationInfoManager,
EurekaClientConfig config, EurekaInstanceConfig instance) {
applicationInfoManager.initComponent(instance);
EurekaClientConfig config) {
return new CloudEurekaClient(applicationInfoManager, config, optionalArgs, this.context);
}
}
......@@ -93,7 +92,6 @@ public class DiscoveryClientConfiguration {
@org.springframework.cloud.context.config.annotation.RefreshScope
public EurekaClient eurekaClient(ApplicationInfoManager applicationInfoManager,
EurekaClientConfig config, EurekaInstanceConfig instance) {
applicationInfoManager.initComponent(instance);
return new CloudEurekaClient(applicationInfoManager, config, optionalArgs, this.context);
}
......
......@@ -90,7 +90,7 @@ public class EurekaClientAutoConfiguration {
@Bean
@ConditionalOnMissingBean(InstanceInfo.class)
public MutableInstanceInfo instanceInfo(EurekaInstanceConfig config) {
public InstanceInfo instanceInfo(EurekaInstanceConfig config) {
return new InstanceInfoFactory().create(config);
}
......
......@@ -16,9 +16,11 @@
package org.springframework.cloud.netflix.eureka;
import java.lang.reflect.Field;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import lombok.SneakyThrows;
import lombok.extern.apachecommons.CommonsLog;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -46,9 +48,11 @@ import org.springframework.core.Ordered;
import com.netflix.appinfo.ApplicationInfoManager;
import com.netflix.appinfo.EurekaInstanceConfig;
import com.netflix.appinfo.HealthCheckHandler;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.appinfo.InstanceInfo.InstanceStatus;
import com.netflix.discovery.EurekaClient;
import com.netflix.discovery.EurekaClientConfig;
import org.springframework.util.ReflectionUtils;
/**
* @author Dave Syer
......@@ -86,14 +90,14 @@ public class EurekaDiscoveryClientConfiguration implements SmartLifecycle, Order
private EurekaClient eurekaClient;
@Autowired
private MutableInstanceInfo instanceInfo;
private InstanceInfo instanceInfo;
@Override
public void start() {
// only set the port if the nonSecurePort is 0 and this.port != 0
if (this.port.get() != 0 && this.instanceConfig.getNonSecurePort() == 0) {
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
......@@ -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
public void stop() {
if (this.applicationInfoManager.getInfo() != null) {
......
......@@ -31,7 +31,7 @@ import com.netflix.appinfo.LeaseInfo;
@CommonsLog
public class InstanceInfoFactory {
public MutableInstanceInfo create(EurekaInstanceConfig config) {
public InstanceInfo create(EurekaInstanceConfig config) {
LeaseInfo.Builder leaseInfoBuilder = LeaseInfo.Builder.newBuilder()
.setRenewalIntervalInSecs(config.getLeaseRenewalIntervalInSeconds())
.setDurationInSecs(config.getLeaseExpirationDurationInSeconds());
......@@ -87,6 +87,6 @@ public class InstanceInfoFactory {
InstanceInfo instanceInfo = builder.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;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.times;
import javax.annotation.PostConstruct;
import org.junit.After;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.cloud.config.client.ConfigClientProperties;
import org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration;
import org.springframework.cloud.netflix.eureka.EurekaDiscoveryClientConfiguration;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment;
import com.netflix.appinfo.ApplicationInfoManager;
import com.netflix.appinfo.InstanceInfo;
......@@ -46,11 +43,6 @@ public class DiscoveryClientConfigServiceAutoConfigurationTests {
private AnnotationConfigApplicationContext context;
private EurekaClient client = Mockito.mock(EurekaClient.class);
private InstanceInfo info = InstanceInfo.Builder.newBuilder().setAppName("app")
.setHostName("foo").setHomePageUrl("/", null).build();
@After
public void close() {
if (this.context != null) {
......@@ -63,14 +55,16 @@ public class DiscoveryClientConfigServiceAutoConfigurationTests {
@Test
public void onWhenRequested() throws Exception {
given(this.client.getNextServerFromEureka("CONFIGSERVER", false))
.willReturn(this.info);
setup("spring.cloud.config.discovery.enabled=true");
setup("spring.cloud.config.discovery.enabled=true",
"eureka.instance.metadataMap.foo:bar",
"eureka.instance.nonSecurePort:7001",
"eureka.instance.hostname:foo");
assertEquals(1, this.context.getBeanNamesForType(
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);
Mockito.verify(this.client, times(1)).shutdown();
Mockito.verify(eurekaClient, times(1)).shutdown();
ConfigClientProperties locator = this.context
.getBean(ConfigClientProperties.class);
assertEquals("http://foo:7001/", locator.getRawUri());
......@@ -82,8 +76,6 @@ public class DiscoveryClientConfigServiceAutoConfigurationTests {
private void setup(String... env) {
AnnotationConfigApplicationContext parent = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(parent, env);
parent.getDefaultListableBeanFactory().registerSingleton("eurekaClient",
this.client);
parent.register(PropertyPlaceholderAutoConfiguration.class,
DiscoveryClientConfigServiceBootstrapConfiguration.class,
EnvironmentKnobbler.class, ConfigClientProperties.class);
......@@ -100,13 +92,12 @@ public class DiscoveryClientConfigServiceAutoConfigurationTests {
@Configuration
protected static class EnvironmentKnobbler {
@Autowired
private ConfigurableEnvironment environment;
@PostConstruct
public void init() {
EnvironmentTestUtils.addEnvironment(this.environment,
"eureka.instance.metadataMap.foo:bar");
@Bean
public EurekaClient eurekaClient(InstanceInfo info) {
EurekaClient client = Mockito.mock(EurekaClient.class);
given(client.getNextServerFromEureka("CONFIGSERVER", false))
.willReturn(info);
return client;
}
}
......
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