Commit 9bd3a66b by Andrei Sfat Committed by Dave Syer

#920 add slash when eureka.client.service-url.defaultZone does not contain one

parent 314d229c
......@@ -25,6 +25,7 @@ import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.core.env.PropertyResolver;
import org.springframework.util.StringUtils;
import com.netflix.appinfo.EurekaAccept;
import com.netflix.discovery.EurekaClientConfig;
......@@ -430,13 +431,25 @@ public class EurekaClientConfigBean implements EurekaClientConfig, EurekaConstan
if (serviceUrls == null || serviceUrls.isEmpty()) {
serviceUrls = this.serviceUrl.get(DEFAULT_ZONE);
}
if (serviceUrls != null) {
return Arrays.asList(serviceUrls.split(","));
if (!StringUtils.isEmpty(serviceUrls)) {
final String[] serviceUrlsSplit = serviceUrls.split(",");
List<String> eurekaServiceUrls = new ArrayList<>(serviceUrlsSplit.length);
for (String eurekaServiceUrl : serviceUrlsSplit) {
if (!endsWithSlash(eurekaServiceUrl)) {
eurekaServiceUrl += "/";
}
eurekaServiceUrls.add(eurekaServiceUrl);
}
return eurekaServiceUrls;
}
return new ArrayList<>();
}
private boolean endsWithSlash(String url) {
return url.endsWith("/");
}
@Override
public boolean shouldFilterOnlyUpInstances() {
return this.filterOnlyUpInstances;
......
......@@ -65,9 +65,7 @@ public class EurekaClientConfigBeanTests {
assertEquals("{defaultZone=http://example.com}",
this.context.getBean(EurekaClientConfigBean.class).getServiceUrl()
.toString());
assertEquals("[http://example.com]",
this.context.getBean(EurekaClientConfigBean.class)
.getEurekaServerServiceUrls("defaultZone").toString());
assertEquals("[http://example.com/]", getEurekaServiceUrlsForDefaultZone());
}
@Test
......@@ -76,16 +74,15 @@ public class EurekaClientConfigBeanTests {
this.context.getEnvironment().getPropertySources().addFirst(source);
source.addPropertySource(new MapPropertySource("config", Collections
.<String, Object> singletonMap("eureka.client.serviceUrl.defaultZone",
"http://example.com")));
"http://example.com,http://example2.com")));
this.context.register(PropertyPlaceholderAutoConfiguration.class,
TestConfiguration.class);
this.context.refresh();
assertEquals("{defaultZone=http://example.com}",
assertEquals("{defaultZone=http://example.com,http://example2.com}",
this.context.getBean(EurekaClientConfigBean.class).getServiceUrl()
.toString());
assertEquals("[http://example.com]",
this.context.getBean(EurekaClientConfigBean.class)
.getEurekaServerServiceUrls("defaultZone").toString());
assertEquals("[http://example.com/, http://example2.com/]",
getEurekaServiceUrlsForDefaultZone());
}
@Test
......@@ -95,9 +92,36 @@ public class EurekaClientConfigBeanTests {
this.context.register(PropertyPlaceholderAutoConfiguration.class,
TestConfiguration.class);
this.context.refresh();
assertEquals("[http://example.com]",
this.context.getBean(EurekaClientConfigBean.class)
.getEurekaServerServiceUrls("defaultZone").toString());
assertEquals("[http://example.com/]", getEurekaServiceUrlsForDefaultZone());
}
@Test
public void serviceUrlWithCustomZone() {
EnvironmentTestUtils.addEnvironment(this.context,
"eureka.client.serviceUrl.customZone:http://custom-example.com");
this.context.register(PropertyPlaceholderAutoConfiguration.class,
TestConfiguration.class);
this.context.refresh();
assertEquals("[http://custom-example.com/]", getEurekaServiceUrls("customZone"));
}
@Test
public void serviceUrlWithEmptyServiceUrls() {
EnvironmentTestUtils.addEnvironment(this.context,
"eureka.client.serviceUrl.defaultZone:");
this.context.register(PropertyPlaceholderAutoConfiguration.class,
TestConfiguration.class);
this.context.refresh();
assertEquals("[]", getEurekaServiceUrlsForDefaultZone());
}
private String getEurekaServiceUrlsForDefaultZone() {
return getEurekaServiceUrls("defaultZone");
}
private String getEurekaServiceUrls(String myZone) {
return this.context.getBean(EurekaClientConfigBean.class)
.getEurekaServerServiceUrls(myZone).toString();
}
@Configuration
......
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