Skip to content

Commit

Permalink
Develop optimize controplugin 20221214 (#9715)
Browse files Browse the repository at this point in the history
* 限流插件优化

* 反脆弱插件移除nacos-sys依赖

* 移除nacos-sys依赖
  • Loading branch information
shiyiyue1102 authored Dec 16, 2022
1 parent 0759e78 commit 1322486
Show file tree
Hide file tree
Showing 9 changed files with 716 additions and 72 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright 1999-2020 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.alibaba.nacos.core.control;

import com.alibaba.nacos.common.utils.StringUtils;
import com.alibaba.nacos.plugin.control.configs.ControlConfigs;
import com.alibaba.nacos.plugin.control.configs.ControlConfigsInitializer;
import com.alibaba.nacos.sys.env.EnvUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
* spring value for control configs.
*
* @author shiyiyue
*/
@Component
public class SpringValueConfigsInitializer implements ControlConfigsInitializer {

@Value("${nacos.plugin.control.tps.barrier.creator:nacos}")
private String tpsBarrierCreator = "nacos";

@Value("${nacos.plugin.control.tps.barrier.rule.creator:nacos}")
private String tpsRuleBarrierCreator = "nacos";

@Value("${nacos.plugin.control.connection.runtime.ejector:nacos}")
private String connectionRuntimeEjector = "nacos";

@Value("${nacos.plugin.control.connection.manager:nacos}")
private String connectionManager = "nacos";

@Value("${nacos.plugin.control.tps.manager:nacos}")
private String tpsManager = "nacos";

@Value("${nacos.plugin.control.rule.external.storage:}")
private String ruleExternalStorage = "";

@Value("${nacos.plugin.control.rule.parser:nacos}")
private String ruleParser = "nacos";

@Value("${nacos.plugin.control.rule.local.basedir:}")
private String localRuleStorageBaseDir = "";

@Override
public void initialize(ControlConfigs controlConfigs) {
controlConfigs.setTpsManager(tpsManager);
controlConfigs.setTpsBarrierCreator(tpsBarrierCreator);
controlConfigs.setTpsRuleBarrierCreator(tpsRuleBarrierCreator);

controlConfigs.setConnectionRuntimeEjector(connectionRuntimeEjector);
controlConfigs.setConnectionManager(connectionManager);

controlConfigs.setRuleParser(ruleParser);
if (StringUtils.isNotBlank(localRuleStorageBaseDir)) {
controlConfigs.setLocalRuleStorageBaseDir(localRuleStorageBaseDir);
} else {
controlConfigs.setLocalRuleStorageBaseDir(EnvUtil.getNacosHome());
}
controlConfigs.setRuleExternalStorage(ruleExternalStorage);

}
}
5 changes: 1 addition & 4 deletions plugin/control/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@
<artifactId>nacos-common</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-sys</artifactId>
</dependency>

</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import com.alibaba.nacos.plugin.control.connection.nacos.NacosConnectionControlManager;
import com.alibaba.nacos.plugin.control.event.ConnectionLimitRuleChangeEvent;
import com.alibaba.nacos.plugin.control.event.TpsControlRuleChangeEvent;
import com.alibaba.nacos.plugin.control.ruleactivator.NacosRuleParser;
import com.alibaba.nacos.plugin.control.ruleactivator.RuleParser;
import com.alibaba.nacos.plugin.control.ruleactivator.RuleParserProxy;
import com.alibaba.nacos.plugin.control.ruleactivator.RuleStorageProxy;
import com.alibaba.nacos.plugin.control.tps.TpsControlManager;
import com.alibaba.nacos.plugin.control.tps.nacos.NacosTpsControlManager;
Expand All @@ -44,46 +44,8 @@ public class ControlManagerCenter {

private ConnectionControlManager connectionControlManager;

private RuleParser ruleParser;

private RuleStorageProxy ruleStorageProxy;

private void initRuleParser() {
Collection<RuleParser> ruleParsers = NacosServiceLoader.load(RuleParser.class);
String ruleParserName = ControlConfigs.getInstance().getRuleParser();

for (RuleParser ruleParserInternal : ruleParsers) {
if (ruleParserInternal.getName().equalsIgnoreCase(ruleParserName)) {
Loggers.CONTROL.info("Found rule parser of name={},class={}", ruleParserName,
ruleParserInternal.getClass().getSimpleName());
ruleParser = ruleParserInternal;
break;
}
}
if (ruleParser == null) {
Loggers.CONTROL.warn("Fail to rule parser of name :" + ruleParserName);
ruleParser = new NacosRuleParser();
}

Collection<ConnectionControlManager> connectionControlManagers = NacosServiceLoader
.load(ConnectionControlManager.class);
String connectionManagerName = ControlConfigs.getInstance().getConnectionManager();

for (ConnectionControlManager connectionControlManagerInternal : connectionControlManagers) {
if (connectionControlManagerInternal.getName().equalsIgnoreCase(connectionManagerName)) {
Loggers.CONTROL.info("Found rule parser of name={},class={}", ruleParserName,
connectionControlManagerInternal.getClass().getSimpleName());
connectionControlManager = connectionControlManagerInternal;
break;
}
}
if (connectionControlManager == null) {
Loggers.CONTROL.warn("Fail to rule parser of name :" + ruleParserName);
connectionControlManager = new NacosConnectionControlManager();
}

}

private void initConnectionManager() {

Collection<ConnectionControlManager> connectionControlManagers = NacosServiceLoader
Expand Down Expand Up @@ -127,7 +89,6 @@ private void initTpsControlManager() {

private ControlManagerCenter() {
initTpsControlManager();
initRuleParser();
initConnectionManager();
ruleStorageProxy = new RuleStorageProxy();
}
Expand All @@ -137,7 +98,7 @@ public RuleStorageProxy getRuleStorageProxy() {
}

public RuleParser getRuleParser() {
return ruleParser;
return RuleParserProxy.getInstance();
}

public TpsControlManager getTpsControlManager() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,29 @@

package com.alibaba.nacos.plugin.control.configs;

import com.alibaba.nacos.plugin.control.Loggers;
import com.alibaba.nacos.sys.utils.ApplicationUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import com.alibaba.nacos.common.spi.NacosServiceLoader;

import java.util.Collection;

/**
* control configs params.
*
* @author shiyiyue
*/
@Component
public class ControlConfigs {

private static ControlConfigs instance = null;

public static ControlConfigs getInstance() {
if (instance == null) {
synchronized (ControlConfigs.class) {
try {
instance = ApplicationUtils.getBean(ControlConfigs.class);
} catch (Throwable throwable) {
Loggers.CONTROL
.warn("Fail to get control configs bean from spring context,use default constructor instance",
throwable);
}
if (instance == null) {
instance = new ControlConfigs();
Collection<ControlConfigsInitializer> load = NacosServiceLoader
.load(ControlConfigsInitializer.class);
for (ControlConfigsInitializer controlConfigsInitializer : load) {
controlConfigsInitializer.initialize(instance);
}
}
}
}
Expand All @@ -54,27 +50,22 @@ public static void setInstance(ControlConfigs instance) {
ControlConfigs.instance = instance;
}

@Value("${nacos.plugin.control.tps.barrier.creator:nacos}")
private String tpsBarrierCreator = "nacos";

@Value("${nacos.plugin.control.tps.barrier.rule.creator:nacos}")
private String tpsRuleBarrierCreator = "nacos";

@Value("${nacos.plugin.control.connection.runtime.ejector:nacos}")
private String connectionRuntimeEjector = "nacos";

@Value("${nacos.plugin.control.connection.manager:nacos}")
private String connectionManager = "nacos";

@Value("${nacos.plugin.control.tps.manager:nacos}")
private String tpsManager = "nacos";

@Value("${nacos.plugin.control.rule.external.storage:}")
private String ruleExternalStorage = "";

@Value("${nacos.plugin.control.rule.parser:nacos}")
private String ruleParser = "nacos";

private String localRuleStorageBaseDir = "";

public String getTpsBarrierCreator() {
return tpsBarrierCreator;
}
Expand Down Expand Up @@ -130,4 +121,12 @@ public String getTpsManager() {
public void setTpsManager(String tpsManager) {
this.tpsManager = tpsManager;
}

public String getLocalRuleStorageBaseDir() {
return localRuleStorageBaseDir;
}

public void setLocalRuleStorageBaseDir(String localRuleStorageBaseDir) {
this.localRuleStorageBaseDir = localRuleStorageBaseDir;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 1999-2020 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.alibaba.nacos.plugin.control.configs;

/**
* control plugin configs initializer.
*
* @author shiyiyue
*/
public interface ControlConfigsInitializer {

/**
* init control configs.
*
* @param controlConfigs control configs.
*/
void initialize(ControlConfigs controlConfigs);

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,12 @@
import com.alibaba.nacos.plugin.control.event.TpsControlRuleChangeEvent;
import com.alibaba.nacos.plugin.control.tps.rule.TpsControlRule;
import org.slf4j.Logger;
import org.springframework.stereotype.Component;

/**
* control rule activator.
* @author shiyiyue
*
* @author shiyiyue
*/
@Component
public class ControlRuleChangeActivator {

private static final Logger LOGGER = Loggers.CONTROL;
Expand Down
Loading

0 comments on commit 1322486

Please sign in to comment.