Skip to content

Commit

Permalink
remove reflection to set InstanceInfo.port
Browse files Browse the repository at this point in the history
  • Loading branch information
spencergibb committed Oct 9, 2015
1 parent 51ea8af commit fe98953
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 16 deletions.
Expand Up @@ -104,7 +104,7 @@ public ApplicationInfoManager applicationInfoManager(EurekaInstanceConfig config

@Bean
@ConditionalOnMissingBean(InstanceInfo.class)
public InstanceInfo instanceInfo(EurekaInstanceConfig config) {
public MutableInstanceInfo instanceInfo(EurekaInstanceConfig config) {
return new InstanceInfoFactory().create(config);
}

Expand Down
Expand Up @@ -16,11 +16,9 @@

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;
Expand All @@ -43,12 +41,10 @@
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.core.Ordered;
import org.springframework.util.ReflectionUtils;

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;
Expand Down Expand Up @@ -88,14 +84,14 @@ public class EurekaDiscoveryClientConfiguration implements SmartLifecycle, Order
private EurekaClient eurekaClient;

@Autowired
private InstanceInfo instanceInfo;
private MutableInstanceInfo 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());
setInstanceInfoPort();
instanceInfo.setPort(this.port.get());
}

// only initialize if nonSecurePort is greater than 0 and it isn't already running
Expand All @@ -122,13 +118,6 @@ public void start() {
}
}

@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) {
Expand Down
Expand Up @@ -31,7 +31,7 @@
@CommonsLog
public class InstanceInfoFactory {

public InstanceInfo create(EurekaInstanceConfig config) {
public MutableInstanceInfo create(EurekaInstanceConfig config) {
LeaseInfo.Builder leaseInfoBuilder = LeaseInfo.Builder.newBuilder()
.setRenewalIntervalInSecs(config.getLeaseRenewalIntervalInSeconds())
.setDurationInSecs(config.getLeaseExpirationDurationInSeconds());
Expand Down Expand Up @@ -87,6 +87,6 @@ public InstanceInfo create(EurekaInstanceConfig config) {

InstanceInfo instanceInfo = builder.build();
instanceInfo.setLeaseInfo(leaseInfoBuilder.build());
return instanceInfo;
return new MutableInstanceInfo(instanceInfo);
}
}
@@ -0,0 +1,44 @@
/*
* 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;
}

}

0 comments on commit fe98953

Please sign in to comment.