Skip to content

Commit

Permalink
SCB-315 Config Center module can't get config by DynamicPropertyFacto…
Browse files Browse the repository at this point in the history
…ry.getInstance().getStringProperty, so has to use ConcurrentCompositeConfiguration
  • Loading branch information
jeho0815 authored and WillemJiang committed Feb 3, 2018
1 parent d382ead commit 153cdeb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@
import java.util.List;

import org.apache.commons.configuration.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.netflix.config.DynamicPropertyFactory;

import io.vertx.core.dns.AddressResolverOptions;

public class AddressResolverConfig {

private static final Logger LOGGER = LoggerFactory.getLogger(AddressResolverConfig.class);

/**
* get the target endpoints with custom address resolve config
* @param tag config tag, such as sc.consumer or cc.consumer
Expand Down Expand Up @@ -56,27 +60,27 @@ public static AddressResolverOptions getAddressResover(String tag, Configuration
"addressResolver." + tag + ".optResourceEnabled",
"addressResolver.optResourceEnabled"));
addressResolverOptions
.setCacheMinTimeToLive(getIntProperty(configSource,
.setCacheMinTimeToLive(getPositiveIntProperty(configSource,
AddressResolverOptions.DEFAULT_CACHE_MIN_TIME_TO_LIVE,
"addressResolver." + tag + ".cacheMinTimeToLive",
"addressResolver.cacheMinTimeToLive"));
addressResolverOptions
.setCacheMaxTimeToLive(getIntProperty(configSource,
.setCacheMaxTimeToLive(getPositiveIntProperty(configSource,
AddressResolverOptions.DEFAULT_CACHE_MAX_TIME_TO_LIVE,
"addressResolver." + tag + ".cacheMaxTimeToLive",
"addressResolver.cacheMaxTimeToLive"));
addressResolverOptions
.setCacheNegativeTimeToLive(getIntProperty(configSource,
.setCacheNegativeTimeToLive(getPositiveIntProperty(configSource,
AddressResolverOptions.DEFAULT_CACHE_NEGATIVE_TIME_TO_LIVE,
"addressResolver." + tag + ".cacheNegativeTimeToLive",
"addressResolver.cacheNegativeTimeToLive"));
addressResolverOptions
.setQueryTimeout(getIntProperty(configSource,
.setQueryTimeout(getPositiveIntProperty(configSource,
AddressResolverOptions.DEFAULT_QUERY_TIMEOUT,
"addressResolver." + tag + ".queryTimeout",
"addressResolver.queryTimeout"));
addressResolverOptions
.setMaxQueries(getIntProperty(configSource,
.setMaxQueries(getPositiveIntProperty(configSource,
AddressResolverOptions.DEFAULT_MAX_QUERIES,
"addressResolver." + tag + ".maxQueries",
"addressResolver.maxQueries"));
Expand All @@ -91,7 +95,7 @@ public static AddressResolverOptions getAddressResover(String tag, Configuration
"addressResolver." + tag + ".searchDomains",
"addressResolver.searchDomains"));
addressResolverOptions
.setNdots(getIntProperty(configSource,
.setNdots(getPositiveIntProperty(configSource,
AddressResolverOptions.DEFAULT_CACHE_MIN_TIME_TO_LIVE,
"addressResolver." + tag + ".ndots",
"addressResolver.ndots"));
Expand All @@ -117,13 +121,17 @@ private static List<String> getStringListProperty(Configuration configSource,
return defaultValue;
}

private static int getIntProperty(Configuration configSource, int defaultValue, String... keys) {
private static int getPositiveIntProperty(Configuration configSource, int defaultValue, String... keys) {
if (configSource == null) {
configSource = (Configuration) DynamicPropertyFactory.getBackingConfigurationSource();
}
for (String key : keys) {
Integer val = configSource.getInteger(key, null);
if (val != null && val > 0) {
if (val != null && val <= 0) {
LOGGER.warn("Address resover key:{}'s value:{} is not positive, please check!", key, val);
continue;
}
if (val != null) {
return val;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public void testGetResoverFromResource(@Mocked Configuration finalConfig) {
result = new String[] {"default.svc.local.cluster"};
finalConfig.getInteger("addressResolver.queryTimeout", null);
result = 2000;
finalConfig.getInteger("addressResolver.maxQueries", null);
result = -2;
}
};
AddressResolverOptions aroc = AddressResolverConfig.getAddressResover("test", finalConfig);
Expand All @@ -64,6 +66,8 @@ public void testGetResoverFromResource(@Mocked Configuration finalConfig) {
is(Arrays.asList("default.svc.local.cluster")));
Assert.assertEquals(aroc.getQueryTimeout(),
2000);
Assert.assertNotEquals(aroc.getMaxQueries(),
-2);
}

@Test
Expand Down

0 comments on commit 153cdeb

Please sign in to comment.