Browse Source

- Eureka client auto configures the secure port when secure port is enabled

- The eureka instance auto registration also changes the secure port when the server port is random (0)

Fixes gh-2303
pull/6/head
Raiyan 7 years ago committed by Spencer Gibb
parent
commit
b57dedb74a
No known key found for this signature in database
GPG Key ID: 7788A47380690861
  1. 1
      spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/CloudEurekaInstanceConfig.java
  2. 9
      spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/EurekaClientAutoConfiguration.java
  3. 13
      spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/serviceregistry/EurekaAutoServiceRegistration.java
  4. 8
      spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/serviceregistry/EurekaRegistration.java
  5. 24
      spring-cloud-netflix-eureka-client/src/test/java/org/springframework/cloud/netflix/eureka/EurekaClientAutoConfigurationTests.java

1
spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/CloudEurekaInstanceConfig.java

@ -24,5 +24,6 @@ import com.netflix.appinfo.InstanceInfo; @@ -24,5 +24,6 @@ import com.netflix.appinfo.InstanceInfo;
*/
public interface CloudEurekaInstanceConfig extends EurekaInstanceConfig {
void setNonSecurePort(int port);
void setSecurePort(int securePort);
InstanceInfo.InstanceStatus getInitialStatus();
}

9
spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/EurekaClientAutoConfiguration.java

@ -126,13 +126,21 @@ public class EurekaClientAutoConfiguration { @@ -126,13 +126,21 @@ public class EurekaClientAutoConfiguration {
String hostname = eurekaPropertyResolver.getProperty("hostname");
boolean preferIpAddress = Boolean.parseBoolean(eurekaPropertyResolver.getProperty("preferIpAddress"));
boolean isSecurePortEnabled = Boolean.parseBoolean(eurekaPropertyResolver.getProperty("securePortEnabled"));
int nonSecurePort = Integer.valueOf(propertyResolver.getProperty("server.port", propertyResolver.getProperty("port", "8080")));
int managementPort = Integer.valueOf(propertyResolver.getProperty("management.port", String.valueOf(nonSecurePort)));
String managementContextPath = propertyResolver.getProperty("management.contextPath", propertyResolver.getProperty("server.contextPath", "/"));
EurekaInstanceConfigBean instance = new EurekaInstanceConfigBean(inetUtils);
instance.setNonSecurePort(nonSecurePort);
instance.setInstanceId(getDefaultInstanceId(propertyResolver));
instance.setPreferIpAddress(preferIpAddress);
if(isSecurePortEnabled) {
int securePort = Integer.valueOf(propertyResolver.getProperty("server.port", propertyResolver.getProperty("port", "8080")));
instance.setSecurePort(securePort);
}
if (managementPort != nonSecurePort && managementPort != 0) {
if (StringUtils.hasText(hostname)) {
instance.setHostname(hostname);
@ -148,6 +156,7 @@ public class EurekaClientAutoConfiguration { @@ -148,6 +156,7 @@ public class EurekaClientAutoConfiguration {
if (StringUtils.hasText(healthCheckUrlPath)) {
instance.setHealthCheckUrlPath(healthCheckUrlPath);
}
String scheme = instance.getSecurePortEnabled() ? "https" : "http";
URL base = new URL(scheme, instance.getHostname(), managementPort, managementContextPath);
instance.setStatusPageUrl(new URL(base, StringUtils.trimLeadingCharacter(instance.getStatusPageUrlPath(), '/')).toString());

13
spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/serviceregistry/EurekaAutoServiceRegistration.java

@ -36,6 +36,7 @@ import org.springframework.core.Ordered; @@ -36,6 +36,7 @@ import org.springframework.core.Ordered;
* @author Spencer Gibb
* @author Jon Schneider
* @author Jakub Narloch
* @author raiyan
*/
public class EurekaAutoServiceRegistration implements AutoServiceRegistration, SmartLifecycle, Ordered {
@ -61,9 +62,15 @@ public class EurekaAutoServiceRegistration implements AutoServiceRegistration, S @@ -61,9 +62,15 @@ public class EurekaAutoServiceRegistration implements AutoServiceRegistration, S
@Override
public void start() {
// only set the port if the nonSecurePort is 0 and this.port != 0
if (this.port.get() != 0 && this.registration.getNonSecurePort() == 0) {
this.registration.setNonSecurePort(this.port.get());
// only set the port if the nonSecurePort or securePort is 0 and this.port != 0
if (this.port.get() != 0) {
if (this.registration.getNonSecurePort() == 0) {
this.registration.setNonSecurePort(this.port.get());
}
if (this.registration.getSecurePort() == 0 && this.registration.isSecure()) {
this.registration.setSecurePort(this.port.get());
}
}
// only initialize if nonSecurePort is greater than 0 and it isn't already running

8
spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/serviceregistry/EurekaRegistration.java

@ -193,6 +193,14 @@ public class EurekaRegistration implements Registration, Closeable { @@ -193,6 +193,14 @@ public class EurekaRegistration implements Registration, Closeable {
return this.instanceConfig.getNonSecurePort();
}
public void setSecurePort(int port) {
this.instanceConfig.setSecurePort(port);
}
public int getSecurePort() {
return this.instanceConfig.getSecurePort();
}
@Override
public void close() throws IOException {
this.eurekaClient.shutdown();

24
spring-cloud-netflix-eureka-client/src/test/java/org/springframework/cloud/netflix/eureka/EurekaClientAutoConfigurationTests.java

@ -91,6 +91,23 @@ public class EurekaClientAutoConfigurationTests { @@ -91,6 +91,23 @@ public class EurekaClientAutoConfigurationTests {
this.context.getBeanDefinition("eurekaClient").getFactoryMethodName());
}
@Test
public void securePortPeriods() {
testSecurePort("server.port");
}
@Test
public void securePortUnderscores() {
testSecurePort("SERVER_PORT");
}
@Test
public void securePort() {
testSecurePort("PORT");
assertEquals("eurekaClient",
this.context.getBeanDefinition("eurekaClient").getFactoryMethodName());
}
@Test
public void managementPort() {
EnvironmentTestUtils.addEnvironment(this.context, "server.port=8989",
@ -331,6 +348,13 @@ public class EurekaClientAutoConfigurationTests { @@ -331,6 +348,13 @@ public class EurekaClientAutoConfigurationTests {
assertEquals(8888, getInstanceConfig().getNonSecurePort());
}
private void testSecurePort(String propName) {
EnvironmentTestUtils.addEnvironment(this.context, "eureka.instance.securePortEnabled=true");
addEnvironment(this.context, propName + ":8443");
setupContext();
assertEquals(8443, getInstanceConfig().getSecurePort());
}
private EurekaInstanceConfigBean getInstanceConfig() {
return this.context.getBean(EurekaInstanceConfigBean.class);
}

Loading…
Cancel
Save