feature #I63WME 希望支持Apollo配置中心配置规则功能

This commit is contained in:
everywhere.z
2022-12-05 13:54:30 +08:00
parent 2adeb141b5
commit 341f1bc4ee
5 changed files with 53 additions and 20 deletions

View File

@@ -2,6 +2,7 @@ package com.yomahub.liteflow.parser.apollo.util;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import com.ctrip.framework.apollo.Config;
@@ -14,6 +15,7 @@ import com.yomahub.liteflow.enums.NodeTypeEnum;
import com.yomahub.liteflow.flow.FlowBus;
import com.yomahub.liteflow.parser.apollo.exception.ApolloException;
import com.yomahub.liteflow.parser.apollo.vo.ApolloParserConfigVO;
import com.yomahub.liteflow.spi.holder.ContextAwareHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -49,12 +51,23 @@ public class ApolloParseHelper {
public ApolloParseHelper(ApolloParserConfigVO apolloParserConfigVO) {
this.apolloParserConfigVO = apolloParserConfigVO;
try {
chainConfig = ConfigService.getConfig(apolloParserConfigVO.getChainNamespace());
String scriptNamespace;
// scriptConfig is optional
if (StrUtil.isNotBlank(scriptNamespace = apolloParserConfigVO.getScriptNamespace())) {
scriptConfig = ConfigService.getConfig(scriptNamespace);
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());

View File

@@ -15,6 +15,7 @@
<module>liteflow-rule-sql</module>
<module>liteflow-rule-nacos</module>
<module>liteflow-rule-etcd</module>
<module>liteflow-rule-apollo</module>
</modules>
<artifactId>liteflow-rule-plugin</artifactId>

View File

@@ -1,19 +1,30 @@
package com.yomahub.liteflow.test.apollo;
import cn.hutool.core.util.StrUtil;
import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigService;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.yomahub.liteflow.core.FlowExecutor;
import com.yomahub.liteflow.flow.FlowBus;
import com.yomahub.liteflow.flow.LiteflowResponse;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import static org.mockito.Mockito.*;
/**
* @Description:
@@ -27,30 +38,38 @@ import javax.annotation.Resource;
@ComponentScan({"com.yomahub.liteflow.test.apollo.cmp"})
public class ApolloWithXmlELSpringbootTest {
@MockBean(name = "chainConfig")
private Config chainConfig;
@MockBean(name = "scriptConfig")
private Config scriptConfig;
@Resource
private FlowExecutor flowExecutor;
@Before
public void setUp(){
MockitoAnnotations.initMocks(this);
}
@After
public void after() {
FlowBus.cleanCache();
}
@Test
public void testApolloWithXml1() throws InterruptedException {
public void testApolloWithXml1(){
Set<String> chainNameList = Sets.newHashSet("chain1");
Set<String> scriptNodeValueList = Sets.newHashSet("s1:script:脚本s1");
when(chainConfig.getPropertyNames()).thenReturn(chainNameList);
when(scriptConfig.getPropertyNames()).thenReturn(scriptNodeValueList);
String chain1Data = "THEN(a, b, c, s1);";
String scriptNodeValue = "defaultContext.setData(\"test\",\"hello\");";
when(chainConfig.getProperty(anyString(), anyString())).thenReturn(chain1Data);
when(scriptConfig.getProperty(anyString(), anyString())).thenReturn(scriptNodeValue);
LiteflowResponse response = flowExecutor.execute2Resp("chain1", "arg");
Assert.assertEquals("a==>b==>c==>s1[脚本s1]", response.getExecuteStepStrWithoutTime());
}
@Test
public void testApolloWithXml2() throws InterruptedException {
while (true) {
LiteflowResponse response = flowExecutor.execute2Resp("chain1", "arg");
System.out.println("liteflow step : " + response.getExecuteStepStrWithoutTime());
Thread.sleep(2000l);
}
}
}

View File

@@ -43,7 +43,6 @@ public class EtcdWithXmlELSpringbootTest extends BaseTest {
private static final String SCRIPT_PATH = "/liteflow/script";
@Before
public void setUp(){
MockitoAnnotations.initMocks(this);

View File

@@ -28,6 +28,7 @@
<module>liteflow-testcase-el-sql-springboot</module>
<module>liteflow-testcase-el-nacos-springboot</module>
<module>liteflow-testcase-el-etcd-springboot</module>
<module>liteflow-testcase-el-apollo-springboot</module>
</modules>
<build>