Commit 46fa2110 by Spencer Gibb

Use default instanceid for sidecar

fixes gh-847
parent ca81f567
......@@ -13,6 +13,8 @@
<name>Spring Cloud Netflix Eureka Client</name>
<description>Spring Cloud Netflix Eureka Client</description>
<properties>
<!-- Why do I need this now? -->
<maven.javadoc.failOnError>false</maven.javadoc.failOnError>
<main.basedir>${basedir}/..</main.basedir>
</properties>
<dependencies>
......
......@@ -26,10 +26,14 @@ import org.springframework.cloud.netflix.eureka.EurekaInstanceConfigBean;
import org.springframework.cloud.util.InetUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.util.StringUtils;
import com.netflix.appinfo.HealthCheckHandler;
import com.netflix.discovery.EurekaClientConfig;
import static org.springframework.cloud.util.IdUtils.getDefaultInstanceId;
/**
* @author Spencer Gibb
*/
......@@ -57,17 +61,24 @@ public class SidecarConfiguration {
@Autowired
private InetUtils inetUtils;
@Value("${server.port:${SERVER_PORT:${PORT:8080}}}")
private int serverPort = 8080;
@Value("${management.port:${MANAGEMENT_PORT:${PORT:8080}}}")
private int managementPort = 8080;
@Value("${eureka.instance.hostname:${EUREKA_INSTANCE_HOSTNAME:}}")
String hostname;
@Autowired
ConfigurableEnvironment env;
@Bean
public EurekaInstanceConfigBean eurekaInstanceConfigBean() {
EurekaInstanceConfigBean config = new EurekaInstanceConfigBean(inetUtils);
int port = this.sidecarProperties.getPort();
config.setNonSecurePort(port);
config.setInstanceId(getDefaultInstanceId(this.env));
if (StringUtils.hasText(this.hostname)) {
config.setHostname(this.hostname);
}
String scheme = config.getSecurePortEnabled() ? "https" : "http";
config.setStatusPageUrl(scheme + "://" + config.getHostname() + ":"
+ this.managementPort + config.getStatusPageUrlPath());
......
......@@ -18,19 +18,30 @@ package org.springframework.cloud.netflix.sidecar;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.IntegrationTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.WebIntegrationTest;
import org.springframework.cloud.netflix.eureka.EurekaInstanceConfigBean;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = SidecarApplication.class)
@IntegrationTest("server.port=0")
@WebAppConfiguration
@WebIntegrationTest(randomPort = true, value = {"spring.application.name=mytest", "spring.cloud.client.hostname=mhhost", "spring.application.instance_id=1", "eureka.instance.hostname=mhhost", "sidecar.port=7000"})
public class SidecarApplicationTests {
@Autowired
EurekaInstanceConfigBean config;
@Test
public void contextLoads() {
public void testEurekaConfigBean() {
assertThat(this.config.getAppname(), equalTo("mytest"));
assertThat(this.config.getHostname(), equalTo("mhhost"));
assertThat(this.config.getInstanceId(), equalTo("mhhost:mytest:1"));
assertThat(this.config.getNonSecurePort(), equalTo(7000));
System.out.println();
}
}
......@@ -11,6 +11,7 @@ sidecar:
eureka:
instance:
app-group-name: mysidecargroup
hostname: mysidecarhost
client:
serviceUrl:
defaultZone: http://user:password@localhost:8761/eureka/
......
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