ApolloConfigDemo.java 3.91 KB
Newer Older
Jason Song committed
1 2
import com.google.common.base.Charsets;

3 4
import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigChangeListener;
5
import com.ctrip.framework.apollo.ConfigFile;
6
import com.ctrip.framework.apollo.ConfigService;
7
import com.ctrip.framework.apollo.core.enums.ConfigFileFormat;
8 9
import com.ctrip.framework.apollo.model.ConfigChange;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
Jason Song committed
10
import com.ctrip.framework.foundation.Foundation;
11 12 13

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
14 15 16 17 18 19 20 21

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @author Jason Song(song_s@ctrip.com)
 */
Jason Song committed
22
public class ApolloConfigDemo {
23
  private static final Logger logger = LoggerFactory.getLogger(ApolloConfigDemo.class);
24
  private String DEFAULT_VALUE = "undefined";
25
  private Config config;
26
  private Config publicConfig;
27 28
  private ConfigFile applicationConfigFile;
  private ConfigFile xmlConfigFile;
29 30

  public ApolloConfigDemo() {
31
    ConfigChangeListener changeListener = new ConfigChangeListener() {
32 33
      @Override
      public void onChange(ConfigChangeEvent changeEvent) {
Jason Song committed
34
        logger.info("Changes for namespace {}", changeEvent.getNamespace());
35 36
        for (String key : changeEvent.changedKeys()) {
          ConfigChange change = changeEvent.getChange(key);
Jason Song committed
37
          logger.info("Change - key: {}, oldValue: {}, newValue: {}, changeType: {}",
38 39 40 41
              change.getPropertyName(), change.getOldValue(), change.getNewValue(),
              change.getChangeType());
        }
      }
42 43 44 45 46
    };
    config = ConfigService.getAppConfig();
    config.addChangeListener(changeListener);
    publicConfig = ConfigService.getConfig("FX.apollo");
    publicConfig.addChangeListener(changeListener);
47 48
    applicationConfigFile = ConfigService.getConfigFile("application", ConfigFileFormat.Properties);
    xmlConfigFile = ConfigService.getConfigFile("datasources", ConfigFileFormat.XML);
49 50 51
  }

  private String getConfig(String key) {
52 53 54 55 56
    String result = config.getProperty(key, DEFAULT_VALUE);
    if (DEFAULT_VALUE.equals(result)) {
      result = publicConfig.getProperty(key, DEFAULT_VALUE);
    }
    logger.info(String.format("Loading key : %s with value: %s", key, result));
57 58 59
    return result;
  }

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
  private void print(String namespace) {
    switch (namespace) {
      case "application":
        print(applicationConfigFile);
        return;
      case "xml":
        print(xmlConfigFile);
        return;
    }
  }

  private void print(ConfigFile configFile) {
    if (!configFile.hasContent()) {
      System.out.println("No config file content found for " + configFile.getNamespace());
      return;
    }
    System.out.println("=== Config File Content for " + configFile.getNamespace() + " is as follows: ");
    System.out.println(configFile.getContent());
  }

Jason Song committed
80 81 82 83 84 85 86
  private void printEnvInfo() {
    String message = String.format("AppId: %s, Env: %s, DC: %s, IP: %s", Foundation.app()
        .getAppId(), Foundation.server().getEnvType(), Foundation.server().getDataCenter(),
        Foundation.net().getHostAddress());
    System.out.println(message);
  }

87 88
  public static void main(String[] args) throws IOException {
    ApolloConfigDemo apolloConfigDemo = new ApolloConfigDemo();
Jason Song committed
89
    apolloConfigDemo.printEnvInfo();
90 91 92 93
    System.out.println(
        "Apollo Config Demo. Please input key to get the value.");
    while (true) {
      System.out.print("> ");
Jason Song committed
94
      String input = new BufferedReader(new InputStreamReader(System.in, Charsets.UTF_8)).readLine();
Jason Song committed
95
      if (input == null || input.length() == 0) {
96
        continue;
97 98
      }
      input = input.trim();
99 100 101 102 103 104 105 106
      if (input.equalsIgnoreCase("application")) {
        apolloConfigDemo.print("application");
        continue;
      }
      if (input.equalsIgnoreCase("xml")) {
        apolloConfigDemo.print("xml");
        continue;
      }
107 108 109 110 111 112 113
      if (input.equalsIgnoreCase("quit")) {
        System.exit(0);
      }
      apolloConfigDemo.getConfig(input);
    }
  }
}