version 38x-deploy replicas 1 restart_policy condition on-failureservices adrs-ui image master5000devly-mssp-auth-ui-test2220230808190051 volumes - servertest222DBcontainertest222DB
你可以使用Java的YAML库来解析这个YAML字符串并将其分割为多个String数组。以下是使用SnakeYAML库的示例代码:
import org.yaml.snakeyaml.Yaml;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class Main {
public static void main(String[] args) {
String yamlString = "version: '3.8'\n" +
"x-deploy:\n" +
" replicas: 1\n" +
" restart_policy:\n" +
" condition: on-failure\n" +
"services:\n" +
" adrs-ui:\n" +
" image: master:5000/dev/ly-mssp-auth-ui-test:2.2.20230808190051\n" +
" volumes:\n" +
" - /server/test222.DB:/container/test222.DB\n" +
" - /server/test.DB:/container/test.DB\n" +
" adrs-ui_test:\n" +
" image: 192.168.2.78:5000/dev/ly-mssp-auth-svc:2.2\n" +
" hostname: adrs-ui-aaa\n" +
" init: true\n" +
" ports:\n" +
" - 15121:80\n" +
" depends_on:\n" +
" - api\n" +
" environment:\n" +
" spring.cloud.inetutils.preferred-networks: 10.10.0\n" +
" APIGATEWAY: 192.168.35.55:1101\n" +
" GATEWAY: 192.168.35.55:1101\n" +
" Cas_Enable: 'false'\n" +
" DOCURL: 192.168.35.210:8888\n" +
" networks:\n" +
" - portainer_agent_network\n" +
" deploy:\n" +
" placement:\n" +
" constraints:\n" +
" - node.hostname == p1\n" +
"networks:\n" +
" portainer_agent_network:\n" +
" external: true";
Yaml yaml = new Yaml();
Map<String, Object> yamlMap = yaml.load(yamlString);
List<String[]> servicesList = new ArrayList<>();
if (yamlMap.containsKey("services")) {
Map<String, Object> services = (Map<String, Object>) yamlMap.get("services");
for (String service : services.keySet()) {
List<String> serviceLines = new ArrayList<>();
serviceLines.add(service + ":");
Map<String, Object> serviceProperties = (Map<String, Object>) services.get(service);
for (String key : serviceProperties.keySet()) {
Object value = serviceProperties.get(key);
if (value instanceof String) {
serviceLines.add(" " + key + ": " + value);
} else if (value instanceof List) {
List<String> itemList = (List<String>) value;
for (String item : itemList) {
serviceLines.add(" " + key + ": " + item);
}
} else if (value instanceof Map) {
Map<String, String> map = (Map<String, String>) value;
for (String mapKey : map.keySet()) {
String mapValue = map.get(mapKey);
serviceLines.add(" " + key + ":");
serviceLines.add(" " + mapKey + ": " + mapValue);
}
}
}
servicesList.add(serviceLines.toArray(new String[0]));
}
}
for (String[] serviceLines : servicesList) {
for (String line : serviceLines) {
System.out.println(line);
}
System.out.println();
}
}
}
这样,你就可以得到每个服务的配置信息,并将其存储在一个String数组中。注意,这个示例假设每个服务的配置信息都是键值对的形式。如果有其他复杂的结构,请根据需要进行适当的修改
原文地址: http://www.cveoy.top/t/topic/iDT7 著作权归作者所有。请勿转载和采集!