Skip to content

Commit 51ea8af

Browse files
committed
Set port on InstanceInfo to support random server.port.
fixes spring-cloudgh-570
1 parent 5065e47 commit 51ea8af

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/eureka/EurekaDiscoveryClientConfiguration.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,13 @@
1616

1717
package org.springframework.cloud.netflix.eureka;
1818

19+
import java.lang.reflect.Field;
1920
import java.util.concurrent.atomic.AtomicBoolean;
2021
import java.util.concurrent.atomic.AtomicInteger;
2122

23+
import lombok.SneakyThrows;
24+
import lombok.extern.apachecommons.CommonsLog;
25+
2226
import org.springframework.beans.factory.annotation.Autowired;
2327
import org.springframework.boot.actuate.endpoint.Endpoint;
2428
import org.springframework.boot.actuate.health.HealthAggregator;
@@ -39,16 +43,16 @@
3943
import org.springframework.context.event.ContextClosedEvent;
4044
import org.springframework.context.event.EventListener;
4145
import org.springframework.core.Ordered;
46+
import org.springframework.util.ReflectionUtils;
4247

4348
import com.netflix.appinfo.ApplicationInfoManager;
4449
import com.netflix.appinfo.EurekaInstanceConfig;
4550
import com.netflix.appinfo.HealthCheckHandler;
51+
import com.netflix.appinfo.InstanceInfo;
4652
import com.netflix.appinfo.InstanceInfo.InstanceStatus;
4753
import com.netflix.discovery.EurekaClient;
4854
import com.netflix.discovery.EurekaClientConfig;
4955

50-
import lombok.extern.apachecommons.CommonsLog;
51-
5256
/**
5357
* @author Dave Syer
5458
* @author Spencer Gibb
@@ -83,11 +87,15 @@ public class EurekaDiscoveryClientConfiguration implements SmartLifecycle, Order
8387
@Autowired
8488
private EurekaClient eurekaClient;
8589

90+
@Autowired
91+
private InstanceInfo instanceInfo;
92+
8693
@Override
8794
public void start() {
8895
// only set the port if the nonSecurePort is 0 and this.port != 0
8996
if (this.port.get() != 0 && this.instanceConfig.getNonSecurePort() == 0) {
9097
this.instanceConfig.setNonSecurePort(this.port.get());
98+
setInstanceInfoPort();
9199
}
92100

93101
// only initialize if nonSecurePort is greater than 0 and it isn't already running
@@ -114,6 +122,13 @@ public void start() {
114122
}
115123
}
116124

125+
@SneakyThrows
126+
private void setInstanceInfoPort() {
127+
Field port = ReflectionUtils.findField(InstanceInfo.class, "port");
128+
ReflectionUtils.makeAccessible(port);
129+
port.setInt(this.instanceInfo, this.port.get());
130+
}
131+
117132
@Override
118133
public void stop() {
119134
if (this.applicationInfoManager.getInfo() != null) {

0 commit comments

Comments
 (0)