com.ctrip.framework.apollo.ConfigService Java Examples
The following examples show how to use
com.ctrip.framework.apollo.ConfigService.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ApolloConfigWatcherRegister.java From skywalking with Apache License 2.0 | 6 votes |
public ApolloConfigWatcherRegister(ApolloConfigurationCenterSettings settings) { super(settings.getPeriod()); final String namespace = settings.getNamespace(); final boolean isDefaultNamespace = Strings.isNullOrEmpty(namespace); if (isDefaultNamespace) { this.configReader = ConfigService.getAppConfig(); if (LOGGER.isInfoEnabled()) { LOGGER.info("Read dynamic configs from Apollo default namespace"); } } else { this.configReader = ConfigService.getConfig(namespace); if (LOGGER.isInfoEnabled()) { LOGGER.info("Read dynamic configs from Apollo namespace: {}", namespace); } } }
Example #2
Source File: ConfigIntegrationTest.java From apollo with Apache License 2.0 | 6 votes |
@Test public void testOrderGetConfigWithNoLocalFileButWithRemoteConfig() throws Exception { setPropertiesOrderEnabled(true); String someKey1 = "someKey1"; String someValue1 = "someValue1"; String someKey2 = "someKey2"; String someValue2 = "someValue2"; Map<String, String> configurations = new LinkedHashMap<>(); configurations.put(someKey1, someValue1); configurations.put(someKey2, someValue2); ApolloConfig apolloConfig = assembleApolloConfig(ImmutableMap.copyOf(configurations)); ContextHandler handler = mockConfigServerHandler(HttpServletResponse.SC_OK, apolloConfig); startServerWithHandlers(handler); Config config = ConfigService.getAppConfig(); Set<String> propertyNames = config.getPropertyNames(); Iterator<String> it = propertyNames.iterator(); assertEquals(someKey1, it.next()); assertEquals(someKey2, it.next()); }
Example #3
Source File: ConfigIntegrationTest.java From apollo with Apache License 2.0 | 6 votes |
@Test public void testGetConfigWithLocalFileAndWithRemoteConfig() throws Exception { String someKey = "someKey"; String someValue = "someValue"; String anotherValue = "anotherValue"; Properties properties = new Properties(); properties.put(someKey, someValue); createLocalCachePropertyFile(properties); ApolloConfig apolloConfig = assembleApolloConfig(ImmutableMap.of(someKey, anotherValue)); ContextHandler handler = mockConfigServerHandler(HttpServletResponse.SC_OK, apolloConfig); startServerWithHandlers(handler); Config config = ConfigService.getAppConfig(); assertEquals(anotherValue, config.getProperty(someKey, null)); }
Example #4
Source File: ApolloApplicationContextInitializer.java From apollo with Apache License 2.0 | 6 votes |
/** * Initialize Apollo Configurations Just after environment is ready. * * @param environment */ protected void initialize(ConfigurableEnvironment environment) { if (environment.getPropertySources().contains(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME)) { //already initialized return; } String namespaces = environment.getProperty(PropertySourcesConstants.APOLLO_BOOTSTRAP_NAMESPACES, ConfigConsts.NAMESPACE_APPLICATION); logger.debug("Apollo bootstrap namespaces: {}", namespaces); List<String> namespaceList = NAMESPACE_SPLITTER.splitToList(namespaces); CompositePropertySource composite = new CompositePropertySource(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME); for (String namespace : namespaceList) { Config config = ConfigService.getConfig(namespace); composite.addPropertySource(configPropertySourceFactory.getConfigPropertySource(namespace, config)); } environment.getPropertySources().addFirst(composite); }
Example #5
Source File: ApolloAnnotationProcessor.java From apollo with Apache License 2.0 | 6 votes |
@Override protected void processField(Object bean, String beanName, Field field) { ApolloConfig annotation = AnnotationUtils.getAnnotation(field, ApolloConfig.class); if (annotation == null) { return; } Preconditions.checkArgument(Config.class.isAssignableFrom(field.getType()), "Invalid type: %s for field: %s, should be Config", field.getType(), field); String namespace = annotation.value(); Config config = ConfigService.getConfig(namespace); ReflectionUtils.makeAccessible(field); ReflectionUtils.setField(field, bean, config); }
Example #6
Source File: ApolloMockServerApiTest.java From apollo with Apache License 2.0 | 6 votes |
@Test public void testUpdateSamePropertyTwice() throws Exception { String someNewValue = "someNewValue"; Config otherConfig = ConfigService.getConfig(anotherNamespace); final Semaphore changes = new Semaphore(0); otherConfig.addChangeListener(new ConfigChangeListener() { @Override public void onChange(ConfigChangeEvent changeEvent) { changes.release(); } }); assertEquals("otherValue3", otherConfig.getProperty("key3", null)); embeddedApollo.addOrModifyProperty(anotherNamespace, "key3", someNewValue); embeddedApollo.addOrModifyProperty(anotherNamespace, "key3", someNewValue); assertTrue(changes.tryAcquire(5, TimeUnit.SECONDS)); assertEquals(someNewValue, otherConfig.getProperty("key3", null)); assertEquals(0, changes.availablePermits()); }
Example #7
Source File: ApolloDataSource.java From Sentinel with Apache License 2.0 | 6 votes |
/** * Constructs the Apollo data source * * @param namespaceName the namespace name in Apollo, should not be null or empty * @param ruleKey the rule key in the namespace, should not be null or empty * @param defaultRuleValue the default rule value when the ruleKey is not found or any error * occurred * @param parser the parser to transform string configuration to actual flow rules */ public ApolloDataSource(String namespaceName, String ruleKey, String defaultRuleValue, Converter<String, T> parser) { super(parser); Preconditions.checkArgument(!Strings.isNullOrEmpty(namespaceName), "Namespace name could not be null or empty"); Preconditions.checkArgument(!Strings.isNullOrEmpty(ruleKey), "RuleKey could not be null or empty!"); this.ruleKey = ruleKey; this.defaultRuleValue = defaultRuleValue; this.config = ConfigService.getConfig(namespaceName); initialize(); RecordLog.info(String.format("Initialized rule for namespace: %s, rule key: %s", namespaceName, ruleKey)); }
Example #8
Source File: ApolloMockServerApiTest.java From apollo with Apache License 2.0 | 6 votes |
@Test public void testDeleteSamePropertyTwice() throws Exception { Config otherConfig = ConfigService.getConfig(anotherNamespace); final Semaphore changes = new Semaphore(0); otherConfig.addChangeListener(new ConfigChangeListener() { @Override public void onChange(ConfigChangeEvent changeEvent) { changes.release(); } }); assertEquals("otherValue6", otherConfig.getProperty("key6", null)); embeddedApollo.deleteProperty(anotherNamespace, "key6"); embeddedApollo.deleteProperty(anotherNamespace, "key6"); assertTrue(changes.tryAcquire(5, TimeUnit.SECONDS)); assertNull(otherConfig.getProperty("key6", null)); assertEquals(0, changes.availablePermits()); }
Example #9
Source File: SimpleApolloConfigDemo.java From apollo with Apache License 2.0 | 6 votes |
public SimpleApolloConfigDemo() { ConfigChangeListener changeListener = new ConfigChangeListener() { @Override public void onChange(ConfigChangeEvent changeEvent) { logger.info("Changes for namespace {}", changeEvent.getNamespace()); for (String key : changeEvent.changedKeys()) { ConfigChange change = changeEvent.getChange(key); logger.info("Change - key: {}, oldValue: {}, newValue: {}, changeType: {}", change.getPropertyName(), change.getOldValue(), change.getNewValue(), change.getChangeType()); } } }; config = ConfigService.getAppConfig(); config.addChangeListener(changeListener); }
Example #10
Source File: ApolloDataSource.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 6 votes |
/** * Constructs the Apollo data source * * @param namespaceName the namespace name in Apollo, should not be null or empty * @param ruleKey the rule key in the namespace, should not be null or empty * @param defaultRuleValue the default rule value when the ruleKey is not found or any error * occurred * @param parser the parser to transform string configuration to actual flow rules */ public ApolloDataSource(String namespaceName, String ruleKey, String defaultRuleValue, Converter<String, T> parser) { super(parser); Preconditions.checkArgument(!Strings.isNullOrEmpty(namespaceName), "Namespace name could not be null or empty"); Preconditions.checkArgument(!Strings.isNullOrEmpty(ruleKey), "RuleKey could not be null or empty!"); this.ruleKey = ruleKey; this.defaultRuleValue = defaultRuleValue; this.config = ConfigService.getConfig(namespaceName); initialize(); RecordLog.info(String.format("Initialized rule for namespace: %s, rule key: %s", namespaceName, ruleKey)); }
Example #11
Source File: ApolloMockServerApiTest.java From apollo with Apache License 2.0 | 5 votes |
@Test public void testGetProperty() throws Exception { Config applicationConfig = ConfigService.getAppConfig(); assertEquals("value1", applicationConfig.getProperty("key1", null)); assertEquals("value2", applicationConfig.getProperty("key2", null)); }
Example #12
Source File: ConfigIntegrationTest.java From apollo with Apache License 2.0 | 5 votes |
@Test public void testLongPollRefresh() throws Exception { final String someKey = "someKey"; final String someValue = "someValue"; final String anotherValue = "anotherValue"; long someNotificationId = 1; long pollTimeoutInMS = 50; Map<String, String> configurations = Maps.newHashMap(); configurations.put(someKey, someValue); ApolloConfig apolloConfig = assembleApolloConfig(configurations); ContextHandler configHandler = mockConfigServerHandler(HttpServletResponse.SC_OK, apolloConfig); ContextHandler pollHandler = mockPollNotificationHandler(pollTimeoutInMS, HttpServletResponse.SC_OK, Lists.newArrayList( new ApolloConfigNotification(apolloConfig.getNamespaceName(), someNotificationId)), false); startServerWithHandlers(configHandler, pollHandler); Config config = ConfigService.getAppConfig(); assertEquals(someValue, config.getProperty(someKey, null)); final SettableFuture<Boolean> longPollFinished = SettableFuture.create(); config.addChangeListener(new ConfigChangeListener() { @Override public void onChange(ConfigChangeEvent changeEvent) { longPollFinished.set(true); } }); apolloConfig.getConfigurations().put(someKey, anotherValue); longPollFinished.get(pollTimeoutInMS * 20, TimeUnit.MILLISECONDS); assertEquals(anotherValue, config.getProperty(someKey, null)); }
Example #13
Source File: ConfigIntegrationTest.java From apollo with Apache License 2.0 | 5 votes |
@Test public void testGetConfigWithNoLocalFileAndRemoteConfigServiceRetry() throws Exception { String someKey = "someKey"; String someValue = "someValue"; ApolloConfig apolloConfig = assembleApolloConfig(ImmutableMap.of(someKey, someValue)); boolean failedAtFirstTime = true; ContextHandler handler = mockConfigServerHandler(HttpServletResponse.SC_OK, apolloConfig, failedAtFirstTime); startServerWithHandlers(handler); Config config = ConfigService.getAppConfig(); assertEquals(someValue, config.getProperty(someKey, null)); }
Example #14
Source File: ConfigIntegrationTest.java From apollo with Apache License 2.0 | 5 votes |
@Test public void testGetConfigWithNoLocalFileAndRemoteMetaServiceRetry() throws Exception { String someKey = "someKey"; String someValue = "someValue"; ApolloConfig apolloConfig = assembleApolloConfig(ImmutableMap.of(someKey, someValue)); ContextHandler configHandler = mockConfigServerHandler(HttpServletResponse.SC_OK, apolloConfig); boolean failAtFirstTime = true; ContextHandler metaServerHandler = mockMetaServerHandler(failAtFirstTime); startServerWithHandlers(metaServerHandler, configHandler); Config config = ConfigService.getAppConfig(); assertEquals(someValue, config.getProperty(someKey, null)); }
Example #15
Source File: ConfigIntegrationTest.java From apollo with Apache License 2.0 | 5 votes |
@Test public void testOrderGetConfigWithLocalFileAndRemoteConfigError() throws Exception { String someKey1 = "someKey1"; String someValue1 = "someValue1"; String someKey2 = "someKey2"; String someValue2 = "someValue2"; setPropertiesOrderEnabled(true); Properties properties = new OrderedProperties(); properties.put(someKey1, someValue1); properties.put(someKey2, someValue2); createLocalCachePropertyFile(properties); ContextHandler handler = mockConfigServerHandler(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, null); startServerWithHandlers(handler); Config config = ConfigService.getAppConfig(); assertEquals(someValue1, config.getProperty(someKey1, null)); assertEquals(someValue2, config.getProperty(someKey2, null)); Set<String> propertyNames = config.getPropertyNames(); Iterator<String> it = propertyNames.iterator(); assertEquals(someKey1, it.next()); assertEquals(someKey2, it.next()); }
Example #16
Source File: ConfigIntegrationTest.java From apollo with Apache License 2.0 | 5 votes |
@Test public void testGetConfigWithLocalFileAndRemoteConfigError() throws Exception { String someKey = "someKey"; String someValue = "someValue"; Properties properties = new Properties(); properties.put(someKey, someValue); createLocalCachePropertyFile(properties); ContextHandler handler = mockConfigServerHandler(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, null); startServerWithHandlers(handler); Config config = ConfigService.getAppConfig(); assertEquals(someValue, config.getProperty(someKey, null)); }
Example #17
Source File: ConfigIntegrationTest.java From apollo with Apache License 2.0 | 5 votes |
@Test public void testGetConfigWithNoLocalFileAndRemoteConfigError() throws Exception { ContextHandler handler = mockConfigServerHandler(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, null); startServerWithHandlers(handler); Config config = ConfigService.getAppConfig(); String someKey = "someKey"; String someDefaultValue = "defaultValue" + Math.random(); assertEquals(someDefaultValue, config.getProperty(someKey, someDefaultValue)); }
Example #18
Source File: ApolloConfigWrapper.java From shardingsphere with Apache License 2.0 | 5 votes |
public ApolloConfigWrapper(final CenterConfiguration config, final ApolloProperties props) { String appId = props.getValue(ApolloPropertyKey.APP_ID); String env = props.getValue(ApolloPropertyKey.ENV); String clusterName = props.getValue(ApolloPropertyKey.CLUSTER_NAME); System.setProperty(APOLLO_KEY_APP_ID, appId); System.setProperty(APOLLO_KEY_ENV, env); System.setProperty(ConfigConsts.APOLLO_CLUSTER_KEY, clusterName); System.setProperty(ConfigConsts.APOLLO_META_KEY, config.getServerLists()); String namespace = config.getNamespace(); apolloConfig = ConfigService.getConfig(namespace); }
Example #19
Source File: ConfigIntegrationTest.java From apollo with Apache License 2.0 | 5 votes |
@Test public void testOrderGetConfigWithLocalFileAndWithRemoteConfig() throws Exception { String someKey = "someKey"; String someValue = "someValue"; String anotherValue = "anotherValue"; String someKey1 = "someKey1"; String someValue1 = "someValue1"; String anotherValue1 = "anotherValue1"; String someKey2 = "someKey2"; String someValue2 = "someValue2"; setPropertiesOrderEnabled(true); Properties properties = new OrderedProperties(); properties.put(someKey, someValue); properties.put(someKey1, someValue1); properties.put(someKey2, someValue2); createLocalCachePropertyFile(properties); Map<String, String> configurations = new LinkedHashMap<>(); configurations.put(someKey, anotherValue); configurations.put(someKey1, anotherValue1); configurations.put(someKey2, someValue2); ApolloConfig apolloConfig = assembleApolloConfig(ImmutableMap.copyOf(configurations)); ContextHandler handler = mockConfigServerHandler(HttpServletResponse.SC_OK, apolloConfig); startServerWithHandlers(handler); Config config = ConfigService.getAppConfig(); assertEquals(anotherValue, config.getProperty(someKey, null)); Set<String> propertyNames = config.getPropertyNames(); Iterator<String> it = propertyNames.iterator(); assertEquals(someKey, it.next()); assertEquals(someKey1, it.next()); assertEquals(someKey2, it.next()); assertEquals(anotherValue1, config.getProperty(someKey1, "")); }
Example #20
Source File: ConfigIntegrationTest.java From apollo with Apache License 2.0 | 5 votes |
@Test public void testGetConfigWithNoLocalFileButWithRemoteConfig() throws Exception { String someKey = "someKey"; String someValue = "someValue"; String someNonExistedKey = "someNonExistedKey"; String someDefaultValue = "someDefaultValue"; ApolloConfig apolloConfig = assembleApolloConfig(ImmutableMap.of(someKey, someValue)); ContextHandler handler = mockConfigServerHandler(HttpServletResponse.SC_OK, apolloConfig); startServerWithHandlers(handler); Config config = ConfigService.getAppConfig(); assertEquals(someValue, config.getProperty(someKey, null)); assertEquals(someDefaultValue, config.getProperty(someNonExistedKey, someDefaultValue)); }
Example #21
Source File: DefaultConfigFactory.java From apollo with Apache License 2.0 | 5 votes |
PropertiesCompatibleFileConfigRepository createPropertiesCompatibleFileConfigRepository(String namespace, ConfigFileFormat format) { String actualNamespaceName = trimNamespaceFormat(namespace, format); PropertiesCompatibleConfigFile configFile = (PropertiesCompatibleConfigFile) ConfigService .getConfigFile(actualNamespaceName, format); return new PropertiesCompatibleFileConfigRepository(configFile); }
Example #22
Source File: PropertySourcesProcessor.java From apollo with Apache License 2.0 | 5 votes |
private void initializePropertySources() { if (environment.getPropertySources().contains(PropertySourcesConstants.APOLLO_PROPERTY_SOURCE_NAME)) { //already initialized return; } CompositePropertySource composite = new CompositePropertySource(PropertySourcesConstants.APOLLO_PROPERTY_SOURCE_NAME); //sort by order asc ImmutableSortedSet<Integer> orders = ImmutableSortedSet.copyOf(NAMESPACE_NAMES.keySet()); Iterator<Integer> iterator = orders.iterator(); while (iterator.hasNext()) { int order = iterator.next(); for (String namespace : NAMESPACE_NAMES.get(order)) { Config config = ConfigService.getConfig(namespace); composite.addPropertySource(configPropertySourceFactory.getConfigPropertySource(namespace, config)); } } // clean up NAMESPACE_NAMES.clear(); // add after the bootstrap property source or to the first if (environment.getPropertySources() .contains(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME)) { // ensure ApolloBootstrapPropertySources is still the first ensureBootstrapPropertyPrecedence(environment); environment.getPropertySources() .addAfter(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME, composite); } else { environment.getPropertySources().addFirst(composite); } }
Example #23
Source File: ApolloConfigManager.java From jboot with Apache License 2.0 | 5 votes |
private Config getDefaultConfig() { ApolloServerConfig apolloServerConfig = Jboot.config(ApolloServerConfig.class); if (StrUtil.isNotBlank(apolloServerConfig.getDefaultNamespace())) { return ConfigService.getConfig(apolloServerConfig.getDefaultNamespace()); } else { return ConfigService.getAppConfig(); } }
Example #24
Source File: UserController.java From mini-platform with MIT License | 5 votes |
/** * @param request * @return */ @GetMapping("/demo") public String demo(HttpServletRequest request) { //Apollo配置中心示例 //config instance is singleton for each namespace and is never null Config config = ConfigService.getAppConfig(); String timeoutKey = "timeout"; String timeoutDefaultValue = "100"; String value = config.getProperty(timeoutKey, timeoutDefaultValue); //OAuth Client示例 Boolean isLogin = isLogin(); Long loginUserId = getUserId(); UserInfo userInfo = getUserInfo(); //高可用测试 String path = request.getRemoteHost() + ":" + request.getServerPort(); return " Path:" + path + System.getProperty("line.separator", "\n") + " Timeout: " + value + System.getProperty("line.separator", "\n") + " is login: " + isLogin + System.getProperty("line.separator", "\n") + " userId: " + loginUserId + System.getProperty("line.separator", "\n") + " userName: " + (userInfo == null ? "Null" : userInfo.getUserName()) + System.getProperty("line.separator", "\n") + " clientId: " + (userInfo == null ? "Null" : userInfo.getClientId()) + System.getProperty("line.separator", "\n") ; }
Example #25
Source File: ItemController.java From mini-platform with MIT License | 5 votes |
/** * 测试高可用 * @param request * @return */ @GetMapping("url") public String get(HttpServletRequest request) { //config instance is singleton for each namespace and is never null Config config = ConfigService.getAppConfig(); String someKey = "timeout"; String someDefaultValue = "100"; String value = config.getProperty(someKey, someDefaultValue); return ">>>>>" + "Host:" + request.getRemoteHost() + " Port: 【" + request.getServerPort() + "】 Path:" + request.getRequestURI() + " Timeout: " + value; }
Example #26
Source File: ApolloAgent.java From apollo-use-cases with Apache License 2.0 | 5 votes |
public static void premain(String agentArgs, Instrumentation inst) { Config config = ConfigService.getAppConfig(); for (String key : config.getPropertyNames()) { System.getProperties().put(key, config.getProperty(key, "")); } LOGGER.debug("Apollo Configure load complete"); }
Example #27
Source File: ApolloSpringApplicationRunListener.java From summerframework with Apache License 2.0 | 5 votes |
private void initInfraConfig(ConfigurableEnvironment env) { com.ctrip.framework.apollo.Config apolloConfig = ConfigService.getConfig(CONFIGCENTER_INFRA_NAMESPACE); Set<String> propertyNames = apolloConfig.getPropertyNames(); if (propertyNames != null && propertyNames.size() > 0) { Properties propertes = new Properties(); for (String propertyName : propertyNames) { propertes.setProperty(propertyName, apolloConfig.getProperty(propertyName, null)); } EnumerablePropertySource enumerablePropertySource = new PropertiesPropertySource(CONFIGCENTER_INFRA_NAMESPACE, propertes); env.getPropertySources().addLast(enumerablePropertySource); } }
Example #28
Source File: ApolloConfigLoader.java From tunnel with Apache License 2.0 | 4 votes |
public ApolloConfigLoader(String appId, String metaDomain) { this.config = ConfigService.getAppConfig(); }
Example #29
Source File: PasswordMD5.java From mini-platform with MIT License | 4 votes |
protected PasswordMD5() { Config config = ConfigService.getAppConfig(); this.algorithm = config.getProperty("oauth.user.password.md5.algorithm", "MD5"); }
Example #30
Source File: PasswordPBKDF2.java From mini-platform with MIT License | 4 votes |
protected PasswordPBKDF2() { Config config = ConfigService.getAppConfig(); this.algorithm = config.getProperty("oauth.user.password.pbkdf2.algorithm", "PBKDF2WithHmacSHA1"); this.keyLength = config.getIntProperty("oauth.user.password.pbkdf2.keyLength", 128); this.iterationCount = config.getIntProperty("oauth.user.password.pbkdf2.iterationCount", 1024); }