forked from apache/incubator-seata
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
optimize: seata server startup mode support nacos yaml config ability (…
- Loading branch information
Smile
committed
Sep 14, 2021
1 parent
207b09d
commit a38b873
Showing
18 changed files
with
530 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
* Copyright 1999-2019 Seata.io Group. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* 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. | ||
*/ | ||
package io.seata.common.util; | ||
|
||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* Map Util | ||
* | ||
* @author zhixing | ||
*/ | ||
public class MapUtil { | ||
|
||
/** | ||
* obj convert to Map | ||
* | ||
* @param object | ||
* @return Map<String,Object> | ||
*/ | ||
public static Map<String, Object> asMap(Object object) { | ||
// YAML can have numbers as keys | ||
Map<String, Object> result = new LinkedHashMap<>(); | ||
if (!(object instanceof Map)) { | ||
// A document can be a text literal | ||
result.put("document", object); | ||
return result; | ||
} | ||
|
||
Map<Object, Object> map = (Map<Object, Object>) object; | ||
for (Map.Entry<Object, Object> entry : map.entrySet()) { | ||
Object value = entry.getValue(); | ||
if (value instanceof Map) { | ||
value = asMap(value); | ||
} | ||
Object key = entry.getKey(); | ||
if (key instanceof CharSequence) { | ||
result.put(key.toString(), value); | ||
} else { | ||
// It has to be a map key in this case | ||
result.put("[" + key.toString() + "]", value); | ||
} | ||
} | ||
return result; | ||
} | ||
/** | ||
* get flattened Map | ||
* | ||
* @param source | ||
* @return Map<String,Object> | ||
*/ | ||
public static Map<String, Object> getFlattenedMap(Map<String, Object> source) { | ||
Map<String, Object> result = new LinkedHashMap<>(); | ||
buildFlattenedMap(result, source, null); | ||
return result; | ||
} | ||
|
||
private static void buildFlattenedMap(Map<String, Object> result, Map<String, Object> source, String path) { | ||
for (Map.Entry<String, Object> entry : source.entrySet()) { | ||
String key = entry.getKey(); | ||
if (StringUtils.isNotBlank(path)) { | ||
if (key.startsWith("[")) { | ||
key = path + key; | ||
} else { | ||
key = path + '.' + key; | ||
} | ||
} | ||
Object value = entry.getValue(); | ||
if (value instanceof String) { | ||
result.put(key, value); | ||
} else if (value instanceof Map) { | ||
// Need a compound key | ||
@SuppressWarnings("unchecked") | ||
Map<String, Object> map = (Map<String, Object>) value; | ||
buildFlattenedMap(result, map, key); | ||
} else if (value instanceof Collection) { | ||
// Need a compound key | ||
@SuppressWarnings("unchecked") | ||
Collection<Object> collection = (Collection<Object>) value; | ||
int count = 0; | ||
for (Object object : collection) { | ||
buildFlattenedMap(result, | ||
Collections.singletonMap("[" + (count++) + "]", object), key); | ||
} | ||
} else { | ||
result.put(key, value != null ? value : ""); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
config/seata-config-core/src/main/java/io/seata/config/processor/ConfigDataType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright 1999-2019 Seata.io Group. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* 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. | ||
*/ | ||
package io.seata.config.processor; | ||
|
||
/** | ||
* The enum Config Data type. | ||
* | ||
* @author zhixing | ||
*/ | ||
public enum ConfigDataType { | ||
/** | ||
* data type yaml | ||
*/ | ||
yaml("yaml","yml"), | ||
/** | ||
* data type properties | ||
*/ | ||
properties("properties"); | ||
|
||
|
||
/** | ||
* suffix support data type | ||
*/ | ||
private String[] suffix; | ||
|
||
ConfigDataType(String... suffix) { | ||
this.suffix = suffix; | ||
} | ||
|
||
/** | ||
* Gets type. | ||
* | ||
* @param name the name | ||
* @return the type | ||
*/ | ||
public static ConfigDataType getType(String name) { | ||
for (ConfigDataType configDataType : values()) { | ||
if (configDataType.name().equalsIgnoreCase(name)) { | ||
return configDataType; | ||
} | ||
} | ||
throw new IllegalArgumentException("not support config data type type: " + name); | ||
} | ||
|
||
/** | ||
* Gets type by suffix. | ||
* | ||
* @param suffix the suffix | ||
* @return the type | ||
*/ | ||
public static ConfigDataType getTypeBySuffix(String suffix) { | ||
for (ConfigDataType configDataType : values()) { | ||
for (String sfx : configDataType.suffix) { | ||
if (sfx.equals(suffix)) { | ||
return configDataType; | ||
} | ||
} | ||
} | ||
throw new IllegalArgumentException("not support config data type suffix: " + suffix); | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
config/seata-config-core/src/main/java/io/seata/config/processor/ConfigProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Copyright 1999-2019 Seata.io Group. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* 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. | ||
*/ | ||
package io.seata.config.processor; | ||
|
||
import io.seata.common.loader.EnhancedServiceLoader; | ||
import io.seata.common.util.StringUtils; | ||
import io.seata.config.Configuration; | ||
import io.seata.config.ConfigurationFactory; | ||
import io.seata.config.ConfigurationKeys; | ||
|
||
import java.io.IOException; | ||
import java.util.Properties; | ||
|
||
/** | ||
* The Config Processor. | ||
* | ||
* @author zhixing | ||
*/ | ||
public class ConfigProcessor { | ||
private static final String SEPARATOR = "."; | ||
private static final Configuration FILE_CONFIG = ConfigurationFactory.CURRENT_FILE_INSTANCE; | ||
private static final String DEFAULT_DATA_TYPE = "properties"; | ||
/** | ||
* processing configuration | ||
* | ||
* @param config config string | ||
* @param dataType the data type | ||
* @return the properties | ||
* @throws IOException IOException | ||
*/ | ||
public static Properties processConfig(String config, String dataType) throws IOException { | ||
return EnhancedServiceLoader.load(Processor.class, dataType).processor(config); | ||
} | ||
|
||
/** | ||
* resolver config data type | ||
* | ||
* @param dataId the configured data id | ||
* @return data type | ||
*/ | ||
public static String resolverConfigDataType(String dataId) { | ||
return resolverConfigDataType(FILE_CONFIG.getConfig(getDataTypeKey()),dataId,DEFAULT_DATA_TYPE); | ||
} | ||
|
||
/** | ||
* resolver config data type | ||
* | ||
* @param dataType the configured data type | ||
* @param dataId the configured data id | ||
* @param defaultDataType the default data type | ||
* @return data type | ||
*/ | ||
public static String resolverConfigDataType(String dataType,String dataId,String defaultDataType) { | ||
if (StringUtils.isNotBlank(dataType)) { | ||
return dataType; | ||
} | ||
if (!dataId.contains(SEPARATOR)) { | ||
return defaultDataType; | ||
} | ||
String[] splitString = dataId.split("\\" + SEPARATOR); | ||
try { | ||
ConfigDataType configDataType = ConfigDataType.getTypeBySuffix(splitString[splitString.length - 1]); | ||
return configDataType.name(); | ||
} catch (IllegalArgumentException e) { | ||
return defaultDataType; | ||
} | ||
|
||
} | ||
|
||
private static String getDataTypeKey() { | ||
return String.join(ConfigurationKeys.FILE_CONFIG_SPLIT_CHAR, ConfigurationKeys.FILE_ROOT_CONFIG, ConfigurationKeys.DATA_TYPE); | ||
} | ||
|
||
} |
Oops, something went wrong.