com.ctrip.framework.apollo.model.ConfigChange Java Examples

The following examples show how to use com.ctrip.framework.apollo.model.ConfigChange. 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: SimpleConfig.java    From apollo with Apache License 2.0 6 votes vote down vote up
@Override
public synchronized void onRepositoryChange(String namespace, Properties newProperties) {
  if (newProperties.equals(m_configProperties)) {
    return;
  }
  Properties newConfigProperties = propertiesFactory.getPropertiesInstance();
  newConfigProperties.putAll(newProperties);

  List<ConfigChange> changes = calcPropertyChanges(namespace, m_configProperties, newConfigProperties);
  Map<String, ConfigChange> changeMap = Maps.uniqueIndex(changes,
      new Function<ConfigChange, String>() {
        @Override
        public String apply(ConfigChange input) {
          return input.getPropertyName();
        }
      });

  updateConfig(newConfigProperties, m_configRepository.getSourceType());
  clearConfigCache();

  this.fireConfigChange(new ConfigChangeEvent(m_namespace, changeMap));

  Tracer.logEvent("Apollo.Client.ConfigChanges", m_namespace);
}
 
Example #2
Source File: DefaultConfig.java    From apollo with Apache License 2.0 6 votes vote down vote up
@Override
public synchronized void onRepositoryChange(String namespace, Properties newProperties) {
  if (newProperties.equals(m_configProperties.get())) {
    return;
  }

  ConfigSourceType sourceType = m_configRepository.getSourceType();
  Properties newConfigProperties = propertiesFactory.getPropertiesInstance();
  newConfigProperties.putAll(newProperties);

  Map<String, ConfigChange> actualChanges = updateAndCalcConfigChanges(newConfigProperties, sourceType);

  //check double checked result
  if (actualChanges.isEmpty()) {
    return;
  }

  this.fireConfigChange(new ConfigChangeEvent(m_namespace, actualChanges));

  Tracer.logEvent("Apollo.Client.ConfigChanges", m_namespace);
}
 
Example #3
Source File: JavaConfigAnnotationTest.java    From apollo with Apache License 2.0 6 votes vote down vote up
@Test
public void testApolloConfigChangeListenerWithYamlFile() throws Exception {
  String someKey = "someKey";
  String someValue = "someValue";
  String anotherValue = "anotherValue";

  YamlConfigFile configFile = prepareYamlConfigFile(APPLICATION_YAML_NAMESPACE,
      readYamlContentAsConfigFileProperties("case9.yml"));

  TestApolloConfigChangeListenerWithYamlFile bean = getBean(TestApolloConfigChangeListenerWithYamlFile.class, AppConfig9.class);

  Config yamlConfig = bean.getYamlConfig();
  SettableFuture<ConfigChangeEvent> future = bean.getConfigChangeEventFuture();

  assertEquals(someValue, yamlConfig.getProperty(someKey, null));
  assertFalse(future.isDone());

  configFile.onRepositoryChange(APPLICATION_YAML_NAMESPACE, readYamlContentAsConfigFileProperties("case9-new.yml"));

  ConfigChangeEvent configChangeEvent = future.get(100, TimeUnit.MILLISECONDS);
  ConfigChange change = configChangeEvent.getChange(someKey);
  assertEquals(someValue, change.getOldValue());
  assertEquals(anotherValue, change.getNewValue());

  assertEquals(anotherValue, yamlConfig.getProperty(someKey, null));
}
 
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: ApolloCenterRepository.java    From shardingsphere with Apache License 2.0 6 votes vote down vote up
@Override
public void watch(final String key, final DataChangedEventListener dataChangedEventListener) {
    String apolloKey = ConfigKeyUtils.pathToKey(key);
    caches.put(apolloKey, dataChangedEventListener);
    ConfigChangeListener listener = changeEvent -> {
        for (String changeKey : changeEvent.changedKeys()) {
            ConfigChange change = changeEvent.getChange(changeKey);
            DataChangedEvent.ChangedType changedType = getChangedType(change.getChangeType());
            if (DataChangedEvent.ChangedType.IGNORED == changedType) {
                continue;
            }
            if (caches.get(changeKey) == null) {
                continue;
            }
            caches.get(changeKey).onChange(new DataChangedEvent(ConfigKeyUtils.keyToPath(changeKey), change.getNewValue(), changedType));
        }
    };
    configWrapper.addChangeListener(listener, Collections.singleton(apolloKey), Collections.singleton(apolloKey));
}
 
Example #6
Source File: ApolloConfigLoader.java    From tunnel with Apache License 2.0 5 votes vote down vote up
@Override
public void onChange(ConfigChangeEvent changeEvent) {
    Set<String> keys = changeEvent.changedKeys();
    for (String key : keys) {
        ConfigChange change = changeEvent.getChange(key);
        if (change == null) {
            continue;
        }

        String oldValue = change.getOldValue();
        String newValue = change.getNewValue();
        configListener.onChange(key, oldValue, newValue);
    }
}
 
Example #7
Source File: ApolloDataSource.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
private void initializeConfigChangeListener() {
    configChangeListener = new ConfigChangeListener() {
        @Override
        public void onChange(ConfigChangeEvent changeEvent) {
            ConfigChange change = changeEvent.getChange(ruleKey);
            //change is never null because the listener will only notify for this key
            if (change != null) {
                RecordLog.info("[ApolloDataSource] Received config changes: " + change.toString());
            }
            loadAndUpdateRules();
        }
    };
    config.addChangeListener(configChangeListener, Sets.newHashSet(ruleKey));
}
 
Example #8
Source File: ApolloConfigLoader.java    From tunnel with Apache License 2.0 5 votes vote down vote up
@Override
public void onChange(ConfigChangeEvent changeEvent) {
    Set<String> keys = changeEvent.changedKeys();
    for (String key : keys) {
        ConfigChange change = changeEvent.getChange(key);
        if (change == null) {
            continue;
        }

        String oldValue = change.getOldValue();
        String newValue = change.getNewValue();
        configListener.onChange(key, oldValue, newValue);
    }
}
 
Example #9
Source File: ApolloConfig.java    From java-tutorial with MIT License 5 votes vote down vote up
@ApolloConfigChangeListener(ConfigConsts.NAMESPACE_APPLICATION)
public void onChange(ConfigChangeEvent changeEvent) {
    logger.info("before refresh");
    for (String changedKey : changeEvent.changedKeys()) {
        logger.info("===============================================================");
        logger.info("changedKey:{} value:{}", changedKey, changeEvent.getChange(changedKey));
        ConfigChange configChange = changeEvent.getChange(changedKey);
        configChange.getOldValue();
    }
    refreshScope.refreshAll();
    logger.info("after refresh");
}
 
Example #10
Source File: ApolloDataSource.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
private void initializeConfigChangeListener() {
    configChangeListener = new ConfigChangeListener() {
        @Override
        public void onChange(ConfigChangeEvent changeEvent) {
            ConfigChange change = changeEvent.getChange(ruleKey);
            //change is never null because the listener will only notify for this key
            if (change != null) {
                RecordLog.info("[ApolloDataSource] Received config changes: " + change.toString());
            }
            loadAndUpdateRules();
        }
    };
    config.addChangeListener(configChangeListener, Sets.newHashSet(ruleKey));
}
 
Example #11
Source File: ApolloConfigManager.java    From jboot with Apache License 2.0 5 votes vote down vote up
public void init(JbootConfigManager configManager) {

        ApolloServerConfig apolloServerConfig = configManager.get(ApolloServerConfig.class);
        if (!apolloServerConfig.isEnable() || !apolloServerConfig.isConfigOk()){
            return;
        }

        Config config = getDefaultConfig();

        Set<String> propNames = config.getPropertyNames();
        if (propNames != null && !propNames.isEmpty()) {
            for (String name : propNames) {
                String value = config.getProperty(name, null);
                configManager.setRemoteProperty(name, value);
            }
        }

        config.addChangeListener(changeEvent -> {
            for (String key : changeEvent.changedKeys()) {
                ConfigChange change = changeEvent.getChange(key);
                configManager.setRemoteProperty(change.getPropertyName(), change.getNewValue());
            }

            configManager.notifyChangeListeners(changeEvent.changedKeys());
        });

    }
 
Example #12
Source File: FlinkApolloTest.java    From flink-learning with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    env.getConfig().setGlobalJobParameters(ParameterTool.fromArgs(args));
    env.setParallelism(1);

    env.addSource(new RichSourceFunction<String>() {

        private Config config;

        @Override
        public void open(Configuration parameters) throws Exception {
            super.open(parameters);
            config = ConfigService.getAppConfig();
            config.addChangeListener(new ConfigChangeListener() {
                @Override
                public void onChange(ConfigChangeEvent configChangeEvent) {
                    for (String key : configChangeEvent.changedKeys()) {
                        ConfigChange change = configChangeEvent.getChange(key);
                        log.info("Change - key: {}, oldValue: {}, newValue: {}, changeType: {}",
                                change.getPropertyName(), change.getOldValue(), change.getNewValue(),
                                change.getChangeType());
                    }
                }
            });
        }

        @Override
        public void run(SourceContext<String> ctx) throws Exception {
            while (true) {
                ctx.collect(config.getProperty("name", "zhisheng"));
                Thread.sleep(3000);
            }
        }

        @Override
        public void cancel() {
        }
    }).print();

    env.execute("zhisheng flink Apollo");
}
 
Example #13
Source File: FlinkApolloTest.java    From flink-learning with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    env.getConfig().setGlobalJobParameters(ParameterTool.fromArgs(args));
    env.setParallelism(1);

    env.addSource(new RichSourceFunction<String>() {

        private Config config;

        @Override
        public void open(Configuration parameters) throws Exception {
            super.open(parameters);
            config = ConfigService.getAppConfig();
            config.addChangeListener(new ConfigChangeListener() {
                @Override
                public void onChange(ConfigChangeEvent configChangeEvent) {
                    for (String key : configChangeEvent.changedKeys()) {
                        ConfigChange change = configChangeEvent.getChange(key);
                        log.info("Change - key: {}, oldValue: {}, newValue: {}, changeType: {}",
                                change.getPropertyName(), change.getOldValue(), change.getNewValue(),
                                change.getChangeType());
                    }
                }
            });
        }

        @Override
        public void run(SourceContext<String> ctx) throws Exception {
            while (true) {
                ctx.collect(config.getProperty("name", "zhisheng"));
                Thread.sleep(3000);
            }
        }

        @Override
        public void cancel() {
        }
    }).print();

    env.execute("zhisheng flink Apollo");
}
 
Example #14
Source File: AbstractConfig.java    From apollo with Apache License 2.0 4 votes vote down vote up
List<ConfigChange> calcPropertyChanges(String namespace, Properties previous,
                                       Properties current) {
  if (previous == null) {
    previous = propertiesFactory.getPropertiesInstance();
  }

  if (current == null) {
    current =  propertiesFactory.getPropertiesInstance();
  }

  Set<String> previousKeys = previous.stringPropertyNames();
  Set<String> currentKeys = current.stringPropertyNames();

  Set<String> commonKeys = Sets.intersection(previousKeys, currentKeys);
  Set<String> newKeys = Sets.difference(currentKeys, commonKeys);
  Set<String> removedKeys = Sets.difference(previousKeys, commonKeys);

  List<ConfigChange> changes = Lists.newArrayList();

  for (String newKey : newKeys) {
    changes.add(new ConfigChange(namespace, newKey, null, current.getProperty(newKey),
        PropertyChangeType.ADDED));
  }

  for (String removedKey : removedKeys) {
    changes.add(new ConfigChange(namespace, removedKey, previous.getProperty(removedKey), null,
        PropertyChangeType.DELETED));
  }

  for (String commonKey : commonKeys) {
    String previousValue = previous.getProperty(commonKey);
    String currentValue = current.getProperty(commonKey);
    if (Objects.equal(previousValue, currentValue)) {
      continue;
    }
    changes.add(new ConfigChange(namespace, commonKey, previousValue, currentValue,
        PropertyChangeType.MODIFIED));
  }

  return changes;
}
 
Example #15
Source File: GatewayPropertiesRefresher.java    From apollo-use-cases with Apache License 2.0 3 votes vote down vote up
/***
 * 根据changeEvent和定义的pattern匹配key,如果所有对应PropertyChangeType为DELETED则需要清空GatewayProperties里相关集合
 *
 * @param changeEvent
 * @param pattern
 * @param existSize
 * @return boolean
 * @author ksewen
 * @date 2019/5/23 2:18 PM
 */
private boolean checkNeedClear(ConfigChangeEvent changeEvent, String pattern, int existSize) {

    return changeEvent.changedKeys().stream().filter(key -> key.matches(pattern))
            .filter(key -> {
                ConfigChange change = changeEvent.getChange(key);
                return PropertyChangeType.DELETED.equals(change.getChangeType());
            }).count() == existSize;
}