未验证 提交 077f78fe 编写于 作者: E elandau 提交者: GitHub

Merge pull request #417 from elandau/feature/scoped_property

config: Dynamic properties that are only scoped to the client (no default)
...@@ -155,6 +155,13 @@ public interface IClientConfig { ...@@ -155,6 +155,13 @@ public interface IClientConfig {
*/ */
<T> Property<T> getDynamicProperty(IClientConfigKey<T> key); <T> Property<T> getDynamicProperty(IClientConfigKey<T> key);
/**
* @return Get a property that is only scoped to this client.
*/
default <T> Property<T> getScopedProperty(IClientConfigKey<T> key) {
throw new UnsupportedOperationException();
}
/** /**
* @return Return a dynamically updated property that is a mapping of all properties prefixed by the key name to an * @return Return a dynamically updated property that is a mapping of all properties prefixed by the key name to an
* object with static method valueOf(Map{@literal <}String, String{@literal >}) * object with static method valueOf(Map{@literal <}String, String{@literal >})
......
...@@ -198,10 +198,10 @@ public abstract class ReloadableClientConfig implements IClientConfig { ...@@ -198,10 +198,10 @@ public abstract class ReloadableClientConfig implements IClientConfig {
public final <T> Property<T> getGlobalProperty(IClientConfigKey<T> key) { public final <T> Property<T> getGlobalProperty(IClientConfigKey<T> key) {
LOG.debug("Get global property {} default {}", key.key(), key.defaultValue()); LOG.debug("Get global property {} default {}", key.key(), key.defaultValue());
return createProperty( return (Property<T>) dynamicProperties.computeIfAbsent(key, ignore -> createProperty(
() -> resolver.get(key.key(), key.type()), () -> resolver.get(key.key(), key.type()),
key::defaultValue, key::defaultValue,
true); true));
} }
interface ReloadableProperty<T> extends Property<T> { interface ReloadableProperty<T> extends Property<T> {
...@@ -241,12 +241,16 @@ public abstract class ReloadableClientConfig implements IClientConfig { ...@@ -241,12 +241,16 @@ public abstract class ReloadableClientConfig implements IClientConfig {
return value; return value;
} }
value = getIfSet(key); return getIfSet(key);
}
private <T> Optional<T> resolverScopedProperty(IClientConfigKey<T> key) {
Optional<T> value = resolver.get(clientName + "." + getNameSpace() + "." + key.key(), key.type());
if (value.isPresent()) { if (value.isPresent()) {
return value; return value;
} }
return Optional.empty(); return getIfSet(key);
} }
@Override @Override
...@@ -296,12 +300,20 @@ public abstract class ReloadableClientConfig implements IClientConfig { ...@@ -296,12 +300,20 @@ public abstract class ReloadableClientConfig implements IClientConfig {
return getClientDynamicProperty(key, true); return getClientDynamicProperty(key, true);
} }
@Override
public <T> Property<T> getScopedProperty(IClientConfigKey<T> key) {
return (Property<T>) dynamicProperties.computeIfAbsent(key, ignore -> createProperty(
() -> resolverScopedProperty(key),
key::defaultValue,
isDynamic));
}
@Override @Override
public <T> Property<T> getPrefixMappedProperty(IClientConfigKey<T> key) { public <T> Property<T> getPrefixMappedProperty(IClientConfigKey<T> key) {
return createProperty( return (Property<T>) dynamicProperties.computeIfAbsent(key, ignore -> createProperty(
getPrefixedMapPropertySupplier(key), getPrefixedMapPropertySupplier(key),
key::defaultValue, key::defaultValue,
isDynamic); isDynamic));
} }
private <T> Supplier<Optional<T>> getPrefixedMapPropertySupplier(IClientConfigKey<T> key) { private <T> Supplier<Optional<T>> getPrefixedMapPropertySupplier(IClientConfigKey<T> key) {
......
...@@ -191,5 +191,17 @@ public class ClientConfigTest { ...@@ -191,5 +191,17 @@ public class ClientConfigTest {
Property<CustomValueOf> prop = clientConfig.getDynamicProperty(CUSTOM_KEY); Property<CustomValueOf> prop = clientConfig.getDynamicProperty(CUSTOM_KEY);
Assert.assertEquals("value", prop.getOrDefault().getValue()); Assert.assertEquals("value", prop.getOrDefault().getValue());
} }
@Test
public void testScopedProperty() {
ConfigurationManager.getConfigInstance().setProperty("ribbon.foo.ScopePropertyTimeout", "2000");
ConfigurationManager.getConfigInstance().setProperty("testScopedProperty.ribbon.foo.ScopePropertyTimeout", "1000");
DefaultClientConfigImpl clientConfig = new DefaultClientConfigImpl();
clientConfig.setClientName("testScopedProperty");
Property<Integer> prop = clientConfig.getScopedProperty(new CommonClientConfigKey<Integer>("foo.ScopePropertyTimeout", 0) {});
Assert.assertEquals(1000, prop.get().get().intValue());
}
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册