use of com.tencent.polaris.configuration.api.core.ConfigKVFile in project polaris-java by polarismesh.
the class DefaultConfigFileManager method getConfigKVFile.
@Override
public ConfigKVFile getConfigKVFile(ConfigFileMetadata configFileMetadata, ConfigFileFormat fileFormat) {
ConfigKVFile configFile = configPropertiesFileCache.get(configFileMetadata);
if (configFile == null) {
synchronized (this) {
configFile = configPropertiesFileCache.get(configFileMetadata);
if (configFile == null) {
ConfigFileFactory configFileFactory = configFileFactoryManager.getFactory(configFileMetadata);
configFile = configFileFactory.createConfigKVFile(configFileMetadata, fileFormat);
configPropertiesFileCache.put(configFileMetadata, configFile);
}
}
}
return configFile;
}
use of com.tencent.polaris.configuration.api.core.ConfigKVFile 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));
}
}
use of com.tencent.polaris.configuration.api.core.ConfigKVFile in project spring-cloud-tencent by Tencent.
the class PolarisConfigFileLocator method loadPolarisPropertySource.
private PolarisPropertySource loadPolarisPropertySource(String namespace, String group, String fileName) {
ConfigKVFile configKVFile;
// unknown extension is resolved as properties file
if (ConfigFileFormat.isPropertyFile(fileName) || ConfigFileFormat.isUnknownFile(fileName)) {
configKVFile = configFileService.getConfigPropertiesFile(namespace, group, fileName);
} else if (ConfigFileFormat.isYamlFile(fileName)) {
configKVFile = configFileService.getConfigYamlFile(namespace, group, fileName);
} else {
LOGGER.warn("[SCT Config] Unsupported config file. namespace = {}, group = {}, fileName = {}", namespace, group, fileName);
throw new IllegalStateException("Only configuration files in the format of properties / yaml / yaml" + " can be injected into the spring context");
}
Map<String, Object> map = new ConcurrentHashMap<>();
for (String key : configKVFile.getPropertyNames()) {
map.put(key, configKVFile.getProperty(key, null));
}
return new PolarisPropertySource(namespace, group, fileName, configKVFile, map);
}
use of com.tencent.polaris.configuration.api.core.ConfigKVFile in project polaris-java by polarismesh.
the class ConfigFileManagerTest method testGetConfigPropertiesFile.
@Test
public void testGetConfigPropertiesFile() {
ConfigFileMetadata configFileMetadata = ConfigFileTestUtils.assembleDefaultConfigFileMeta();
ConfigKVFile mockedConfigFile = mock(ConfigKVFile.class);
when(configFileFactoryManager.getFactory(any())).thenReturn(configFileFactory);
when(configFileFactory.createConfigKVFile(configFileMetadata, ConfigFileFormat.Properties)).thenReturn(mockedConfigFile);
// 第一次获取
ConfigKVFile configFile = defaultConfigFileManager.getConfigKVFile(configFileMetadata, ConfigFileFormat.Properties);
verify(configFileFactoryManager).getFactory(configFileMetadata);
verify(configFileFactory).createConfigKVFile(configFileMetadata, ConfigFileFormat.Properties);
Assert.assertEquals(mockedConfigFile, configFile);
// 第二次获取,经过缓存
ConfigKVFile configFile2 = defaultConfigFileManager.getConfigKVFile(configFileMetadata, ConfigFileFormat.Properties);
verify(configFileFactoryManager).getFactory(configFileMetadata);
verify(configFileFactory).createConfigKVFile(configFileMetadata, ConfigFileFormat.Properties);
Assert.assertEquals(mockedConfigFile, configFile2);
}
use of com.tencent.polaris.configuration.api.core.ConfigKVFile 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));
}
}
Aggregations