only set EurekaInstanceConfigBean.nonSecurePort in EurekaClientAutoConfiguration
fixes gh-225
This commit is contained in:
@@ -22,6 +22,7 @@ import java.util.concurrent.ConcurrentMap;
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
@@ -57,6 +58,9 @@ public class EurekaClientAutoConfiguration implements ApplicationListener<Parent
|
||||
|
||||
private static final ConcurrentMap<String, String> listenerAdded = new ConcurrentHashMap<>();
|
||||
|
||||
@Value("${server.port:${SERVER_PORT:${PORT:8080}}}")
|
||||
int nonSecurePort;
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
XmlXStream.getInstance().setMarshallingStrategy(
|
||||
@@ -74,7 +78,9 @@ public class EurekaClientAutoConfiguration implements ApplicationListener<Parent
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(EurekaInstanceConfig.class)
|
||||
public EurekaInstanceConfigBean eurekaInstanceConfigBean() {
|
||||
return new EurekaInstanceConfigBean();
|
||||
EurekaInstanceConfigBean instance = new EurekaInstanceConfigBean();
|
||||
instance.setNonSecurePort(this.nonSecurePort);
|
||||
return instance;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -30,6 +30,8 @@ import com.netflix.appinfo.InstanceInfo;
|
||||
import com.netflix.discovery.shared.Application;
|
||||
import com.netflix.discovery.shared.Applications;
|
||||
|
||||
import static com.netflix.appinfo.InstanceInfo.PortType.*;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
@@ -108,12 +110,16 @@ public class EurekaDiscoveryClient implements DiscoveryClient {
|
||||
|
||||
@Override
|
||||
public int getPort() {
|
||||
return this.instance.getPort();
|
||||
// assume if unsecure is enabled, that is the default
|
||||
if (this.instance.isPortEnabled(UNSECURE) || !this.instance.isPortEnabled(SECURE)) {
|
||||
return this.instance.getPort();
|
||||
}
|
||||
return this.instance.getSecurePort();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSecure() {
|
||||
return this.instance.isPortEnabled(InstanceInfo.PortType.SECURE);
|
||||
return this.instance.isPortEnabled(SECURE);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -56,7 +56,6 @@ public class EurekaInstanceConfigBean implements EurekaInstanceConfig {
|
||||
|
||||
private boolean instanceEnabledOnit;
|
||||
|
||||
@Value("${server.port:${SERVER_PORT:${PORT:8080}}}")
|
||||
private int nonSecurePort = 80;
|
||||
|
||||
private int securePort = 443;
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* 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 org.junit.After;
|
||||
import org.junit.Test;
|
||||
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.springframework.boot.test.EnvironmentTestUtils.addEnvironment;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
public class EurekaClientAutoConfigurationTests {
|
||||
private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
||||
|
||||
@After
|
||||
public void init() {
|
||||
if (this.context != null) {
|
||||
this.context.close();
|
||||
}
|
||||
}
|
||||
|
||||
private void setupContext() {
|
||||
this.context.register(PropertyPlaceholderAutoConfiguration.class,
|
||||
TestConfiguration.class);
|
||||
this.context.refresh();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nonSecurePort2() {
|
||||
testNonSecurePort("server.port");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nonSecurePort3() {
|
||||
testNonSecurePort("SERVER_PORT");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nonSecurePort4() {
|
||||
testNonSecurePort("PORT");
|
||||
}
|
||||
|
||||
private void testNonSecurePort(String propName) {
|
||||
addEnvironment(this.context, propName + ":8888");
|
||||
setupContext();
|
||||
assertEquals(8888, getInstanceConfig().getNonSecurePort());
|
||||
}
|
||||
|
||||
private EurekaInstanceConfigBean getInstanceConfig() {
|
||||
return this.context.getBean(EurekaInstanceConfigBean.class);
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableConfigurationProperties
|
||||
@Import(EurekaClientAutoConfiguration.class)
|
||||
protected static class TestConfiguration {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -62,26 +62,7 @@ public class EurekaInstanceConfigBeanTests {
|
||||
|
||||
@Test
|
||||
public void nonSecurePort() {
|
||||
testNonSecurePort("eureka.instance.nonSecurePort");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nonSecurePort2() {
|
||||
testNonSecurePort("server.port");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nonSecurePort3() {
|
||||
testNonSecurePort("SERVER_PORT");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nonSecurePort4() {
|
||||
testNonSecurePort("PORT");
|
||||
}
|
||||
|
||||
private void testNonSecurePort(String propName) {
|
||||
addEnvironment(this.context, propName + ":8888");
|
||||
addEnvironment(this.context, "eureka.instance.nonSecurePort:8888");
|
||||
setupContext();
|
||||
assertEquals(8888, getInstanceConfig().getNonSecurePort());
|
||||
}
|
||||
@@ -133,7 +114,7 @@ public class EurekaInstanceConfigBeanTests {
|
||||
this.context.refresh();
|
||||
}
|
||||
|
||||
protected EurekaInstanceConfigBean getInstanceConfig() {
|
||||
private EurekaInstanceConfigBean getInstanceConfig() {
|
||||
return this.context.getBean(EurekaInstanceConfigBean.class);
|
||||
}
|
||||
|
||||
|
||||
@@ -19,11 +19,13 @@ package org.springframework.cloud.netflix.sidecar;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.actuate.metrics.repository.InMemoryMetricRepository;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableSidecar
|
||||
@EnableDiscoveryClient
|
||||
@RestController
|
||||
public class SidecarApplication {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user