Commit fe989530 by Spencer Gibb

remove reflection to set InstanceInfo.port

parent 51ea8afb
...@@ -104,7 +104,7 @@ public class EurekaClientAutoConfiguration { ...@@ -104,7 +104,7 @@ public class EurekaClientAutoConfiguration {
@Bean @Bean
@ConditionalOnMissingBean(InstanceInfo.class) @ConditionalOnMissingBean(InstanceInfo.class)
public InstanceInfo instanceInfo(EurekaInstanceConfig config) { public MutableInstanceInfo instanceInfo(EurekaInstanceConfig config) {
return new InstanceInfoFactory().create(config); return new InstanceInfoFactory().create(config);
} }
......
...@@ -16,11 +16,9 @@ ...@@ -16,11 +16,9 @@
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;
...@@ -43,12 +41,10 @@ import org.springframework.context.annotation.Configuration; ...@@ -43,12 +41,10 @@ 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;
...@@ -88,14 +84,14 @@ public class EurekaDiscoveryClientConfiguration implements SmartLifecycle, Order ...@@ -88,14 +84,14 @@ public class EurekaDiscoveryClientConfiguration implements SmartLifecycle, Order
private EurekaClient eurekaClient; private EurekaClient eurekaClient;
@Autowired @Autowired
private InstanceInfo instanceInfo; private MutableInstanceInfo 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(); instanceInfo.setPort(this.port.get());
} }
// 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
...@@ -122,13 +118,6 @@ public class EurekaDiscoveryClientConfiguration implements SmartLifecycle, Order ...@@ -122,13 +118,6 @@ 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 InstanceInfo create(EurekaInstanceConfig config) { public MutableInstanceInfo 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 instanceInfo; return new MutableInstanceInfo(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;
}
}
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