!258 针对nacos增加阿里云MSE的鉴权方式

Merge pull request !258 from 卑微小韩/issues/I8H1LT
This commit is contained in:
铂赛东 2023-12-19 06:54:47 +00:00 committed by Gitee
commit ae42bfc31d
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
6 changed files with 108 additions and 18 deletions

View File

@ -1,6 +1,7 @@
/** /**
* <p>Title: liteflow</p> * <p>Title: liteflow</p>
* <p>Description: 轻量级的组件式流程框架</p> * <p>Description: 轻量级的组件式流程框架</p>
*
* @author Bryan.Zhang * @author Bryan.Zhang
* @email weenyc31@163.com * @email weenyc31@163.com
* @Date 2020/4/1 * @Date 2020/4/1
@ -14,6 +15,6 @@ package com.yomahub.liteflow.enums;
*/ */
public enum CmpStepTypeEnum { public enum CmpStepTypeEnum {
START, END, SINGLE; START, END, SINGLE, WHEN_START, WHEN_END;
} }

View File

@ -8,13 +8,17 @@
package com.yomahub.liteflow.flow.element.condition; package com.yomahub.liteflow.flow.element.condition;
import com.yomahub.liteflow.common.LocalDefaultFlowConstant; import com.yomahub.liteflow.common.LocalDefaultFlowConstant;
import com.yomahub.liteflow.enums.CmpStepTypeEnum;
import com.yomahub.liteflow.enums.ConditionTypeEnum; import com.yomahub.liteflow.enums.ConditionTypeEnum;
import com.yomahub.liteflow.enums.ParallelStrategyEnum; import com.yomahub.liteflow.enums.ParallelStrategyEnum;
import com.yomahub.liteflow.flow.element.Condition; import com.yomahub.liteflow.flow.element.Condition;
import com.yomahub.liteflow.flow.entity.CmpStep;
import com.yomahub.liteflow.flow.parallel.strategy.ParallelStrategyExecutor; import com.yomahub.liteflow.flow.parallel.strategy.ParallelStrategyExecutor;
import com.yomahub.liteflow.flow.parallel.strategy.ParallelStrategyHelper; import com.yomahub.liteflow.flow.parallel.strategy.ParallelStrategyHelper;
import com.yomahub.liteflow.log.LFLog; import com.yomahub.liteflow.log.LFLog;
import com.yomahub.liteflow.log.LFLoggerManager; import com.yomahub.liteflow.log.LFLoggerManager;
import com.yomahub.liteflow.slot.DataBus;
import com.yomahub.liteflow.slot.Slot;
import com.yomahub.liteflow.thread.ExecutorHelper; import com.yomahub.liteflow.thread.ExecutorHelper;
import java.util.Set; import java.util.Set;
@ -64,13 +68,16 @@ public class WhenCondition extends Condition {
// 使用线程池执行 when 并发流程 // 使用线程池执行 when 并发流程
// 这块涉及到挺多的多线程逻辑所以注释比较详细看到这里的童鞋可以仔细阅读 // 这块涉及到挺多的多线程逻辑所以注释比较详细看到这里的童鞋可以仔细阅读
private void executeAsyncCondition(Integer slotIndex) throws Exception { private void executeAsyncCondition(Integer slotIndex) throws Exception {
Slot slot = DataBus.getSlot(slotIndex);
slot.addStep(new CmpStep("-1", "-1", CmpStepTypeEnum.WHEN_START));
// 获取并发执行策略 // 获取并发执行策略
ParallelStrategyExecutor parallelStrategyExecutor = ParallelStrategyHelper.loadInstance().buildParallelExecutor(this.getParallelStrategy()); ParallelStrategyExecutor parallelStrategyExecutor = ParallelStrategyHelper.loadInstance().buildParallelExecutor(this.getParallelStrategy());
try {
// 执行并发逻辑 // 执行并发逻辑
parallelStrategyExecutor.execute(this, slotIndex); parallelStrategyExecutor.execute(this, slotIndex);
} finally {
slot.addStep(new CmpStep("-1", "-1", CmpStepTypeEnum.WHEN_END));
}
} }
public boolean isIgnoreError() { public boolean isIgnoreError() {

View File

@ -1,6 +1,7 @@
/** /**
* <p>Title: liteflow</p> * <p>Title: liteflow</p>
* <p>Description: 轻量级的组件式流程框架</p> * <p>Description: 轻量级的组件式流程框架</p>
*
* @author Bryan.Zhang * @author Bryan.Zhang
* @email weenyc31@163.com * @email weenyc31@163.com
* @Date 2020/4/1 * @Date 2020/4/1
@ -11,6 +12,7 @@ import cn.hutool.core.collection.ConcurrentHashSet;
import cn.hutool.core.collection.ListUtil; import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import com.yomahub.liteflow.enums.CmpStepTypeEnum;
import com.yomahub.liteflow.exception.NoSuchContextBeanException; import com.yomahub.liteflow.exception.NoSuchContextBeanException;
import com.yomahub.liteflow.exception.NullParamException; import com.yomahub.liteflow.exception.NullParamException;
import com.yomahub.liteflow.flow.element.Condition; import com.yomahub.liteflow.flow.element.Condition;
@ -340,6 +342,21 @@ public class Slot {
CmpStep cmpStep; CmpStep cmpStep;
for (Iterator<CmpStep> it = executeSteps.iterator(); it.hasNext();) { for (Iterator<CmpStep> it = executeSteps.iterator(); it.hasNext();) {
cmpStep = it.next(); cmpStep = it.next();
if (CmpStepTypeEnum.WHEN_START.equals(cmpStep.getStepType())) {
str.append("");
continue;
}
if (CmpStepTypeEnum.WHEN_END.equals(cmpStep.getStepType())) {
// 如果最后一个是==>则移除最后一个==>
if (str.toString().endsWith("==>")) {
str.delete(str.length() - 3, str.length());
}
str.append("");
if (it.hasNext()) {
str.append("==>");
}
continue;
}
if (withTimeSpent) { if (withTimeSpent) {
str.append(cmpStep.buildStringWithTime()); str.append(cmpStep.buildStringWithTime());
} }
@ -376,6 +393,21 @@ public class Slot {
CmpStep cmpStep; CmpStep cmpStep;
for (Iterator<CmpStep> it = rollbackSteps.iterator(); it.hasNext();) { for (Iterator<CmpStep> it = rollbackSteps.iterator(); it.hasNext();) {
cmpStep = it.next(); cmpStep = it.next();
if (CmpStepTypeEnum.WHEN_START.equals(cmpStep.getStepType())) {
str.append("");
continue;
}
if (CmpStepTypeEnum.WHEN_END.equals(cmpStep.getStepType())) {
// 如果最后一个是==>则移除最后一个==>
if (str.toString().endsWith("==>")) {
str.delete(str.length() - 3, str.length());
}
str.append("");
if (it.hasNext()) {
str.append("==>");
}
continue;
}
if (withRollbackTimeSpent) { if (withRollbackTimeSpent) {
str.append(cmpStep.buildRollbackStringWithTime()); str.append(cmpStep.buildRollbackStringWithTime());
} }

View File

@ -60,6 +60,12 @@ public class NacosXmlELParser extends ClassXmlFlowELParser {
if (StrUtil.isBlank(nacosParserVO.getPassword())) { if (StrUtil.isBlank(nacosParserVO.getPassword())) {
nacosParserVO.setPassword(""); nacosParserVO.setPassword("");
} }
if (StrUtil.isBlank(nacosParserVO.getAccessKey())){
nacosParserVO.setAccessKey("");
}
if (StrUtil.isBlank(nacosParserVO.getSecretKey())){
nacosParserVO.setSecretKey("");
}
helper = new NacosParserHelper(nacosParserVO); helper = new NacosParserHelper(nacosParserVO);
} }
catch (Exception e) { catch (Exception e) {

View File

@ -11,7 +11,6 @@ import com.yomahub.liteflow.spi.holder.ContextAwareHolder;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.text.MessageFormat;
import java.util.Properties; import java.util.Properties;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import java.util.function.Consumer; import java.util.function.Consumer;
@ -40,12 +39,8 @@ public class NacosParserHelper {
catch (Exception ignored) { catch (Exception ignored) {
} }
if (this.configService == null) { if (this.configService == null) {
Properties properties = new Properties(); Properties properties = getProperties(nacosParserVO);
properties.put(PropertyKeyConst.SERVER_ADDR, nacosParserVO.getServerAddr()); this.configService = new NacosConfigService(properties);
properties.put(PropertyKeyConst.NAMESPACE, nacosParserVO.getNamespace());
properties.put(PropertyKeyConst.USERNAME, nacosParserVO.getUsername());
properties.put(PropertyKeyConst.PASSWORD, nacosParserVO.getPassword());
this.configService = new NacosConfigService(properties);
} }
} }
catch (Exception e) { catch (Exception e) {
@ -53,6 +48,29 @@ public class NacosParserHelper {
} }
} }
private static Properties getProperties(NacosParserVO nacosParserVO) {
Properties properties = new Properties();
properties.put(PropertyKeyConst.SERVER_ADDR, nacosParserVO.getServerAddr());
properties.put(PropertyKeyConst.NAMESPACE, nacosParserVO.getNamespace());
if (StrUtil.isNotEmpty(nacosParserVO.getUsername())) {
// 用户名密码模式 填写用户名就必有密码
if (StrUtil.isEmpty(PropertyKeyConst.PASSWORD)){
throw new NacosException("Nacos config password is empty");
}
// 历史版本会使用用户名密码
properties.put(PropertyKeyConst.USERNAME, nacosParserVO.getUsername());
properties.put(PropertyKeyConst.PASSWORD, nacosParserVO.getPassword());
} else if (StrUtil.isNotEmpty(PropertyKeyConst.ACCESS_KEY)){
// 以下为阿里云RAM子账号使用 填写了ak就必有sk
if (StrUtil.isEmpty(PropertyKeyConst.SECRET_KEY)){
throw new NacosException("Nacos config secretKey is empty");
}
properties.put(PropertyKeyConst.ACCESS_KEY, nacosParserVO.getAccessKey());
properties.put(PropertyKeyConst.SECRET_KEY, nacosParserVO.getSecretKey());
}
return properties;
}
public String getContent() { public String getContent() {
try { try {
return configService.getConfig(nacosParserVO.getDataId(), nacosParserVO.getGroup(), 3000L); return configService.getConfig(nacosParserVO.getDataId(), nacosParserVO.getGroup(), 3000L);

View File

@ -16,6 +16,10 @@ public class NacosParserVO {
private String group; private String group;
private String accessKey;
private String secretKey;
private String username; private String username;
private String password; private String password;
@ -68,11 +72,33 @@ public class NacosParserVO {
this.password = password; this.password = password;
} }
@Override public String getAccessKey() {
public String toString() { return accessKey;
return "NacosParserVO{" + "serverAddr='" + serverAddr + '\'' + ", namespace='" + namespace + '\'' + ", dataId='"
+ dataId + '\'' + ", group='" + group + '\'' + ", username='" + username + '\'' + ", password='"
+ password + '\'' + '}';
} }
public void setAccessKey(String accessKey) {
this.accessKey = accessKey;
}
public String getSecretKey() {
return secretKey;
}
public void setSecretKey(String secretKey) {
this.secretKey = secretKey;
}
@Override
public String toString() {
return "NacosParserVO{" +
"serverAddr='" + serverAddr + '\'' +
", namespace='" + namespace + '\'' +
", dataId='" + dataId + '\'' +
", group='" + group + '\'' +
", accessKey='" + accessKey + '\'' +
", secretKey='" + secretKey + '\'' +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
} }