From ddc1069e86e57b3ff3e631780d59803e059bd3c1 Mon Sep 17 00:00:00 2001 From: Spencer Gibb Date: Wed, 19 Oct 2016 12:36:15 -0600 Subject: [PATCH] Move properties to new namespace. Moving eureka.server.{expectedNumberOfRenewsPerMin,defaultOpenForTrafficCount} to eureka.instance.registry so relaxed binding can be used. Default to old values for backwards compatibility. --- .../server/EurekaServerConfiguration.java | 19 ++---- .../eureka/server/InstanceRegistry.java | 4 +- .../server/InstanceRegistryProperties.java | 63 +++++++++++++++++++ 3 files changed, 71 insertions(+), 15 deletions(-) create mode 100644 spring-cloud-netflix-eureka-server/src/main/java/org/springframework/cloud/netflix/eureka/server/InstanceRegistryProperties.java diff --git a/spring-cloud-netflix-eureka-server/src/main/java/org/springframework/cloud/netflix/eureka/server/EurekaServerConfiguration.java b/spring-cloud-netflix-eureka-server/src/main/java/org/springframework/cloud/netflix/eureka/server/EurekaServerConfiguration.java index 0e2f81ed..e5fae161 100644 --- a/spring-cloud-netflix-eureka-server/src/main/java/org/springframework/cloud/netflix/eureka/server/EurekaServerConfiguration.java +++ b/spring-cloud-netflix-eureka-server/src/main/java/org/springframework/cloud/netflix/eureka/server/EurekaServerConfiguration.java @@ -71,7 +71,7 @@ import com.sun.jersey.spi.container.servlet.ServletContainer; @Configuration @Import(EurekaServerInitializerConfiguration.class) @EnableDiscoveryClient -@EnableConfigurationProperties(EurekaDashboardProperties.class) +@EnableConfigurationProperties({ EurekaDashboardProperties.class, InstanceRegistryProperties.class }) @PropertySource("classpath:/eureka/server.properties") public class EurekaServerConfiguration extends WebMvcConfigurerAdapter { /** @@ -92,17 +92,9 @@ public class EurekaServerConfiguration extends WebMvcConfigurerAdapter { @Autowired private EurekaClient eurekaClient; - /* - * Setting expectedNumberOfRenewsPerMin to non-zero to ensure that even an isolated - * server can adjust its eviction policy to the number of registrations (when it's - * zero, even a successful registration won't reset the rate threshold in - * InstanceRegistry.register()). - */ - @Value("${eureka.server.expectedNumberOfRenewsPerMin:1}") - private int expectedNumberOfRenewsPerMin; + @Autowired + private InstanceRegistryProperties instanceRegistryProperties; - @Value("${eureka.server.defaultOpenForTrafficCount:1}") - private int defaultOpenForTrafficCount; public static final CloudJacksonJson JACKSON_JSON = new CloudJacksonJson(); @Bean @@ -166,8 +158,9 @@ public class EurekaServerConfiguration extends WebMvcConfigurerAdapter { ServerCodecs serverCodecs) { this.eurekaClient.getApplications(); // force initialization return new InstanceRegistry(this.eurekaServerConfig, this.eurekaClientConfig, - serverCodecs, this.eurekaClient, this.expectedNumberOfRenewsPerMin, - this.defaultOpenForTrafficCount); + serverCodecs, this.eurekaClient, + this.instanceRegistryProperties.getExpectedNumberOfRenewsPerMin(), + this.instanceRegistryProperties.getDefaultOpenForTrafficCount()); } @Bean diff --git a/spring-cloud-netflix-eureka-server/src/main/java/org/springframework/cloud/netflix/eureka/server/InstanceRegistry.java b/spring-cloud-netflix-eureka-server/src/main/java/org/springframework/cloud/netflix/eureka/server/InstanceRegistry.java index c1ba8b25..3faaef7e 100644 --- a/spring-cloud-netflix-eureka-server/src/main/java/org/springframework/cloud/netflix/eureka/server/InstanceRegistry.java +++ b/spring-cloud-netflix-eureka-server/src/main/java/org/springframework/cloud/netflix/eureka/server/InstanceRegistry.java @@ -66,8 +66,8 @@ public class InstanceRegistry extends PeerAwareInstanceRegistryImpl /** * If * {@link PeerAwareInstanceRegistryImpl#openForTraffic(ApplicationInfoManager, int)} - * is called with a zero * argument, it means that leases are not automatically * - * cancelled if the instance * hasn't sent any renewals recently. This happens for a + * is called with a zero argument, it means that leases are not automatically + * cancelled if the instance hasn't sent any renewals recently. This happens for a * standalone server. It seems like a bad default, so we set it to the smallest * non-zero value we can, so that any instances that subsequently register can bump up * the threshold. diff --git a/spring-cloud-netflix-eureka-server/src/main/java/org/springframework/cloud/netflix/eureka/server/InstanceRegistryProperties.java b/spring-cloud-netflix-eureka-server/src/main/java/org/springframework/cloud/netflix/eureka/server/InstanceRegistryProperties.java new file mode 100644 index 00000000..87a0ebac --- /dev/null +++ b/spring-cloud-netflix-eureka-server/src/main/java/org/springframework/cloud/netflix/eureka/server/InstanceRegistryProperties.java @@ -0,0 +1,63 @@ +/* + * Copyright 2013-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.springframework.cloud.netflix.eureka.server; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.context.properties.ConfigurationProperties; + +import static org.springframework.cloud.netflix.eureka.server.InstanceRegistryProperties.PREFIX; + +/** + * @author Spencer Gibb + */ +@ConfigurationProperties(PREFIX) +public class InstanceRegistryProperties { + + public static final String PREFIX = "eureka.instance.registry"; + + + /* Default number of expected renews per minute, defaults to 1. + * Setting expectedNumberOfRenewsPerMin to non-zero to ensure that even an isolated + * server can adjust its eviction policy to the number of registrations (when it's + * zero, even a successful registration won't reset the rate threshold in + * InstanceRegistry.register()). + */ + @Value("${eureka.server.expectedNumberOfRenewsPerMin:1}") // for backwards compatibility + private int expectedNumberOfRenewsPerMin = 1; + + /** Value used in determining when leases are cancelled, default to 1 for standalone. + * Should be set to 0 for peer replicated eurekas */ + @Value("${eureka.server.defaultOpenForTrafficCount:1}") // for backwards compatibility + private int defaultOpenForTrafficCount = 1; + + public int getExpectedNumberOfRenewsPerMin() { + return expectedNumberOfRenewsPerMin; + } + + public void setExpectedNumberOfRenewsPerMin(int expectedNumberOfRenewsPerMin) { + this.expectedNumberOfRenewsPerMin = expectedNumberOfRenewsPerMin; + } + + public int getDefaultOpenForTrafficCount() { + return defaultOpenForTrafficCount; + } + + public void setDefaultOpenForTrafficCount(int defaultOpenForTrafficCount) { + this.defaultOpenForTrafficCount = defaultOpenForTrafficCount; + } +}