Commit 51ea8afb by Spencer Gibb

Set port on InstanceInfo to support random server.port.

fixes gh-570
parent 5065e475
...@@ -16,9 +16,13 @@ ...@@ -16,9 +16,13 @@
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 org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.endpoint.Endpoint; import org.springframework.boot.actuate.endpoint.Endpoint;
import org.springframework.boot.actuate.health.HealthAggregator; import org.springframework.boot.actuate.health.HealthAggregator;
...@@ -39,16 +43,16 @@ import org.springframework.context.annotation.Configuration; ...@@ -39,16 +43,16 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextClosedEvent; import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.EventListener; import org.springframework.context.event.EventListener;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.util.ReflectionUtils;
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 lombok.extern.apachecommons.CommonsLog;
/** /**
* @author Dave Syer * @author Dave Syer
* @author Spencer Gibb * @author Spencer Gibb
...@@ -83,11 +87,15 @@ public class EurekaDiscoveryClientConfiguration implements SmartLifecycle, Order ...@@ -83,11 +87,15 @@ public class EurekaDiscoveryClientConfiguration implements SmartLifecycle, Order
@Autowired @Autowired
private EurekaClient eurekaClient; private EurekaClient eurekaClient;
@Autowired
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());
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
...@@ -114,6 +122,13 @@ public class EurekaDiscoveryClientConfiguration implements SmartLifecycle, Order ...@@ -114,6 +122,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) {
......
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