com.netflix.config.DynamicWatchedConfiguration Java Examples

The following examples show how to use com.netflix.config.DynamicWatchedConfiguration. 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: ZkUtils.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
public static void init() {
    try {
        curatorClient = CuratorFrameworkFactory
                .builder()
                .connectString(zkConfig.getZkAddrs())
                .sessionTimeoutMs(zkConfig.getZkSessionTimeoutMs())
                .retryPolicy(new BoundedExponentialBackoffRetry(zkConfig.getBaseSleepTimeMs(), zkConfig.getMaxSleepMs(), zkConfig.getMaxRetries()))
                .build();

        if (curatorClient.getState() == CuratorFrameworkState.LATENT) {
            curatorClient.start();
        }

        ZooKeeperConfigurationSource zkConfigSource = new ZooKeeperConfigurationSource(curatorClient, Constants.META_BASE_ZK_PATH);
        zkConfigSource.start();
        DynamicWatchedConfiguration zkDynamicConfig = new DynamicWatchedConfiguration(zkConfigSource);
        ConfigurationManager.install(zkDynamicConfig);
    } catch (Exception e) {
        LOGGER.error("ZkUtils getCuratorClient err:{}", e.getMessage(), e);
    }
}
 
Example #2
Source File: TestConfigUtil.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateDynamicConfigNoConfigCenterSPI() {
  new Expectations(SPIServiceUtils.class) {
    {
      SPIServiceUtils.getTargetService(ConfigCenterConfigurationSource.class);
      result = null;
    }
  };

  AbstractConfiguration dynamicConfig = ConfigUtil.createDynamicConfig();
  MicroserviceConfigLoader loader = ConfigUtil.getMicroserviceConfigLoader(dynamicConfig);
  List<ConfigModel> list = loader.getConfigModels();
  Assert.assertEquals(loader, ConfigUtil.getMicroserviceConfigLoader(dynamicConfig));
  Assert.assertEquals(1, list.size());
  Assert.assertNotEquals(DynamicWatchedConfiguration.class,
      ((ConcurrentCompositeConfiguration) dynamicConfig).getConfiguration(0).getClass());
}
 
Example #3
Source File: ZkUtils.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
public static void init() {
    try {
        curatorClient = CuratorFrameworkFactory
                .builder()
                .connectString(zkConfig.getZkAddrs())
                .sessionTimeoutMs(zkConfig.getZkSessionTimeoutMs())
                .retryPolicy(new BoundedExponentialBackoffRetry(zkConfig.getBaseSleepTimeMs(), zkConfig.getMaxSleepMs(), zkConfig.getMaxRetries()))
                .build();

        if (curatorClient.getState() == CuratorFrameworkState.LATENT) {
            curatorClient.start();
        }

        ZooKeeperConfigurationSource zkConfigSource = new ZooKeeperConfigurationSource(curatorClient, Constants.META_BASE_ZK_PATH);
        zkConfigSource.start();
        DynamicWatchedConfiguration zkDynamicConfig = new DynamicWatchedConfiguration(zkConfigSource);
        ConfigurationManager.install(zkDynamicConfig);
    } catch (Exception e) {
        LOGGER.error("ZkUtils getCuratorClient err:{}", e.getMessage(), e);
    }
}
 
Example #4
Source File: ConfigUtil.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
private static void createDynamicWatchedConfiguration(
    ConcurrentCompositeConfiguration localConfiguration,
    ConfigCenterConfigurationSource configCenterConfigurationSource) {
  ConcurrentMapConfiguration injectConfig = new ConcurrentMapConfiguration();
  localConfiguration.addConfigurationAtFront(injectConfig, "extraInjectConfig");
  configCenterConfigurationSource.addUpdateListener(new ServiceCombPropertyUpdateListener(injectConfig));

  DynamicWatchedConfiguration configFromConfigCenter =
      new DynamicWatchedConfiguration(configCenterConfigurationSource);
  duplicateCseConfigToServicecomb(configFromConfigCenter);
  localConfiguration.addConfigurationAtFront(configFromConfigCenter, "configCenterConfig");
}
 
Example #5
Source File: ConsulController.java    From james with Apache License 2.0 5 votes vote down vote up
private void setupConsulWatcher(ConsulControllerConfiguration configuration,
                                InformationPointService informationPointService) {
    ConsulClient client = new ConsulClient(configuration.getHost(), configuration.getPort());
    ConsulWatchedConfigurationSource configurationSource =
            new ConsulWatchedConfigurationSource(configuration.getFolderPath(), client);
    DynamicWatchedConfiguration dynamicConfig = new DynamicWatchedConfiguration(configurationSource);

    dynamicConfig.addConfigurationListener(event -> {
        if (!event.isBeforeUpdate()) {
            switch (event.getType()) {
                case AbstractConfiguration.EVENT_ADD_PROPERTY:
                    onInformationPointAdded(event, informationPointService);
                    break;
                case AbstractConfiguration.EVENT_SET_PROPERTY:
                    onInformationPointModified(event, informationPointService);
                    break;
                case AbstractConfiguration.EVENT_CLEAR_PROPERTY:
                    onInformationPointRemoved(event, informationPointService);
                    break;
                case AbstractConfiguration.EVENT_CLEAR:
                    onInformationPointsCleared(informationPointService);
                    break;
                default:
                    LOG.debug(() -> "Unsupported event type: " + event.getType());
            }
        }
    });
    configurationSource.startAsync();

    ConcurrentCompositeConfiguration compositeConfig = new ConcurrentCompositeConfiguration();
    compositeConfig.addConfiguration(dynamicConfig, "consul-dynamic");
    ConfigurationManager.install(compositeConfig);
}
 
Example #6
Source File: ArchaiusConfigMapSourceRegistar.java    From spring-cloud-kubernetes with Apache License 2.0 5 votes vote down vote up
private void registerSourceConfiguration(BeanDefinitionRegistry registry, Object name, Object namespace) {
    BeanDefinitionBuilder configMapSourceConfigBuilder = BeanDefinitionBuilder.genericBeanDefinition(ArchaiusConfigMapSourceConfiguration.class);
    BeanDefinitionBuilder dynamicWatchedConfigBuilder = BeanDefinitionBuilder.genericBeanDefinition(DynamicWatchedConfiguration.class);

    configMapSourceConfigBuilder.addConstructorArgReference(KUBERNETES_CLIENT_REF);
    configMapSourceConfigBuilder.addConstructorArgValue(name);
    configMapSourceConfigBuilder.addConstructorArgValue(namespace);
    String configMapSourceConfigName = name + CONFIG_MAP_SOURCE_SUFFIX;
    registry.registerBeanDefinition(configMapSourceConfigName,  configMapSourceConfigBuilder.getBeanDefinition());

    String dynamicWatchedConfigName = name + DYNAMIC_WATCH_CONFIG_SUFFIX;
    dynamicWatchedConfigBuilder.addConstructorArgReference(configMapSourceConfigName);
    registry.registerBeanDefinition(dynamicWatchedConfigName, dynamicWatchedConfigBuilder.getBeanDefinition());
}
 
Example #7
Source File: ZookeeperConfigurationProvider.java    From chassis with Apache License 2.0 5 votes vote down vote up
/**
 * @see ConfigurationProvider#getApplicationConfiguration(String, String, String, com.kixeye.chassis.bootstrap.aws.ServerInstanceContext)
 */
@Override
public AbstractConfiguration getApplicationConfiguration(String environment, String applicationName, String applicationVersion, ServerInstanceContext serverInstanceContext) {
    String instanceId = serverInstanceContext == null ? "local" : serverInstanceContext.getInstanceId();

    String configRoot = getPath(environment, applicationName, applicationVersion);
    String primaryConfigPath = configRoot += "/config";
    String instanceConfigNode = instanceId + "-config";

    checkPath(primaryConfigPath);

    ZooKeeperConfigurationSource source = new ZooKeeperConfigurationSource(curatorFramework, primaryConfigPath);

    try {
        source.start();
    } catch (Exception e) {
        source.close();
        BootstrapException.zookeeperInitializationFailed(zookeeperConnectionString, primaryConfigPath, e);
    }

    logger.debug("Initializing zookeeper configuration from host " + zookeeperConnectionString + " at path " + primaryConfigPath);

    ConcurrentCompositeConfiguration configuration = new ConcurrentCompositeConfiguration();
    configuration.addConfiguration(getServerInstanceSpecificApplicationConfiguration(configRoot, instanceConfigNode));
    configuration.addConfiguration(new DynamicWatchedConfiguration(source));

    return configuration;
}
 
Example #8
Source File: DynamicZookeeperConfigurationSourceTest.java    From chassis with Apache License 2.0 5 votes vote down vote up
@Test
public void instanceConfigDoesNotExistAtStartup() {
    config.addConfiguration(new DynamicWatchedConfiguration(new DynamicZookeeperConfigurationSource(curatorFramework, CONFIG_BASE_PATH, node)));
    config.addConfiguration(defaultConfiguration);

    Assert.assertEquals(DEF_VAL1, config.getString(DEF_KEY1));
    Assert.assertEquals(DEF_VAL2, config.getString(DEF_KEY2));
}
 
Example #9
Source File: DynamicZookeeperConfigurationSourceTest.java    From chassis with Apache License 2.0 5 votes vote down vote up
@Test
public void instanceConfigAddedAfterStartup() throws Exception {
    String instanceConfigNode = CONFIG_BASE_PATH + "/" + node;
    Assert.assertNull(curatorFramework.checkExists().forPath(instanceConfigNode));

    config.addConfiguration(new DynamicWatchedConfiguration(new DynamicZookeeperConfigurationSource(curatorFramework, CONFIG_BASE_PATH, node)));
    config.addConfiguration(defaultConfiguration);

    Assert.assertEquals(DEF_VAL1, config.getString(DEF_KEY1));
    Assert.assertEquals(DEF_VAL2, config.getString(DEF_KEY2));

    //create the instance node
    curatorFramework.create().forPath(instanceConfigNode);

    Thread.sleep(1000);

    Assert.assertEquals(DEF_VAL1, config.getString(DEF_KEY1));
    Assert.assertEquals(DEF_VAL2, config.getString(DEF_KEY2));

    //create a property
    String key = instanceConfigNode + "/" + DEF_KEY1;
    String val = DEF_VAL1 + "--override";

    curatorFramework.create().forPath(key, val.getBytes());

    Thread.sleep(1000);

    Assert.assertEquals(val, config.getString(DEF_KEY1));
    Assert.assertEquals(DEF_VAL2, config.getString(DEF_KEY2));
}
 
Example #10
Source File: DynamicZookeeperConfigurationSourceTest.java    From chassis with Apache License 2.0 5 votes vote down vote up
@Test
public void instanceConfigPropertyAdded() throws Exception {
    curatorFramework.create().forPath(CONFIG_BASE_PATH + "/" + node);
    config.addConfiguration(new DynamicWatchedConfiguration(new DynamicZookeeperConfigurationSource(curatorFramework, CONFIG_BASE_PATH, node)));
    config.addConfiguration(defaultConfiguration);

    Assert.assertEquals(DEF_VAL1, config.getString(DEF_KEY1));

    curatorFramework.create().forPath(CONFIG_BASE_PATH + "/" + node + "/" + DEF_KEY1, "override".getBytes());

    Thread.sleep(1000);

    Assert.assertEquals("override", config.getString(DEF_KEY1));
}
 
Example #11
Source File: ZookeeperConfigurationProvider.java    From chassis with Apache License 2.0 4 votes vote down vote up
private AbstractConfiguration getServerInstanceSpecificApplicationConfiguration(String configRoot, String instanceConfigNode) {
    return
            new DynamicWatchedConfiguration(
                    new DynamicZookeeperConfigurationSource(curatorFramework, configRoot, instanceConfigNode));
}
 
Example #12
Source File: DynamicZookeeperConfigurationSourceTest.java    From chassis with Apache License 2.0 4 votes vote down vote up
@Test
public void instanceConfigDeletedAfterStartup() throws Exception {
    curatorFramework.create().forPath(CONFIG_BASE_PATH + "/" + node);
    config.addConfiguration(new DynamicWatchedConfiguration(new DynamicZookeeperConfigurationSource(curatorFramework, CONFIG_BASE_PATH, node)));
    config.addConfiguration(defaultConfiguration);

    Assert.assertEquals(DEF_VAL1, config.getString(DEF_KEY1));

    curatorFramework.create().forPath(CONFIG_BASE_PATH + "/" + node + "/" + DEF_KEY1, "override".getBytes());

    Thread.sleep(1000);

    Assert.assertEquals("override", config.getString(DEF_KEY1));

    curatorFramework.delete().forPath(CONFIG_BASE_PATH + "/" + node + "/" + DEF_KEY1);
    curatorFramework.delete().forPath(CONFIG_BASE_PATH + "/" + node);

    Thread.sleep(1000);

    Assert.assertEquals(DEF_VAL1, config.getString(DEF_KEY1));
}
 
Example #13
Source File: DynamicZookeeperConfigurationSourceTest.java    From chassis with Apache License 2.0 4 votes vote down vote up
@Test
public void instanceConfigPropertyDeleted() throws Exception {
    curatorFramework.create().forPath(CONFIG_BASE_PATH + "/" + node);
    config.addConfiguration(new DynamicWatchedConfiguration(new DynamicZookeeperConfigurationSource(curatorFramework, CONFIG_BASE_PATH, node)));
    config.addConfiguration(defaultConfiguration);

    Assert.assertEquals(DEF_VAL1, config.getString(DEF_KEY1));

    curatorFramework.create().forPath(CONFIG_BASE_PATH + "/" + node + "/" + DEF_KEY1, "override".getBytes());

    Thread.sleep(1000);

    Assert.assertEquals("override", config.getString(DEF_KEY1));

    curatorFramework.delete().forPath(CONFIG_BASE_PATH + "/" + node + "/" + DEF_KEY1);

    Thread.sleep(1000);

    Assert.assertEquals(DEF_VAL1, config.getString(DEF_KEY1));
}
 
Example #14
Source File: DynamicZookeeperConfigurationSourceTest.java    From chassis with Apache License 2.0 4 votes vote down vote up
@Test
public void instanceConfigPropertyUpdated() throws Exception {
    curatorFramework.create().forPath(CONFIG_BASE_PATH + "/" + node);
    config.addConfiguration(new DynamicWatchedConfiguration(new DynamicZookeeperConfigurationSource(curatorFramework, CONFIG_BASE_PATH, node)));
    config.addConfiguration(defaultConfiguration);

    Assert.assertEquals(DEF_VAL1, config.getString(DEF_KEY1));

    curatorFramework.create().forPath(CONFIG_BASE_PATH + "/" + node + "/" + DEF_KEY1, "override".getBytes());

    Thread.sleep(1000);

    Assert.assertEquals("override", config.getString(DEF_KEY1));

    curatorFramework.setData().forPath(CONFIG_BASE_PATH + "/" + node + "/" + DEF_KEY1, "override2".getBytes());

    Thread.sleep(1000);

    Assert.assertEquals("override2", config.getString(DEF_KEY1));

}
 
Example #15
Source File: DynamicZookeeperConfigurationSourceTest.java    From chassis with Apache License 2.0 4 votes vote down vote up
@Test
public void instanceConfigExistsAtStartup() throws Exception {
    curatorFramework.create().forPath(CONFIG_BASE_PATH + "/" + node);
    String key = CONFIG_BASE_PATH + "/" + node + "/" + DEF_KEY1;

    curatorFramework.create().forPath(key, "val1-override".getBytes());

    Assert.assertNotNull(curatorFramework.checkExists().forPath(key));

    config.addConfiguration(new DynamicWatchedConfiguration(new DynamicZookeeperConfigurationSource(curatorFramework, CONFIG_BASE_PATH, node)));
    config.addConfiguration(defaultConfiguration);

    Assert.assertEquals("val1-override", config.getString(DEF_KEY1));
    Assert.assertEquals(DEF_VAL2, config.getString(DEF_KEY2));

}
 
Example #16
Source File: TestConfigUtil.java    From servicecomb-java-chassis with Apache License 2.0 4 votes vote down vote up
@Test
public void testCreateDynamicConfigHasConfigCenter() {
  AbstractConfiguration dynamicConfig = ConfigUtil.createDynamicConfig();
  Assert.assertEquals(DynamicWatchedConfiguration.class,
      ((ConcurrentCompositeConfiguration) dynamicConfig).getConfiguration(0).getClass());
}
 
Example #17
Source File: DynamicZookeeperConfigurationSourceTest.java    From chassis with Apache License 2.0 3 votes vote down vote up
@Test
public void instanceConfigDeletedAndReadded() throws Exception {
    curatorFramework.create().forPath(CONFIG_BASE_PATH + "/" + node);
    config.addConfiguration(new DynamicWatchedConfiguration(new DynamicZookeeperConfigurationSource(curatorFramework, CONFIG_BASE_PATH, node)));
    config.addConfiguration(defaultConfiguration);

    Assert.assertEquals(DEF_VAL1, config.getString(DEF_KEY1));

    curatorFramework.create().forPath(CONFIG_BASE_PATH + "/" + node + "/" + DEF_KEY1, "override".getBytes());

    Thread.sleep(1000);

    Assert.assertEquals("override", config.getString(DEF_KEY1));

    curatorFramework.delete().forPath(CONFIG_BASE_PATH + "/" + node + "/" + DEF_KEY1);
    curatorFramework.delete().forPath(CONFIG_BASE_PATH + "/" + node);

    Thread.sleep(1000);

    Assert.assertEquals(DEF_VAL1, config.getString(DEF_KEY1));

    curatorFramework.create().forPath(CONFIG_BASE_PATH + "/" + node);

    Thread.sleep(1000);

    curatorFramework.create().forPath(CONFIG_BASE_PATH + "/" + node + "/" + DEF_KEY1, "override".getBytes());

    Thread.sleep(1000);

    Assert.assertEquals("override", config.getString(DEF_KEY1));
}