Java Code Examples for com.ctrip.framework.apollo.ConfigService#getAppConfig()

The following examples show how to use com.ctrip.framework.apollo.ConfigService#getAppConfig() . 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 vote down vote up
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 vote down vote up
@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 3
Source File: ConfigIntegrationTest.java    From apollo with Apache License 2.0 6 votes vote down vote up
@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 4
Source File: SimpleApolloConfigDemo.java    From apollo with Apache License 2.0 6 votes vote down vote up
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 5
Source File: ConfigIntegrationTest.java    From apollo with Apache License 2.0 5 votes vote down vote up
@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 6
Source File: ConfigIntegrationTest.java    From apollo with Apache License 2.0 5 votes vote down vote up
@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 7
Source File: ConfigIntegrationTest.java    From apollo with Apache License 2.0 5 votes vote down vote up
@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 8
Source File: ConfigIntegrationTest.java    From apollo with Apache License 2.0 5 votes vote down vote up
@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 9
Source File: ConfigIntegrationTest.java    From apollo with Apache License 2.0 5 votes vote down vote up
@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 10
Source File: ApolloMockServerApiTest.java    From apollo with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetProperty() throws Exception {
  Config applicationConfig = ConfigService.getAppConfig();

  assertEquals("value1", applicationConfig.getProperty("key1", null));
  assertEquals("value2", applicationConfig.getProperty("key2", null));
}
 
Example 11
Source File: ApolloConfigManager.java    From jboot with Apache License 2.0 5 votes vote down vote up
private Config getDefaultConfig() {
    ApolloServerConfig apolloServerConfig = Jboot.config(ApolloServerConfig.class);
    if (StrUtil.isNotBlank(apolloServerConfig.getDefaultNamespace())) {
        return ConfigService.getConfig(apolloServerConfig.getDefaultNamespace());
    } else {
        return ConfigService.getAppConfig();
    }
}
 
Example 12
Source File: ItemController.java    From mini-platform with MIT License 5 votes vote down vote up
/**
 * 测试高可用
 * @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 13
Source File: ApolloAgent.java    From apollo-use-cases with Apache License 2.0 5 votes vote down vote up
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 14
Source File: PasswordMD5.java    From mini-platform with MIT License 4 votes vote down vote up
protected PasswordMD5() {
    Config config = ConfigService.getAppConfig();
    this.algorithm = config.getProperty("oauth.user.password.md5.algorithm", "MD5");
}
 
Example 15
Source File: PasswordPBKDF2.java    From mini-platform with MIT License 4 votes vote down vote up
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);
}
 
Example 16
Source File: ConfigIntegrationTest.java    From apollo with Apache License 2.0 4 votes vote down vote up
@Test
public void testLongPollRefreshWithMultipleNamespacesAndOnlyOneNamespaceNotified()
    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 someOtherConfig = ConfigService.getConfig(someOtherNamespace);
  Config config = ConfigService.getAppConfig();
  assertEquals(someValue, config.getProperty(someKey, null));
  assertEquals(someValue, someOtherConfig.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(5000, TimeUnit.MILLISECONDS);

  assertEquals(anotherValue, config.getProperty(someKey, null));

  TimeUnit.MILLISECONDS.sleep(pollTimeoutInMS * 10);
  assertEquals(someValue, someOtherConfig.getProperty(someKey, null));
}
 
Example 17
Source File: ApolloDynamicConfigManager.java    From sofa-rpc with Apache License 2.0 4 votes vote down vote up
protected ApolloDynamicConfigManager(String appName) {
    super(appName);
    config = ConfigService.getAppConfig();
}
 
Example 18
Source File: ApolloConfigLoader.java    From tunnel with Apache License 2.0 4 votes vote down vote up
public ApolloConfigLoader(String appId, String metaDomain) {
    this.config = ConfigService.getAppConfig();
}
 
Example 19
Source File: ConfigIntegrationTest.java    From apollo with Apache License 2.0 4 votes vote down vote up
@Test
public void testRefreshConfig() throws Exception {
  final String someKey = "someKey";
  final String someValue = "someValue";
  final String anotherValue = "anotherValue";

  int someRefreshInterval = 500;
  TimeUnit someRefreshTimeUnit = TimeUnit.MILLISECONDS;

  setRefreshInterval(someRefreshInterval);
  setRefreshTimeUnit(someRefreshTimeUnit);

  Map<String, String> configurations = Maps.newHashMap();
  configurations.put(someKey, someValue);
  ApolloConfig apolloConfig = assembleApolloConfig(configurations);
  ContextHandler handler = mockConfigServerHandler(HttpServletResponse.SC_OK, apolloConfig);
  startServerWithHandlers(handler);

  Config config = ConfigService.getAppConfig();
  final List<ConfigChangeEvent> changeEvents = Lists.newArrayList();

  final SettableFuture<Boolean> refreshFinished = SettableFuture.create();
  config.addChangeListener(new ConfigChangeListener() {
    AtomicInteger counter = new AtomicInteger(0);

    @Override
    public void onChange(ConfigChangeEvent changeEvent) {
      //only need to assert once
      if (counter.incrementAndGet() > 1) {
        return;
      }
      assertEquals(1, changeEvent.changedKeys().size());
      assertTrue(changeEvent.isChanged(someKey));
      assertEquals(someValue, changeEvent.getChange(someKey).getOldValue());
      assertEquals(anotherValue, changeEvent.getChange(someKey).getNewValue());
      // if there is any assertion failed above, this line won't be executed
      changeEvents.add(changeEvent);
      refreshFinished.set(true);
    }
  });

  apolloConfig.getConfigurations().put(someKey, anotherValue);

  refreshFinished.get(someRefreshInterval * 5, someRefreshTimeUnit);

  assertThat(
      "Change event's size should equal to one or there must be some assertion failed in change listener",
      1, equalTo(changeEvents.size()));
  assertEquals(anotherValue, config.getProperty(someKey, null));
}
 
Example 20
Source File: ApolloConfigLoader.java    From tunnel with Apache License 2.0 4 votes vote down vote up
public ApolloConfigLoader(String appId, String metaDomain) {
    this.config = ConfigService.getAppConfig();
}