Commit fccbe1af by Thibaud Lepretre Committed by Spencer Gibb

Create `sidecar.hostname` and `sidecar.ipAddress` (#1561)

In addition to `eureka.instance.hostname` property you can now defined `sidecar.hostname` in order to set different hostname from application and its sidecar. However if `eureka.instance.hostname` is present it will override `sidecar.hostname`. Fixes #981
parent fa45a9f3
......@@ -20,6 +20,7 @@ import static org.springframework.cloud.commons.util.IdUtils.getDefaultInstanceI
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.bind.RelaxedPropertyResolver;
......@@ -35,9 +36,25 @@ import org.springframework.util.StringUtils;
import com.netflix.appinfo.HealthCheckHandler;
import com.netflix.discovery.EurekaClientConfig;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* Sidecar Configuration that setting up {@link com.netflix.appinfo.EurekaInstanceConfig}.
* <p>
* Depends on {@link SidecarProperties} and {@code eureka.instance.hostname} property. Since there is two way to
* configure hostname:
* <ol>
* <li>{@code eureka.instance.hostname} property</li>
* <li>{@link SidecarProperties#hostname}</li>
* </ol>
* {@code eureka.instance.hostname} will always win against {@link SidecarProperties#hostname} due to
* {@code @ConfigurationProperties("eureka.instance")} on {@link EurekaInstanceConfigBeanConfiguration}.
*
* @author Spencer Gibb
* @author Ryan Baxter
*
* @see EurekaInstanceConfigBeanConfiguration
*/
@Configuration
@EnableConfigurationProperties
......@@ -80,13 +97,21 @@ public class SidecarConfiguration {
int port = this.sidecarProperties.getPort();
config.setNonSecurePort(port);
config.setInstanceId(getDefaultInstanceId(this.env));
if(StringUtils.hasText(springAppName)) {
if (StringUtils.hasText(springAppName)) {
config.setAppname(springAppName);
config.setVirtualHostName(springAppName);
config.setSecureVirtualHostName(springAppName);
}
if (StringUtils.hasText(this.hostname)) {
config.setHostname(this.hostname);
String hostname = this.sidecarProperties.getHostname();
String ipAddress = this.sidecarProperties.getIpAddress();
if (!StringUtils.hasText(hostname) && StringUtils.hasText(this.hostname)) {
hostname = this.hostname;
}
if (StringUtils.hasText(hostname)) {
config.setHostname(hostname);
}
if (StringUtils.hasText(ipAddress)) {
config.setIpAddress(ipAddress);
}
String scheme = config.getSecurePortEnabled() ? "https" : "http";
config.setStatusPageUrl(scheme + "://" + config.getHostname() + ":"
......
......@@ -40,4 +40,8 @@ public class SidecarProperties {
@Min(1)
private int port;
private String hostname;
private String ipAddress;
}
......@@ -27,22 +27,57 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = SidecarApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT, 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;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = SidecarApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT, value = {
"spring.application.name=mytest", "spring.cloud.client.hostname=mhhost", "spring.application.instance_id=1",
"eureka.instance.hostname=mhhost", "sidecar.port=7000", "sidecar.ipAddress=127.0.0.1" })
public static class EurekaTestConfigBeanTest {
@Autowired
EurekaInstanceConfigBean config;
@Test
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));
@Test
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));
}
}
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = SidecarApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT, value = {
"spring.application.name=mytest", "spring.cloud.client.hostname=mhhost", "spring.application.instance_id=1",
"sidecar.hostname=mhhost", "sidecar.port=7000", "sidecar.ipAddress=127.0.0.1" })
public static class NewPropertyEurekaTestConfigBeanTest {
@Autowired
EurekaInstanceConfigBean config;
@Test
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));
}
}
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = SidecarApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT, value = {
"spring.application.name=mytest", "spring.cloud.client.hostname=mhhost", "spring.application.instance_id=1",
"eureka.instance.hostname=mhhost1", "sidecar.hostname=mhhost2", "sidecar.port=7000", "sidecar.ipAddress=127.0.0.1" })
public static class BothPropertiesEurekaTestConfigBeanTest {
@Autowired
EurekaInstanceConfigBean config;
@Test
public void testEurekaConfigBeanEurekaInstanceHostnamePropertyShouldBeUsed() {
assertThat(this.config.getAppname(), equalTo("mytest"));
assertThat(this.config.getHostname(), equalTo("mhhost1"));
assertThat(this.config.getInstanceId(), equalTo("mhhost:mytest:1"));
assertThat(this.config.getNonSecurePort(), equalTo(7000));
}
}
}
......@@ -11,7 +11,6 @@ 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