未验证 提交 1b30bd7e 编写于 作者: E elandau 提交者: GitHub

Merge pull request #421 from elandau/bugfix/overrides

config: Fix isLoaded not set in the correct order
...@@ -44,8 +44,6 @@ public class ArchaiusPropertyResolver implements PropertyResolver { ...@@ -44,8 +44,6 @@ public class ArchaiusPropertyResolver implements PropertyResolver {
@Override @Override
public <T> Optional<T> get(String key, Class<T> type) { public <T> Optional<T> get(String key, Class<T> type) {
LOG.debug("Loading property {}", key);
if (Integer.class.equals(type)) { if (Integer.class.equals(type)) {
return Optional.ofNullable((T) config.getInteger(key, null)); return Optional.ofNullable((T) config.getInteger(key, null));
} else if (Boolean.class.equals(type)) { } else if (Boolean.class.equals(type)) {
...@@ -70,7 +68,8 @@ public class ArchaiusPropertyResolver implements PropertyResolver { ...@@ -70,7 +68,8 @@ public class ArchaiusPropertyResolver implements PropertyResolver {
.orElseThrow(() -> new IllegalArgumentException("Unable to convert value to desired type " + type)); .orElseThrow(() -> new IllegalArgumentException("Unable to convert value to desired type " + type));
} }
}); });
} } }
}
@Override @Override
public void forEach(String prefix, BiConsumer<String, String> consumer) { public void forEach(String prefix, BiConsumer<String, String> consumer) {
......
...@@ -59,12 +59,24 @@ public abstract class ReloadableClientConfig implements IClientConfig { ...@@ -59,12 +59,24 @@ public abstract class ReloadableClientConfig implements IClientConfig {
private boolean isLoaded = false; private boolean isLoaded = false;
/**
* @deprecated Use {@link #ReloadableClientConfig(PropertyResolver, String, String)}
*/
@Deprecated
protected ReloadableClientConfig(PropertyResolver resolver) { protected ReloadableClientConfig(PropertyResolver resolver) {
this.clientName = DEFAULT_CLIENT_NAME; this(resolver, DEFAULT_CLIENT_NAME);
this.resolver = resolver;
} }
/**
* @deprecated Use {@link #ReloadableClientConfig(PropertyResolver, String, String)}
*/
@Deprecated
protected ReloadableClientConfig(PropertyResolver resolver, String clientName) { protected ReloadableClientConfig(PropertyResolver resolver, String clientName) {
this(resolver, DEFAULT_NAMESPACE, DEFAULT_CLIENT_NAME);
}
protected ReloadableClientConfig(PropertyResolver resolver, String namespace, String clientName) {
this.namespace = namespace;
this.clientName = clientName; this.clientName = clientName;
this.resolver = resolver; this.resolver = resolver;
} }
...@@ -106,14 +118,11 @@ public abstract class ReloadableClientConfig implements IClientConfig { ...@@ -106,14 +118,11 @@ public abstract class ReloadableClientConfig implements IClientConfig {
@Override @Override
public void loadProperties(String clientName) { public void loadProperties(String clientName) {
Preconditions.checkState(isLoaded == false, "Config '{}' can only be loaded once", clientName); Preconditions.checkState(!isLoaded, "Config '{}' can only be loaded once", clientName);
if (!isLoaded) {
loadDefaultValues();
this.isLoaded = true;
resolver.onChange(this::reload);
}
this.clientName = clientName; this.clientName = clientName;
this.isLoaded = true;
loadDefaultValues();
resolver.onChange(this::reload);
} }
@Override @Override
...@@ -202,8 +211,6 @@ public abstract class ReloadableClientConfig implements IClientConfig { ...@@ -202,8 +211,6 @@ public abstract class ReloadableClientConfig implements IClientConfig {
} }
private <T> ReloadableProperty<T> getClientDynamicProperty(IClientConfigKey<T> key) { private <T> ReloadableProperty<T> getClientDynamicProperty(IClientConfigKey<T> key) {
LOG.debug("Get dynamic property key={} ns={} client={}", key.key(), getNameSpace(), clientName);
return createProperty( return createProperty(
() -> resolveFinalProperty(key), () -> resolveFinalProperty(key),
key::defaultValue); key::defaultValue);
...@@ -295,11 +302,15 @@ public abstract class ReloadableClientConfig implements IClientConfig { ...@@ -295,11 +302,15 @@ public abstract class ReloadableClientConfig implements IClientConfig {
@Override @Override
public final <T> Property<T> getDynamicProperty(IClientConfigKey<T> key) { public final <T> Property<T> getDynamicProperty(IClientConfigKey<T> key) {
LOG.debug("Get dynamic property key={} ns={} client={}", key.key(), getNameSpace(), clientName);
return getClientDynamicProperty(key); return getClientDynamicProperty(key);
} }
@Override @Override
public <T> Property<T> getScopedProperty(IClientConfigKey<T> key) { public <T> Property<T> getScopedProperty(IClientConfigKey<T> key) {
LOG.debug("Get dynamic property key={} ns={} client={}", key.key(), getNameSpace(), clientName);
return (Property<T>) dynamicProperties.computeIfAbsent(key, ignore -> createProperty( return (Property<T>) dynamicProperties.computeIfAbsent(key, ignore -> createProperty(
() -> resolverScopedProperty(key), () -> resolverScopedProperty(key),
key::defaultValue)); key::defaultValue));
...@@ -307,6 +318,8 @@ public abstract class ReloadableClientConfig implements IClientConfig { ...@@ -307,6 +318,8 @@ public abstract class ReloadableClientConfig implements IClientConfig {
@Override @Override
public <T> Property<T> getPrefixMappedProperty(IClientConfigKey<T> key) { public <T> Property<T> getPrefixMappedProperty(IClientConfigKey<T> key) {
LOG.debug("Get dynamic property key={} ns={} client={}", key.key(), getNameSpace(), clientName);
return (Property<T>) dynamicProperties.computeIfAbsent(key, ignore -> createProperty( return (Property<T>) dynamicProperties.computeIfAbsent(key, ignore -> createProperty(
getPrefixedMapPropertySupplier(key), getPrefixedMapPropertySupplier(key),
key::defaultValue)); key::defaultValue));
......
package com.netflix.client.config;
import com.netflix.config.ConfigurationManager;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
public class ReloadableClientConfigTest {
@Rule
public TestName testName = new TestName();
private CommonClientConfigKey<Integer> testKey;
@Before
public void before() {
this.testKey = new CommonClientConfigKey<Integer>(testName.getMethodName(), -1) {};
}
@Test
public void testOverrideLoadedConfig() {
final DefaultClientConfigImpl overrideconfig = new DefaultClientConfigImpl();
overrideconfig.set(testKey, 123);
final DefaultClientConfigImpl config = new DefaultClientConfigImpl();
config.loadDefaultValues();
config.applyOverride(overrideconfig);
Assert.assertEquals(123, config.get(testKey).intValue());
}
@Test
public void setBeforeLoading() {
ConfigurationManager.getConfigInstance().setProperty("ribbon." + testKey.key(), "123");
final DefaultClientConfigImpl config = new DefaultClientConfigImpl();
config.loadProperties("foo");
Assert.assertEquals(123, config.get(testKey).intValue());
}
@Test
public void setAfterLoading() {
final DefaultClientConfigImpl config = new DefaultClientConfigImpl();
config.loadProperties("foo");
config.set(testKey, 456);
ConfigurationManager.getConfigInstance().setProperty("ribbon." + testKey.key(), "123");
Assert.assertEquals(123, config.get(testKey).intValue());
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册