Commit 46fa2110 by Spencer Gibb

Use default instanceid for sidecar

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