only set EurekaInstanceConfigBean.nonSecurePort in EurekaClientAutoConfiguration

fixes gh-225
This commit is contained in:
Spencer Gibb
2015-02-23 11:57:32 -07:00
parent 8b81eb8b2c
commit dafb5ecf09
6 changed files with 99 additions and 25 deletions

View File

@@ -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;
}

View File

@@ -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

View File

@@ -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;

View File

@@ -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 {
}
}

View File

@@ -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);
}

View File

@@ -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 {