remove reflection to set InstanceInfo.port
This commit is contained in:
@@ -104,7 +104,7 @@ public class EurekaClientAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(InstanceInfo.class)
|
||||
public InstanceInfo instanceInfo(EurekaInstanceConfig config) {
|
||||
public MutableInstanceInfo instanceInfo(EurekaInstanceConfig config) {
|
||||
return new InstanceInfoFactory().create(config);
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
@@ -43,12 +41,10 @@ import org.springframework.context.annotation.Configuration;
|
||||
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;
|
||||
@@ -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
|
||||
@@ -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
|
||||
public void stop() {
|
||||
if (this.applicationInfoManager.getInfo() != null) {
|
||||
|
||||
@@ -31,7 +31,7 @@ import com.netflix.appinfo.LeaseInfo;
|
||||
@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());
|
||||
@@ -87,6 +87,6 @@ public class InstanceInfoFactory {
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user