diff --git a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/ribbon/eureka/EurekaRibbonClientPreprocessor.java b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/ribbon/eureka/EurekaRibbonClientPreprocessor.java index a12b6eef..e8dc3914 100644 --- a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/ribbon/eureka/EurekaRibbonClientPreprocessor.java +++ b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/ribbon/eureka/EurekaRibbonClientPreprocessor.java @@ -6,6 +6,8 @@ import static com.netflix.client.config.CommonClientConfigKey.NFLoadBalancerRule import static com.netflix.client.config.CommonClientConfigKey.NIWSServerListClassName; import static com.netflix.client.config.CommonClientConfigKey.NIWSServerListFilterClassName; +import com.netflix.config.DynamicPropertyFactory; +import com.netflix.config.DynamicStringProperty; import org.springframework.cloud.netflix.ribbon.RibbonClientPreprocessor; import org.springframework.cloud.netflix.ribbon.SpringClientFactory; @@ -29,7 +31,10 @@ import com.netflix.niws.loadbalancer.DiscoveryEnabledNIWSServerList; */ public class EurekaRibbonClientPreprocessor implements RibbonClientPreprocessor { - private EurekaClientConfig clientConfig; + protected static final String VALUE_NOT_SET = "__not__set__"; + protected static final String DEFAULT_NAMESPACE = "ribbon"; + + private EurekaClientConfig clientConfig; private SpringClientFactory clientFactory; public EurekaRibbonClientPreprocessor(EurekaClientConfig clientConfig, SpringClientFactory clientFactory) { @@ -83,9 +88,19 @@ public class EurekaRibbonClientPreprocessor implements RibbonClientPreprocessor protected void setProp(String serviceId, String suffix, String value) { // how to set the namespace properly? - String namespace = "ribbon"; - ConfigurationManager.getConfigInstance().setProperty( - serviceId + "." + namespace + "." + suffix, value); + String key = getKey(serviceId, suffix); + DynamicStringProperty property = getProperty(key); + if (property.get().equals(VALUE_NOT_SET)) { + ConfigurationManager.getConfigInstance().setProperty(key, value); + } } + protected DynamicStringProperty getProperty(String key) { + return DynamicPropertyFactory.getInstance().getStringProperty(key, VALUE_NOT_SET); + } + + protected String getKey(String serviceId, String suffix) { + return serviceId + "." + DEFAULT_NAMESPACE + "." + suffix; + } + } diff --git a/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/ribbon/eureka/EurekaRibbonClientPreprocessorTests.java b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/ribbon/eureka/EurekaRibbonClientPreprocessorTests.java index 42388519..d4345fb1 100644 --- a/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/ribbon/eureka/EurekaRibbonClientPreprocessorTests.java +++ b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/ribbon/eureka/EurekaRibbonClientPreprocessorTests.java @@ -18,7 +18,9 @@ package org.springframework.cloud.netflix.ribbon.eureka; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; +import static org.springframework.cloud.netflix.ribbon.eureka.EurekaRibbonClientPreprocessor.*; +import com.netflix.config.DynamicStringProperty; import org.junit.After; import org.junit.Test; import org.springframework.cloud.netflix.ribbon.SpringClientFactory; @@ -57,4 +59,28 @@ public class EurekaRibbonClientPreprocessorTests { assertEquals("foo", ConfigurationManager.getDeploymentContext().getValue(ContextKey.zone)); } + @Test + public void testSetProp() { + EurekaClientConfigBean client = new EurekaClientConfigBean(); + SpringClientFactory clientFactory = new SpringClientFactory(); + EurekaRibbonClientPreprocessor preprocessor = new EurekaRibbonClientPreprocessor( + client, clientFactory); + + String serviceId = "myService"; + String suffix = "mySuffix"; + String value = "myValue"; + + DynamicStringProperty property = preprocessor.getProperty(preprocessor.getKey(serviceId, suffix)); + + assertEquals("property doesn't have default value", VALUE_NOT_SET, property.get()); + + preprocessor.setProp(serviceId, suffix, value); + + assertEquals("property has wrong value", value, property.get()); + + preprocessor.setProp(serviceId, suffix, value); + + assertEquals("property has wrong value", value, property.get()); + } + }