Skip to content

Commit

Permalink
feature: compatible with file.conf and registry.conf configurations (a…
Browse files Browse the repository at this point in the history
  • Loading branch information
funky-eyes committed Jul 15, 2023
1 parent 02ec367 commit 12c27ff
Show file tree
Hide file tree
Showing 17 changed files with 274 additions and 53 deletions.
1 change: 1 addition & 0 deletions changes/en-us/2.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ The version is updated as follows:
- [[#5529](https://github.com/seata/seata/pull/5529)] docker image supports JVM parameter injection
- [[#3887](https://github.com/seata/seata/pull/3887)] add SQL Server database support in AT mode
- [[#4033](https://github.com/seata/seata/pull/4033)] add SQLServer support for Server DB storage mode
- [[#5717](https://github.com/seata/seata/pull/5717)] compatible with file.conf and registry.conf configurations in version 1.4.2 and below

### bugfix:
- [[#5677](https://github.com/seata/seata/pull/5677)] fix saga mode serviceTask inputParams json autoType convert exception
Expand Down
1 change: 1 addition & 0 deletions changes/zh-cn/2.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Seata 是一款开源的分布式事务解决方案,提供高性能和简单
- [[#5529](https://github.com/seata/seata/pull/5529)] docker镜像支持注入JVM参数到容器
- [[#3887](https://github.com/seata/seata/pull/3887)] 增加AT模式的SQLServer数据库支持
- [[#4033](https://github.com/seata/seata/pull/4033)] 增加ServerDB存储模式的SQLServer支持
- [[#5717](https://github.com/seata/seata/pull/5717)] 兼容1.4.2及以下版本的file.conf/registry.conf配置

### bugfix:
- [[#5677](https://github.com/seata/seata/pull/5677)] 修复saga模式下serviceTask入参autoType转化失败问题
Expand Down
17 changes: 17 additions & 0 deletions common/src/main/java/io/seata/common/ConfigurationKeys.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public interface ConfigurationKeys {
* The constant FILE_ROOT_REGISTRY.
*/
String FILE_ROOT_REGISTRY = "registry";

/**
* The constant FILE_ROOT_CONFIG.
*/
Expand All @@ -37,6 +38,22 @@ public interface ConfigurationKeys {
* The constant FILE_CONFIG_SPLIT_CHAR.
*/
String FILE_CONFIG_SPLIT_CHAR = ".";

/**
* The constant FILE_ROOT_PREFIX_REGISTRY.
*/
String FILE_ROOT_PREFIX_REGISTRY = FILE_ROOT_REGISTRY + FILE_CONFIG_SPLIT_CHAR;

/**
* The constant FILE_ROOT_PREFIX_CONFIG.
*/
String FILE_ROOT_PREFIX_CONFIG = FILE_ROOT_CONFIG + FILE_CONFIG_SPLIT_CHAR;

/**
* The constant SEATA_FILE_PREFIX_ROOT_CONFIG
*/
String SEATA_FILE_PREFIX_ROOT_CONFIG = SEATA_FILE_ROOT_CONFIG + FILE_CONFIG_SPLIT_CHAR;

/**
* The constant FILE_ROOT_TYPE.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package io.seata.config;

import java.util.Objects;
import java.util.Optional;

import io.seata.common.exception.NotSupportYetException;
import io.seata.common.loader.EnhancedServiceLoader;
Expand All @@ -42,32 +43,26 @@ public final class ConfigurationFactory {

private static final String ENV_SEATA_CONFIG_NAME = "SEATA_CONFIG_NAME";

public static Configuration CURRENT_FILE_INSTANCE;
public static volatile Configuration CURRENT_FILE_INSTANCE;

public static volatile FileConfiguration ORIGIN_FILE_INSTANCE_REGISTRY;

public static volatile FileConfiguration ORIGIN_FILE_INSTANCE = null;

static {
initOriginConfiguraction();
load();
maybeNeedOriginFileInstance();
}

private static void load() {
String seataConfigName = System.getProperty(SYSTEM_PROPERTY_SEATA_CONFIG_NAME);
if (seataConfigName == null) {
seataConfigName = System.getenv(ENV_SEATA_CONFIG_NAME);
}
if (seataConfigName == null) {
seataConfigName = REGISTRY_CONF_DEFAULT;
}
String envValue = System.getProperty(ENV_PROPERTY_KEY);
if (envValue == null) {
envValue = System.getenv(ENV_SYSTEM_KEY);
}
Configuration configuration = (envValue == null) ? new FileConfiguration(seataConfigName,
false) : new FileConfiguration(seataConfigName + "-" + envValue, false);
Configuration configuration = ORIGIN_FILE_INSTANCE_REGISTRY;
Configuration extConfiguration = null;
try {
extConfiguration = EnhancedServiceLoader.load(ExtConfigurationProvider.class).provide(configuration);
if (LOGGER.isInfoEnabled()) {
LOGGER.info("load Configuration from :{}", extConfiguration == null ?
configuration.getClass().getSimpleName() : "Spring Configuration");
LOGGER.info("load Configuration from :{}",
extConfiguration == null ? configuration.getClass().getSimpleName() : "Spring Configuration");
}
} catch (EnhancedServiceNotFoundException e) {
if (LOGGER.isDebugEnabled()) {
Expand All @@ -79,6 +74,27 @@ private static void load() {
CURRENT_FILE_INSTANCE = extConfiguration == null ? configuration : extConfiguration;
}

private static void initOriginConfiguraction() {
String seataConfigName = System.getProperty(SYSTEM_PROPERTY_SEATA_CONFIG_NAME);
if (seataConfigName == null) {
seataConfigName = System.getenv(ENV_SEATA_CONFIG_NAME);
}
if (seataConfigName == null) {
seataConfigName = REGISTRY_CONF_DEFAULT;
}
String envValue = System.getProperty(ENV_PROPERTY_KEY);
if (envValue == null) {
envValue = System.getenv(ENV_SYSTEM_KEY);
}
seataConfigName = envValue == null ? seataConfigName : seataConfigName + "-" + envValue;
// create FileConfiguration for read registry.conf
ORIGIN_FILE_INSTANCE_REGISTRY = new FileConfiguration(seataConfigName, false);
}

public static FileConfiguration getOriginFileInstanceRegistry() {
return ORIGIN_FILE_INSTANCE_REGISTRY;
}

private static final String NAME_KEY = "name";
private static final String FILE_TYPE = "file";

Expand All @@ -100,28 +116,39 @@ public static Configuration getInstance() {
return instance;
}

private static Configuration buildConfiguration() {
String configTypeName = CURRENT_FILE_INSTANCE.getConfig(
ConfigurationKeys.FILE_ROOT_CONFIG + ConfigurationKeys.FILE_CONFIG_SPLIT_CHAR
+ ConfigurationKeys.FILE_ROOT_TYPE);
LOGGER.info("use configuration center type: {}", configTypeName);
private static void maybeNeedOriginFileInstance() {
if (ConfigType.File == getConfigType()) {
String pathDataId = String.join(ConfigurationKeys.FILE_CONFIG_SPLIT_CHAR,
ConfigurationKeys.FILE_ROOT_CONFIG, FILE_TYPE, NAME_KEY);
String name = CURRENT_FILE_INSTANCE.getConfig(pathDataId);
// create FileConfiguration for read file.conf
ORIGIN_FILE_INSTANCE = new FileConfiguration(name);
}
}

private static ConfigType getConfigType() {
String configTypeName = CURRENT_FILE_INSTANCE.getConfig(ConfigurationKeys.FILE_ROOT_CONFIG
+ ConfigurationKeys.FILE_CONFIG_SPLIT_CHAR + ConfigurationKeys.FILE_ROOT_TYPE);
if (StringUtils.isBlank(configTypeName)) {
throw new NotSupportYetException("config type can not be null");
}
ConfigType configType = ConfigType.getType(configTypeName);
return ConfigType.getType(configTypeName);
}

public static Optional<FileConfiguration> getOriginFileInstance() {
return Optional.ofNullable(ORIGIN_FILE_INSTANCE);
}

private static Configuration buildConfiguration() {
ConfigType configType = getConfigType();
Configuration extConfiguration = null;
Configuration configuration;
if (ConfigType.File == configType) {
String pathDataId = String.join(ConfigurationKeys.FILE_CONFIG_SPLIT_CHAR,
ConfigurationKeys.FILE_ROOT_CONFIG, FILE_TYPE, NAME_KEY);
String name = CURRENT_FILE_INSTANCE.getConfig(pathDataId);
configuration = new FileConfiguration(name);
Configuration configuration = ORIGIN_FILE_INSTANCE;
if (configuration != null) {
try {
extConfiguration = EnhancedServiceLoader.load(ExtConfigurationProvider.class).provide(configuration);
if (LOGGER.isInfoEnabled()) {
LOGGER.info("load Configuration from :{}", extConfiguration == null ?
configuration.getClass().getSimpleName() : "Spring Configuration");
LOGGER.info("load Configuration from :{}",
extConfiguration == null ? configuration.getClass().getSimpleName() : "Spring Configuration");
}
} catch (EnhancedServiceNotFoundException ignore) {

Expand Down Expand Up @@ -152,7 +179,9 @@ private static Configuration buildConfiguration() {

protected static void reload() {
ConfigurationCache.clear();
initOriginConfiguraction();
load();
maybeNeedOriginFileInstance();
instance = null;
getInstance();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,10 @@ private void setFailResult(ConfigFuture configFuture) {

}

public FileConfig getFileConfig() {
return fileConfig;
}

/**
* The type FileListener.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package io.seata.config.file;

import java.util.Map;

/**
* @author wangwei-ying
Expand All @@ -26,5 +27,9 @@ public interface FileConfig {
*/
String getString(String path);

/**
* @return the all config
*/
Map<String, Object> getAllConfig();

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import io.seata.config.FileConfiguration;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
* @author wangwei-ying
Expand All @@ -50,4 +52,12 @@ public SimpleFileConfig(File file, String name) {
public String getString(String path) {
return fileConfig.getString(path);
}

@Override
public Map<String, Object> getAllConfig() {
return fileConfig.entrySet().stream().collect(HashMap::new, (m, e) ->
m.put(e.getKey(), e.getValue().unwrapped()),
HashMap::putAll);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
Expand All @@ -36,34 +37,44 @@
public class YamlFileConfig implements FileConfig {

private static final Logger LOGGER = LoggerFactory.getLogger(YamlFileConfig.class);
private Map configMap;
private final Map<String, Object> configMap = new HashMap<>();

public YamlFileConfig(File file, String name) throws IOException {
Yaml yaml = new Yaml();
try (InputStream is = new FileInputStream(file)) {
configMap = yaml.load(is);
flattenConfig("", yaml.loadAs(is, HashMap.class), configMap);
} catch (FileNotFoundException e) {
throw new IllegalArgumentException("file not found");
}
}

@Override
public String getString(String path) {
try {
Map config = configMap;
String[] dataId = path.split("\\.");
for (int i = 0; i < dataId.length - 1; i++) {
if (config.containsKey(dataId[i])) {
config = (Map) config.get(dataId[i]);
private void flattenConfig(String prefix, Map<String, Object> config, Map<String, Object> flatMap) {
for (Map.Entry<String, Object> entry : config.entrySet()) {
String key = prefix.isEmpty() ? entry.getKey() : prefix + "." + entry.getKey();
Object value = entry.getValue();
if (value != null) {
if (value instanceof Map) {
flattenConfig(key, (Map<String, Object>)value, flatMap);
} else {
return null;
flatMap.put(key, String.valueOf(value));
}
}
Object value = config.get(dataId[dataId.length - 1]);
}
}

@Override
public String getString(String path) {
try {
Object value = configMap.get(path);
return value == null ? null : String.valueOf(value);
} catch (Exception e) {
LOGGER.warn("get config data error" + path, e);
return null;
}
}

@Override
public Map<String, Object> getAllConfig() {
return configMap;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void testConfigFileProperties() {
FileConfiguration configuration = mock(FileConfiguration.class);
Configuration currentConfiguration = EnhancedServiceLoader.load(ExtConfigurationProvider.class).provide(configuration);

assertEquals(STR_TEST_AAA, currentConfiguration.getConfig("config.type"));
assertEquals("file", currentConfiguration.getConfig("config.type"));
assertEquals(STR_TEST_BBB, currentConfiguration.getConfig("config.dataType"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
seata.config.type=aaa
seata.config.type=file
seata.config.data-type=bbb
seata.config.file.name=aaa

Expand Down
Loading

0 comments on commit 12c27ff

Please sign in to comment.