1. 支持apollo

This commit is contained in:
zhanghua
2022-12-04 01:41:15 +08:00
parent 6bd329ab8b
commit 7a2887ac03
9 changed files with 317 additions and 0 deletions

View File

@@ -0,0 +1,45 @@
<?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-rule-plugin</artifactId>
<groupId>com.yomahub</groupId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>liteflow-rule-apollo</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.yomahub</groupId>
<artifactId>liteflow-core</artifactId>
<version>${revision}</version>
<optional>true</optional>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${httpclient.version}</version>
</dependency>
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,65 @@
package com.yomahub.liteflow.parser.apollo;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.yomahub.liteflow.parser.apollo.exception.ApolloException;
import com.yomahub.liteflow.parser.apollo.util.ApolloParseHelper;
import com.yomahub.liteflow.parser.apollo.vo.ApolloParserConfigVO;
import com.yomahub.liteflow.parser.el.ClassXmlFlowELParser;
import com.yomahub.liteflow.property.LiteflowConfig;
import com.yomahub.liteflow.property.LiteflowConfigGetter;
import com.yomahub.liteflow.util.JsonUtil;
import java.util.Objects;
import java.util.function.Consumer;
/**
* @Description:
* @Author: zhanghua
* @Date: 2022/12/3 13:38
*/
public class ApolloXmlELParser extends ClassXmlFlowELParser {
private final ApolloParseHelper apolloParseHelper;
public ApolloXmlELParser() {
LiteflowConfig liteflowConfig = LiteflowConfigGetter.get();
try {
ApolloParserConfigVO apolloParserConfigVO = null;
if (MapUtil.isNotEmpty((liteflowConfig.getRuleSourceExtDataMap()))) {
apolloParserConfigVO = BeanUtil.toBean(liteflowConfig.getRuleSourceExtDataMap(), ApolloParserConfigVO.class, CopyOptions.create());
} else if (StrUtil.isNotBlank(liteflowConfig.getRuleSourceExtData())) {
apolloParserConfigVO = JsonUtil.parseObject(liteflowConfig.getRuleSourceExtData(), ApolloParserConfigVO.class);
}
if (Objects.isNull(apolloParserConfigVO)) {
throw new ApolloException("ruleSourceExtData or map is empty");
}
apolloParseHelper = new ApolloParseHelper(apolloParserConfigVO);
} catch (Exception e) {
throw new ApolloException(e.getMessage());
}
}
@Override
public String parseCustom() {
Consumer<String> parseConsumer = t -> {
try {
parse(t);
} catch (Exception e) {
throw new ApolloException(e.getMessage());
}
};
try {
String content = apolloParseHelper.getContent();
apolloParseHelper.checkContent(content);
apolloParseHelper.listen(parseConsumer);
return content;
} catch (Exception e) {
throw new ApolloException(e.getMessage());
}
}
}

View File

@@ -0,0 +1,21 @@
package com.yomahub.liteflow.parser.apollo.exception;
/**
* @Description:
* @Author: zhanghua
* @Date: 2022/12/3 13:45
*/
public class ApolloException extends RuntimeException {
private String message;
public ApolloException(String message) {
super();
this.message = message;
}
@Override
public String getMessage() {
return message;
}
}

View File

@@ -0,0 +1,87 @@
package com.yomahub.liteflow.parser.apollo.util;
import cn.hutool.core.util.StrUtil;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.client.config.NacosConfigService;
import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigFile;
import com.ctrip.framework.apollo.ConfigFileChangeListener;
import com.ctrip.framework.apollo.ConfigService;
import com.ctrip.framework.apollo.core.dto.ApolloConfig;
import com.ctrip.framework.apollo.core.enums.ConfigFileFormat;
import com.ctrip.framework.apollo.model.ConfigFileChangeEvent;
import com.yomahub.liteflow.exception.ParseException;
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.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Objects;
import java.util.Properties;
import java.util.function.Consumer;
/**
* @Description:
* @Author: zhanghua
* @Date: 2022/12/3 13:47
*/
public class ApolloParseHelper {
private static final Logger LOG = LoggerFactory.getLogger(ApolloParseHelper.class);
private final ApolloParserConfigVO apolloParserConfigVO;
private ConfigFile liteflowConfigFile;
public ApolloParseHelper(ApolloParserConfigVO apolloParserConfigVO) {
this.apolloParserConfigVO = apolloParserConfigVO;
try {
liteflowConfigFile = ConfigService.getConfigFile(apolloParserConfigVO.getNamespace(), ConfigFileFormat.XML);
} catch (Exception e) {
LOG.error("[ApolloParseHelper] liteflowConfigFile get init error, apolloParserConfigVO:{}", apolloParserConfigVO);
throw new ApolloException(e.getMessage());
}
}
public String getContent() {
try {
ConfigFile configFile = ConfigService.getConfigFile(apolloParserConfigVO.getNamespace(), ConfigFileFormat.XML);
String content;
if (Objects.isNull(configFile) || StrUtil.isBlank(content = configFile.getContent())) {
throw new ApolloException(String.format("not find config file, namespace:%s", apolloParserConfigVO.getNamespace()));
}
return content;
} catch (Exception e) {
throw new ApolloException(e.getMessage());
}
}
/**
* 检查 content 是否合法
*/
public void checkContent(String content) {
if (StrUtil.isBlank(content)) {
String error = StrUtil.format("the node[{}] value is empty", apolloParserConfigVO.toString());
throw new ParseException(error);
}
}
/**
* 监听 apollo 数据变化
*/
public void listen(Consumer<String> parseConsumer) {
try {
liteflowConfigFile.addChangeListener(configFileChangeEvent -> {
String newContext = configFileChangeEvent.getNewValue();
parseConsumer.accept(newContext);
});
} catch (Exception e) {
throw new ApolloException(e.getMessage());
}
}
}

View File

@@ -0,0 +1,34 @@
package com.yomahub.liteflow.parser.apollo.vo;
/**
* @Description:
* @Author: zhanghua
* @Date: 2022/12/3 13:45
*/
public class ApolloParserConfigVO {
private String namespace;
public ApolloParserConfigVO() {
}
public ApolloParserConfigVO(String namespace) {
this.namespace = namespace;
}
public String getNamespace() {
return namespace;
}
public void setNamespace(String namespace) {
this.namespace = namespace;
}
@Override
public String toString() {
return "ApolloParserConfigVO{" +
"namespace='" + namespace + '\'' +
'}';
}
}

View File

@@ -0,0 +1,17 @@
package com.yomahub.liteflow.parser.spi.apollo;
import com.yomahub.liteflow.parser.apollo.ApolloXmlELParser;
import com.yomahub.liteflow.parser.spi.ParserClassNameSpi;
/**
* @Description:
* @Author: zhanghua
* @Date: 2022/12/3 13:40
*/
public class ApolloParserClassNameSpi implements ParserClassNameSpi {
@Override
public String getSpiClassName() {
return ApolloXmlELParser.class.getName();
}
}

View File

@@ -0,0 +1 @@
com.yomahub.liteflow.parser.spi.apollo.ApolloParserClassNameSpi

View File

@@ -0,0 +1,38 @@
<?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</artifactId>
<groupId>com.yomahub</groupId>
<version>2.9.4</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>liteflow-testcase-el-apollo-springboot</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.yomahub</groupId>
<artifactId>liteflow-spring-boot-starter</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>com.yomahub</groupId>
<artifactId>liteflow-rule-apollo</artifactId>
<version>${revision}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@@ -68,6 +68,7 @@
<guava.version>31.1-jre</guava.version>
<httpclient.version>4.5.13</httpclient.version>
<commons-beanutils.version>1.9.4</commons-beanutils.version>
<apollo.version>1.7.0</apollo.version>
</properties>
<dependencyManagement>
@@ -259,6 +260,12 @@
<scope>provided</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId>
<version>${apollo.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
@@ -370,6 +377,8 @@
<module>liteflow-spring-boot-starter</module>
<module>liteflow-spring</module>
<module>liteflow-testcase-el</module>
<module>liteflow-rule-apollo</module>
<module>liteflow-testcase-el-apollo-springboot</module>
</modules>
<distributionManagement>