From 918111dfda9093fb6df5863fa96f6a6dd9a58ee6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9F=A9=E5=8D=8E=E9=94=8B?= <799954772@qq.com> Date: Thu, 14 Sep 2023 11:19:42 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90fix=E3=80=91=E4=BF=AE=E5=A4=8DApolloPa?= =?UTF-8?q?rseHelper.java=E4=B8=AD=E8=B0=83=E7=94=A8convert=E5=90=8E?= =?UTF-8?q?=EF=BC=8C=E5=8F=AF=E8=83=BD=E4=BC=9A=E5=87=BA=E7=8E=B0=E7=A9=BA?= =?UTF-8?q?=E6=8C=87=E9=92=88=E7=9A=84=E9=97=AE=E9=A2=98=20=E3=80=90fix?= =?UTF-8?q?=E3=80=91FlowInitHook.java=E6=96=87=E4=BB=B6=E4=B8=AD=EF=BC=8C?= =?UTF-8?q?=E5=B8=B8=E9=87=8F=E5=91=BD=E5=B0=8F=E5=86=99=EF=BC=8C=E4=B8=8D?= =?UTF-8?q?=E7=AC=A6=E5=90=88=E5=B8=B8=E7=94=A8=E8=A7=84=E8=8C=83=EF=BC=8C?= =?UTF-8?q?=E6=94=B9=E4=B8=BA=E4=BA=86=E5=A4=A7=E5=86=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../yomahub/liteflow/core/FlowInitHook.java | 10 +- .../parser/apollo/util/ApolloParseHelper.java | 337 +++++++++--------- 2 files changed, 177 insertions(+), 170 deletions(-) diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/core/FlowInitHook.java b/liteflow-core/src/main/java/com/yomahub/liteflow/core/FlowInitHook.java index 4e4ea12c2..ba1628d62 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/core/FlowInitHook.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/core/FlowInitHook.java @@ -14,20 +14,20 @@ import java.util.function.BooleanSupplier; */ public class FlowInitHook { - private static final List supplierList = new ArrayList<>(); + private static final List SUPPLIER_LIST = new ArrayList<>(); public static void executeHook() { - if (CollUtil.isNotEmpty(supplierList)) { - supplierList.forEach(BooleanSupplier::getAsBoolean); + if (CollUtil.isNotEmpty(SUPPLIER_LIST)) { + SUPPLIER_LIST.forEach(BooleanSupplier::getAsBoolean); } } public static void addHook(BooleanSupplier hookSupplier) { - supplierList.add(hookSupplier); + SUPPLIER_LIST.add(hookSupplier); } public static void cleanHook() { - supplierList.clear(); + SUPPLIER_LIST.clear(); } } diff --git a/liteflow-rule-plugin/liteflow-rule-apollo/src/main/java/com/yomahub/liteflow/parser/apollo/util/ApolloParseHelper.java b/liteflow-rule-plugin/liteflow-rule-apollo/src/main/java/com/yomahub/liteflow/parser/apollo/util/ApolloParseHelper.java index 468704be1..3a27c8e7e 100644 --- a/liteflow-rule-plugin/liteflow-rule-apollo/src/main/java/com/yomahub/liteflow/parser/apollo/util/ApolloParseHelper.java +++ b/liteflow-rule-plugin/liteflow-rule-apollo/src/main/java/com/yomahub/liteflow/parser/apollo/util/ApolloParseHelper.java @@ -24,215 +24,222 @@ import java.util.Objects; import java.util.Set; import java.util.stream.Collectors; +import static com.ctrip.framework.apollo.enums.PropertyChangeType.DELETED; + /** * @author zhanghua * @since 2.9.5 */ public class ApolloParseHelper { - private static final Logger LOG = LoggerFactory.getLogger(ApolloParseHelper.class); + private static final Logger LOG = LoggerFactory.getLogger(ApolloParseHelper.class); - private final String CHAIN_XML_PATTERN = "{}"; + private final String CHAIN_XML_PATTERN = "{}"; - private final String NODE_XML_PATTERN = "{}"; + private final String NODE_XML_PATTERN = "{}"; - private final String NODE_ITEM_XML_PATTERN = ""; + private final String NODE_ITEM_XML_PATTERN = ""; - private final String XML_PATTERN = "{}{}"; + private final String XML_PATTERN = "{}{}"; - private final ApolloParserConfigVO apolloParserConfigVO; + private final ApolloParserConfigVO apolloParserConfigVO; - private Config chainConfig; + private Config chainConfig; - private Config scriptConfig; + private Config scriptConfig; - public ApolloParseHelper(ApolloParserConfigVO apolloParserConfigVO) { - this.apolloParserConfigVO = apolloParserConfigVO; + public ApolloParseHelper(ApolloParserConfigVO apolloParserConfigVO) { + this.apolloParserConfigVO = apolloParserConfigVO; - try { - try { - // 这里本身对于程序运行来说没有什么意义,拿到的永远是null - // 其实config对象也没有注入到spring容器中 - // 这里这样写的目的是为了单测中的mockito,当有@MockBean的时候,这里就能拿到了 - this.chainConfig = ContextAwareHolder.loadContextAware().getBean("chainConfig"); - this.scriptConfig = ContextAwareHolder.loadContextAware().getBean("scriptConfig"); - } - catch (Exception ignored) { - } + try { + try { + // 这里本身对于程序运行来说没有什么意义,拿到的永远是null + // 其实config对象也没有注入到spring容器中 + // 这里这样写的目的是为了单测中的mockito,当有@MockBean的时候,这里就能拿到了 + this.chainConfig = ContextAwareHolder.loadContextAware().getBean("chainConfig"); + this.scriptConfig = ContextAwareHolder.loadContextAware().getBean("scriptConfig"); + } catch (Exception ignored) { + } - if (ObjectUtil.isNull(chainConfig)) { - chainConfig = ConfigService.getConfig(apolloParserConfigVO.getChainNamespace()); - String scriptNamespace; - // scriptConfig is optional - if (StrUtil.isNotBlank(scriptNamespace = apolloParserConfigVO.getScriptNamespace())) { - scriptConfig = ConfigService.getConfig(scriptNamespace); - } - } - } - catch (Exception e) { - throw new ApolloException(e.getMessage()); - } - } + if (ObjectUtil.isNull(chainConfig)) { + chainConfig = ConfigService.getConfig(apolloParserConfigVO.getChainNamespace()); + String scriptNamespace; + // scriptConfig is optional + if (StrUtil.isNotBlank(scriptNamespace = apolloParserConfigVO.getScriptNamespace())) { + scriptConfig = ConfigService.getConfig(scriptNamespace); + } + } + } catch (Exception e) { + throw new ApolloException(e.getMessage()); + } + } - public String getContent() { + public String getContent() { - try { - // 1. handle chain - Set propertyNames = chainConfig.getPropertyNames(); - if (CollectionUtil.isEmpty(propertyNames)) { - throw new ApolloException(StrUtil.format("There are no chains in namespace : {}", - apolloParserConfigVO.getChainNamespace())); - } - List chainItemContentList = propertyNames.stream() - .map(item -> StrUtil.format(CHAIN_XML_PATTERN, item, chainConfig.getProperty(item, StrUtil.EMPTY))) - .collect(Collectors.toList()); - // merge all chain content - String chainAllContent = CollUtil.join(chainItemContentList, StrUtil.EMPTY); + try { + // 1. handle chain + Set propertyNames = chainConfig.getPropertyNames(); + if (CollectionUtil.isEmpty(propertyNames)) { + throw new ApolloException(StrUtil.format("There are no chains in namespace : {}", + apolloParserConfigVO.getChainNamespace())); + } + List chainItemContentList = propertyNames.stream() + .map(item -> StrUtil.format(CHAIN_XML_PATTERN, item, chainConfig.getProperty(item, StrUtil.EMPTY))) + .collect(Collectors.toList()); + // merge all chain content + String chainAllContent = CollUtil.join(chainItemContentList, StrUtil.EMPTY); - // 2. handle script if needed - String scriptAllContent = StrUtil.EMPTY; - Set scriptNamespaces; - if (Objects.nonNull(scriptConfig) - && CollectionUtil.isNotEmpty(scriptNamespaces = scriptConfig.getPropertyNames())) { + // 2. handle script if needed + String scriptAllContent = StrUtil.EMPTY; + Set scriptNamespaces; + if (Objects.nonNull(scriptConfig) + && CollectionUtil.isNotEmpty(scriptNamespaces = scriptConfig.getPropertyNames())) { - List scriptItemContentList = scriptNamespaces.stream() - .map(item -> convert(item, scriptConfig.getProperty(item, StrUtil.EMPTY))) - .filter(Objects::nonNull) - .map(item -> StrUtil.format(NODE_ITEM_XML_PATTERN, item.getNodeId(), item.getName(), item.getType(), - item.getScript())) - .collect(Collectors.toList()); + List scriptItemContentList = scriptNamespaces.stream() + .map(item -> convert(item, scriptConfig.getProperty(item, StrUtil.EMPTY))) + .filter(Objects::nonNull) + .map(item -> StrUtil.format(NODE_ITEM_XML_PATTERN, item.getNodeId(), item.getName(), item.getType(), + item.getScript())) + .collect(Collectors.toList()); - scriptAllContent = StrUtil.format(NODE_XML_PATTERN, - CollUtil.join(scriptItemContentList, StrUtil.EMPTY)); - } + scriptAllContent = StrUtil.format(NODE_XML_PATTERN, + CollUtil.join(scriptItemContentList, StrUtil.EMPTY)); + } - return StrUtil.format(XML_PATTERN, scriptAllContent, chainAllContent); - } - catch (Exception e) { - throw new ApolloException(e.getMessage()); - } - } + return StrUtil.format(XML_PATTERN, scriptAllContent, chainAllContent); + } catch (Exception e) { + throw new ApolloException(e.getMessage()); + } + } - /** - * listen apollo config change - */ - public void listenApollo() { + /** + * listen apollo config change + */ + public void listenApollo() { + // chain + chainConfig.addChangeListener(changeEvent -> changeEvent.changedKeys().forEach(changeKey -> { + ConfigChange configChange = changeEvent.getChange(changeKey); + String newValue = configChange.getNewValue(); + PropertyChangeType changeType = configChange.getChangeType(); + switch (changeType) { + case ADDED: + case MODIFIED: + LOG.info("starting reload flow config... {} key={} value={},", changeType.name(), changeKey, + newValue); + LiteFlowChainELBuilder.createChain().setChainId(changeKey).setEL(newValue).build(); + break; + case DELETED: + LOG.info("starting reload flow config... delete key={}", changeKey); + FlowBus.removeChain(changeKey); + break; + default: + } + })); - // chain - chainConfig.addChangeListener(changeEvent -> changeEvent.changedKeys().forEach(changeKey -> { - ConfigChange configChange = changeEvent.getChange(changeKey); - String newValue = configChange.getNewValue(); - PropertyChangeType changeType = configChange.getChangeType(); - switch (changeType) { - case ADDED: - case MODIFIED: - LOG.info("starting reload flow config... {} key={} value={},", changeType.name(), changeKey, - newValue); - LiteFlowChainELBuilder.createChain().setChainId(changeKey).setEL(newValue).build(); - break; - case DELETED: - LOG.info("starting reload flow config... delete key={}", changeKey); - FlowBus.removeChain(changeKey); + if (StrUtil.isNotBlank(apolloParserConfigVO.getScriptNamespace())) { + scriptConfig.addChangeListener(changeEvent -> changeEvent.changedKeys().forEach(changeKey -> { + ConfigChange configChange = changeEvent.getChange(changeKey); + String newValue = configChange.getNewValue(); - } - })); + PropertyChangeType changeType = configChange.getChangeType(); + if (DELETED.equals(changeType)) { + newValue = null; + } + NodeSimpleVO nodeSimpleVO = convert(changeKey, newValue); + if (Objects.isNull(nodeSimpleVO)) { + // key不符合规范的时候,直接忽略 + LOG.error("key={} is not a valid node config, ignore it", changeKey); + return; + } + switch (changeType) { + case ADDED: + case MODIFIED: + LOG.info("starting reload flow config... {} key={} value={},", changeType.name(), changeKey, + newValue); - if (StrUtil.isNotBlank(apolloParserConfigVO.getScriptNamespace())) { - scriptConfig.addChangeListener(changeEvent -> changeEvent.changedKeys().forEach(changeKey -> { - ConfigChange configChange = changeEvent.getChange(changeKey); - String newValue = configChange.getNewValue(); + LiteFlowNodeBuilder.createScriptNode() + .setId(nodeSimpleVO.getNodeId()) + .setType(NodeTypeEnum.getEnumByCode(nodeSimpleVO.getType())) + .setName(nodeSimpleVO.getName()) + .setScript(nodeSimpleVO.getScript()) + .build(); + break; + case DELETED: + LOG.info("starting reload flow config... delete key={}", changeKey); + FlowBus.getNodeMap().remove(nodeSimpleVO.getNodeId()); + break; + default: + } + })); + } + } - PropertyChangeType changeType = configChange.getChangeType(); + private NodeSimpleVO convert(String key, String value) { + // 不需要去理解这串正则,就是一个匹配冒号的 + // 一定得是a:b,或是a:b:c...这种完整类型的字符串的 + List matchItemList = ReUtil.findAllGroup0("(?<=[^:]:)[^:]+|[^:]+(?=:[^:])", key); + if (CollUtil.isEmpty(matchItemList)) { + return null; + } - NodeSimpleVO nodeSimpleVO; - switch (changeType) { - case ADDED: - case MODIFIED: - LOG.info("starting reload flow config... {} key={} value={},", changeType.name(), changeKey, - newValue); - nodeSimpleVO = convert(changeKey, newValue); - LiteFlowNodeBuilder.createScriptNode() - .setId(nodeSimpleVO.getNodeId()) - .setType(NodeTypeEnum.getEnumByCode(nodeSimpleVO.getType())) - .setName(nodeSimpleVO.getName()) - .setScript(nodeSimpleVO.getScript()) - .build(); - break; - case DELETED: - LOG.info("starting reload flow config... delete key={}", changeKey); - nodeSimpleVO = convert(changeKey, null); - FlowBus.getNodeMap().remove(nodeSimpleVO.getNodeId()); - } - })); - } - } + NodeSimpleVO nodeSimpleVO = new NodeSimpleVO(); + if (matchItemList.size() > 1) { + nodeSimpleVO.setNodeId(matchItemList.get(0)); + nodeSimpleVO.setType(matchItemList.get(1)); + } - private NodeSimpleVO convert(String key, String value) { - // 不需要去理解这串正则,就是一个匹配冒号的 - // 一定得是a:b,或是a:b:c...这种完整类型的字符串的 - List matchItemList = ReUtil.findAllGroup0("(?<=[^:]:)[^:]+|[^:]+(?=:[^:])", key); - if (CollUtil.isEmpty(matchItemList)) { - return null; - } + if (matchItemList.size() > 2) { + nodeSimpleVO.setName(matchItemList.get(2)); + } - NodeSimpleVO nodeSimpleVO = new NodeSimpleVO(); - if (matchItemList.size() > 1) { - nodeSimpleVO.setNodeId(matchItemList.get(0)); - nodeSimpleVO.setType(matchItemList.get(1)); - } + // set script + nodeSimpleVO.setScript(value); - if (matchItemList.size() > 2) { - nodeSimpleVO.setName(matchItemList.get(2)); - } + return nodeSimpleVO; + } - // set script - nodeSimpleVO.setScript(value); + private static class NodeSimpleVO { - return nodeSimpleVO; - } + private String nodeId; - private static class NodeSimpleVO { + private String type; - private String nodeId; + private String name = StrUtil.EMPTY; - private String type; + private String script; - private String name = StrUtil.EMPTY; + public String getNodeId() { + return nodeId; + } - private String script; + public void setNodeId(String nodeId) { + this.nodeId = nodeId; + } - public String getNodeId() { - return nodeId; - } + public String getType() { + return type; + } - public void setNodeId(String nodeId) { - this.nodeId = nodeId; - } + public void setType(String type) { + this.type = type; + } - public String getType() { - return type; - } + public String getName() { + return name; + } - public void setType(String type) { - this.type = type; - } + public void setName(String name) { + this.name = name; + } - public String getName() { - return name; - } + public String getScript() { + return script; + } - public void setName(String name) { - this.name = name; - } + public void setScript(String script) { + this.script = script; + } - public String getScript() { - return script; - } - - public void setScript(String script) { - this.script = script; - } - - } + } }