From 5ecf40001719bde3f38b36f52b236dfe30510990 Mon Sep 17 00:00:00 2001 From: zendwang Date: Mon, 19 Sep 2022 19:33:57 +0800 Subject: [PATCH] =?UTF-8?q?enhancement=20#I5R005=20=E6=B7=BB=E5=8A=A0Etcd?= =?UTF-8?q?=E5=AD=98=E5=82=A8=E8=A7=84=E5=88=99=E6=8F=92=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../liteflow/parser/etcd/EtcdXmlELParser.java | 19 +++---- .../parser/etcd/util/EtcdParserHelper.java | 10 +--- .../pom.xml | 57 +++++++++++++++++++ .../etcd/EtcdWithXmlELSpringbootTest.java | 15 ++--- 4 files changed, 72 insertions(+), 29 deletions(-) create mode 100644 liteflow-testcase-el/liteflow-testcase-el-etcd-springboot/pom.xml diff --git a/liteflow-rule-plugin/liteflow-rule-etcd/src/main/java/com/yomahub/liteflow/parser/etcd/EtcdXmlELParser.java b/liteflow-rule-plugin/liteflow-rule-etcd/src/main/java/com/yomahub/liteflow/parser/etcd/EtcdXmlELParser.java index 2c843f99..99252655 100644 --- a/liteflow-rule-plugin/liteflow-rule-etcd/src/main/java/com/yomahub/liteflow/parser/etcd/EtcdXmlELParser.java +++ b/liteflow-rule-plugin/liteflow-rule-etcd/src/main/java/com/yomahub/liteflow/parser/etcd/EtcdXmlELParser.java @@ -21,14 +21,6 @@ public class EtcdXmlELParser extends ClassXmlFlowELParser { private final EtcdParserHelper etcdParserHelper; public EtcdXmlELParser() { - Consumer parseConsumer = t -> { - try { - parse(t); - } catch (Exception e) { - throw new RuntimeException(e); - } - }; - LiteflowConfig liteflowConfig = LiteflowConfigGetter.get(); if (StrUtil.isBlank(liteflowConfig.getRuleSourceExtData())){ @@ -46,7 +38,7 @@ public class EtcdXmlELParser extends ClassXmlFlowELParser { throw new EtcdException("Etcd connect string is empty"); } - etcdParserHelper = new EtcdParserHelper(etcdParserVO, parseConsumer); + etcdParserHelper = new EtcdParserHelper(etcdParserVO); }catch (Exception e){ throw new EtcdException(e.getMessage()); } @@ -54,10 +46,17 @@ public class EtcdXmlELParser extends ClassXmlFlowELParser { @Override public String parseCustom() { + Consumer parseConsumer = t -> { + try { + parse(t); + } catch (Exception e) { + throw new RuntimeException(e); + } + }; try { String content = etcdParserHelper.getContent(); etcdParserHelper.checkContent(content); - etcdParserHelper.watchRule(); + etcdParserHelper.listen(parseConsumer); return content; } catch (Exception e){ throw new EtcdException(e.getMessage()); diff --git a/liteflow-rule-plugin/liteflow-rule-etcd/src/main/java/com/yomahub/liteflow/parser/etcd/util/EtcdParserHelper.java b/liteflow-rule-plugin/liteflow-rule-etcd/src/main/java/com/yomahub/liteflow/parser/etcd/util/EtcdParserHelper.java index fa09bdd5..6c45f36a 100644 --- a/liteflow-rule-plugin/liteflow-rule-etcd/src/main/java/com/yomahub/liteflow/parser/etcd/util/EtcdParserHelper.java +++ b/liteflow-rule-plugin/liteflow-rule-etcd/src/main/java/com/yomahub/liteflow/parser/etcd/util/EtcdParserHelper.java @@ -23,13 +23,11 @@ public class EtcdParserHelper { private static final Logger LOG = LoggerFactory.getLogger(EtcdParserHelper.class); private final EtcdParserVO etcdParserVO; - private final Consumer parseConsumer; private EtcdClient etcdClient; - public EtcdParserHelper(EtcdParserVO etcdParserVO, Consumer parseConsumer) { + public EtcdParserHelper(EtcdParserVO etcdParserVO) { this.etcdParserVO = etcdParserVO; - this.parseConsumer = parseConsumer; try{ this.etcdClient = ContextAwareHolder.loadContextAware().getBean(EtcdClient.class); @@ -44,10 +42,6 @@ public class EtcdParserHelper { } } - public Consumer getParseConsumer() { - return parseConsumer; - } - public String getContent(){ try{ return this.etcdClient.get(etcdParserVO.getNodePath()); @@ -69,7 +63,7 @@ public class EtcdParserHelper { /** * 监听 etcd 节点 */ - public void watchRule() { + public void listen(Consumer parseConsumer) { this.etcdClient.watchDataChange(this.etcdParserVO.getNodePath(), (updatePath, updateValue) -> { LOG.info("starting load flow config...."); parseConsumer.accept(updateValue); diff --git a/liteflow-testcase-el/liteflow-testcase-el-etcd-springboot/pom.xml b/liteflow-testcase-el/liteflow-testcase-el-etcd-springboot/pom.xml new file mode 100644 index 00000000..5fdf91f6 --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-etcd-springboot/pom.xml @@ -0,0 +1,57 @@ + + + + liteflow-testcase-el + com.yomahub + ${revision} + ../pom.xml + + 4.0.0 + + liteflow-testcase-el-etcd-springboot + + + + com.yomahub + liteflow-spring-boot-starter + ${revision} + + + + com.yomahub + liteflow-rule-etcd + ${revision} + test + + + + org.springframework.boot + spring-boot-starter-test + + + org.aspectj + aspectjweaver + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + ${springboot.version} + + + org.apache.maven.plugins + maven-deploy-plugin + 2.8.2 + + true + + + + + \ No newline at end of file diff --git a/liteflow-testcase-el/liteflow-testcase-el-etcd-springboot/src/test/java/com/yomahub/liteflow/test/etcd/EtcdWithXmlELSpringbootTest.java b/liteflow-testcase-el/liteflow-testcase-el-etcd-springboot/src/test/java/com/yomahub/liteflow/test/etcd/EtcdWithXmlELSpringbootTest.java index 62cec496..f1756512 100644 --- a/liteflow-testcase-el/liteflow-testcase-el-etcd-springboot/src/test/java/com/yomahub/liteflow/test/etcd/EtcdWithXmlELSpringbootTest.java +++ b/liteflow-testcase-el/liteflow-testcase-el-etcd-springboot/src/test/java/com/yomahub/liteflow/test/etcd/EtcdWithXmlELSpringbootTest.java @@ -41,17 +41,12 @@ public class EtcdWithXmlELSpringbootTest extends BaseTest { @Resource private FlowExecutor flowExecutor; - private String flowXml; - - private String changedFlowXml; - @Before public void setUp() { MockitoAnnotations.initMocks(this); - this.flowXml = "THEN(a, b, c);"; - this.changedFlowXml = "THEN(a, c);"; - when(etcdClient.get(any())).thenReturn(flowXml); - + String flowXml = "THEN(a, b, c);"; + String changedFlowXml = "THEN(a, c);"; + when(etcdClient.get(any())).thenReturn(flowXml).thenReturn(changedFlowXml); } @Test @@ -62,9 +57,7 @@ public class EtcdWithXmlELSpringbootTest extends BaseTest { // 手动触发一次 模拟节点数据变更 EtcdXmlELParser parser = ContextAwareHolder.loadContextAware().getBean(EtcdXmlELParser.class); - EtcdParserHelper etcdParserHelper = (EtcdParserHelper)ReflectUtil.getFieldValue(parser, "etcdParserHelper"); - Consumer parseConsumer = (Consumer)ReflectUtil.getFieldValue(etcdParserHelper, "parseConsumer"); - parseConsumer.accept(changedFlowXml); + parser.parse(etcdClient.get("/lite-flow/flow")); LiteflowResponse response2 = flowExecutor.execute2Resp("chain1", "arg"); Assert.assertTrue(response2.isSuccess());