enhancement #I5R005 添加Etcd存储规则插件

This commit is contained in:
zendwang 2022-09-19 19:33:57 +08:00
parent a368949d71
commit 5ecf400017
4 changed files with 72 additions and 29 deletions

View File

@ -21,14 +21,6 @@ public class EtcdXmlELParser extends ClassXmlFlowELParser {
private final EtcdParserHelper etcdParserHelper;
public EtcdXmlELParser() {
Consumer<String> 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<String> 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());

View File

@ -23,13 +23,11 @@ public class EtcdParserHelper {
private static final Logger LOG = LoggerFactory.getLogger(EtcdParserHelper.class);
private final EtcdParserVO etcdParserVO;
private final Consumer<String> parseConsumer;
private EtcdClient etcdClient;
public EtcdParserHelper(EtcdParserVO etcdParserVO, Consumer<String> 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<String> 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<String> parseConsumer) {
this.etcdClient.watchDataChange(this.etcdParserVO.getNodePath(), (updatePath, updateValue) -> {
LOG.info("starting load flow config....");
parseConsumer.accept(updateValue);

View File

@ -0,0 +1,57 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>liteflow-testcase-el</artifactId>
<groupId>com.yomahub</groupId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>liteflow-testcase-el-etcd-springboot</artifactId>
<dependencies>
<dependency>
<groupId>com.yomahub</groupId>
<artifactId>liteflow-spring-boot-starter</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>com.yomahub</groupId>
<artifactId>liteflow-rule-etcd</artifactId>
<version>${revision}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${springboot.version}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -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 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><flow><chain name=\"chain1\">THEN(a, b, c);</chain></flow>";
this.changedFlowXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><flow><chain name=\"chain1\">THEN(a, c);</chain></flow>";
when(etcdClient.get(any())).thenReturn(flowXml);
String flowXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><flow><chain name=\"chain1\">THEN(a, b, c);</chain></flow>";
String changedFlowXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><flow><chain name=\"chain1\">THEN(a, c);</chain></flow>";
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<String> parseConsumer = (Consumer<String>)ReflectUtil.getFieldValue(etcdParserHelper, "parseConsumer");
parseConsumer.accept(changedFlowXml);
parser.parse(etcdClient.get("/lite-flow/flow"));
LiteflowResponse response2 = flowExecutor.execute2Resp("chain1", "arg");
Assert.assertTrue(response2.isSuccess());