Commit acaf2d20 by Spencer Gibb

Only set ribbon properties if they are not already set.

fixes gh-45
parent d2988b21
......@@ -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;
}
}
......@@ -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());
}
}
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