Search in sources :

Example 1 with ConfigFileService

use of com.tencent.polaris.configuration.api.core.ConfigFileService in project polaris-java by polarismesh.

the class ConfigPropertiesFileExample method main.

public static void main(String[] args) throws IOException {
    String namespace = "dev";
    String fileGroup = "myGroup";
    String fileName = "application.properties";
    // 创建配置中心服务类,一般情况下只需要单例对象
    ConfigFileService configFileService = ConfigFileServiceFactory.createConfigFileService();
    // 获取 properties 格式配置文件对象
    ConfigKVFile configFile = configFileService.getConfigPropertiesFile(namespace, fileGroup, fileName);
    // 获取配置文件完整内容
    Utils.print(configFile.getContent());
    // 获取特定的 key 的值
    Utils.print(configFile.getProperty("key1", "some default value"));
    // 更多基础类型方法
    // getIntProperty、getFloatProperty ...
    // 更多高级数据结构方法
    // getEnumProperty、getArrayProperty、getJsonProperty
    // 获取 Properties
    // asProperties
    // 监听变更事件,kv类型的变更事件可以细化到 key 粒度的变更
    configFile.addChangeListener(new ConfigKVFileChangeListener() {

        @Override
        public void onChange(ConfigKVFileChangeEvent event) {
            for (String key : event.changedKeys()) {
                ConfigPropertyChangeInfo changeInfo = event.getChangeInfo(key);
                System.out.printf("\nChange info :key = %s, old value = %s, new value = %s, change type = %s\n%n", changeInfo.getPropertyName(), changeInfo.getOldValue(), changeInfo.getNewValue(), changeInfo.getChangeType());
            }
        }
    });
    System.err.println("Please input key to get the value. Input quit to exit.");
    while (true) {
        System.out.print("Input key > ");
        String input = new BufferedReader(new InputStreamReader(System.in, Charsets.UTF_8)).readLine();
        if (input == null || input.length() == 0) {
            continue;
        }
        input = input.trim();
        if ("quit".equalsIgnoreCase(input)) {
            System.exit(0);
        }
        Utils.print(configFile.getProperty(input, null));
    }
}
Also used : ConfigPropertyChangeInfo(com.tencent.polaris.configuration.api.core.ConfigPropertyChangeInfo) InputStreamReader(java.io.InputStreamReader) ConfigKVFileChangeEvent(com.tencent.polaris.configuration.api.core.ConfigKVFileChangeEvent) BufferedReader(java.io.BufferedReader) ConfigKVFile(com.tencent.polaris.configuration.api.core.ConfigKVFile) ConfigKVFileChangeListener(com.tencent.polaris.configuration.api.core.ConfigKVFileChangeListener) ConfigFileService(com.tencent.polaris.configuration.api.core.ConfigFileService)

Example 2 with ConfigFileService

use of com.tencent.polaris.configuration.api.core.ConfigFileService in project polaris-java by polarismesh.

the class ConfigFileExample method main.

public static void main(String[] args) throws Exception {
    String namespace = "dev";
    String fileGroup = "myGroup";
    String fileName = "application.properties";
    // 创建配置中心服务类,一般情况下只需要单例对象
    ConfigFileService configFileService = ConfigFileServiceFactory.createConfigFileService();
    // 获取配置文件
    ConfigFile configFile = configFileService.getConfigFile(namespace, fileGroup, fileName);
    // 打印配置文件内容
    Utils.print(configFile.getContent());
    // 添加变更监听器
    configFile.addChangeListener(new ConfigFileChangeListener() {

        @Override
        public void onChange(ConfigFileChangeEvent event) {
            System.out.printf("Received config file change event. old value = %s, new value = %s, change type = %s%n", event.getOldValue(), event.getNewValue(), event.getChangeType());
            // 获取配置文件最新内容
            Utils.print(configFile.getContent());
        }
    });
    // 更多 API 用法
    // User user = configFile.asJson(User.class, null);  自动反序列化配置文件成 JSON 对象
    // List<User> users = configFile.asJson(new TypeToken<List<User>>() {}.getType(), null)
    System.in.read();
}
Also used : ConfigFile(com.tencent.polaris.configuration.api.core.ConfigFile) ConfigFileChangeEvent(com.tencent.polaris.configuration.api.core.ConfigFileChangeEvent) ConfigFileChangeListener(com.tencent.polaris.configuration.api.core.ConfigFileChangeListener) ConfigFileService(com.tencent.polaris.configuration.api.core.ConfigFileService)

Example 3 with ConfigFileService

use of com.tencent.polaris.configuration.api.core.ConfigFileService in project polaris-java by polarismesh.

the class ConfigYamlFileExample method main.

public static void main(String[] args) throws IOException {
    String namespace = "dev";
    String fileGroup = "myGroup";
    // 文件名通过 / 分割在管控端按目录格式展示
    String fileName = "root/bootstrap.yaml";
    // 创建配置中心服务类,一般情况下只需要单例对象
    ConfigFileService configFileService = ConfigFileServiceFactory.createConfigFileService();
    // 获取 yaml 格式配置文件对象,这里是唯一跟 properties 格式区别的地方
    ConfigKVFile configFile = configFileService.getConfigYamlFile(namespace, fileGroup, fileName);
    // 获取配置文件完整内容
    Utils.print(configFile.getContent());
    // 获取特定的 key 的值
    Utils.print(configFile.getProperty("key1", "some default value"));
    // 更多基础类型方法
    // configFile.getIntProperty()、configFile.getFloatProperty() ...
    // 更多高级数据结构方法
    // configFile.getEnumProperty()、configFile.getArrayProperty()、configFile.getJsonProperty()
    // 获取 Properties
    // configFile.asProperties()
    // 监听变更事件,kv类型的变更事件可以细化到 key 粒度的变更
    configFile.addChangeListener(new ConfigKVFileChangeListener() {

        @Override
        public void onChange(ConfigKVFileChangeEvent event) {
            for (String key : event.changedKeys()) {
                ConfigPropertyChangeInfo changeInfo = event.getChangeInfo(key);
                System.out.printf("\nChange info :key = %s, old value = %s, new value = %s, change type = %s\n%n", changeInfo.getPropertyName(), changeInfo.getOldValue(), changeInfo.getNewValue(), changeInfo.getChangeType());
            }
        }
    });
    System.err.println("Please input key to get the value. Input quit to exit.");
    while (true) {
        System.out.print("Input key > ");
        String input = new BufferedReader(new InputStreamReader(System.in, Charsets.UTF_8)).readLine();
        if (input == null || input.length() == 0) {
            continue;
        }
        input = input.trim();
        if ("quit".equalsIgnoreCase(input)) {
            System.exit(0);
        }
        Utils.print(configFile.getProperty(input, null));
    }
}
Also used : ConfigPropertyChangeInfo(com.tencent.polaris.configuration.api.core.ConfigPropertyChangeInfo) InputStreamReader(java.io.InputStreamReader) ConfigKVFileChangeEvent(com.tencent.polaris.configuration.api.core.ConfigKVFileChangeEvent) BufferedReader(java.io.BufferedReader) ConfigKVFile(com.tencent.polaris.configuration.api.core.ConfigKVFile) ConfigKVFileChangeListener(com.tencent.polaris.configuration.api.core.ConfigKVFileChangeListener) ConfigFileService(com.tencent.polaris.configuration.api.core.ConfigFileService)

Aggregations

ConfigFileService (com.tencent.polaris.configuration.api.core.ConfigFileService)3 ConfigKVFile (com.tencent.polaris.configuration.api.core.ConfigKVFile)2 ConfigKVFileChangeEvent (com.tencent.polaris.configuration.api.core.ConfigKVFileChangeEvent)2 ConfigKVFileChangeListener (com.tencent.polaris.configuration.api.core.ConfigKVFileChangeListener)2 ConfigPropertyChangeInfo (com.tencent.polaris.configuration.api.core.ConfigPropertyChangeInfo)2 BufferedReader (java.io.BufferedReader)2 InputStreamReader (java.io.InputStreamReader)2 ConfigFile (com.tencent.polaris.configuration.api.core.ConfigFile)1 ConfigFileChangeEvent (com.tencent.polaris.configuration.api.core.ConfigFileChangeEvent)1 ConfigFileChangeListener (com.tencent.polaris.configuration.api.core.ConfigFileChangeListener)1