----初始化项目

This commit is contained in:
2025-09-19 20:49:14 +08:00
parent b345d2828d
commit df7765c400
2867 changed files with 359313 additions and 89 deletions

View File

@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 1999-2023 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.
-->
<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>nacos-plugin-default-impl</artifactId>
<groupId>com.alibaba.nacos</groupId>
<version>${revision}</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>default-control-plugin</artifactId>
<name>nacos-default-control-plugin ${project.version}</name>
<dependencies>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-control-plugin</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-common</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,72 @@
/*
* Copyright 1999-2023 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.impl;
import com.alibaba.nacos.common.utils.JacksonUtils;
import com.alibaba.nacos.plugin.control.Loggers;
import com.alibaba.nacos.plugin.control.connection.ConnectionControlManager;
import com.alibaba.nacos.plugin.control.connection.ConnectionMetricsCollector;
import com.alibaba.nacos.plugin.control.connection.request.ConnectionCheckRequest;
import com.alibaba.nacos.plugin.control.connection.response.ConnectionCheckCode;
import com.alibaba.nacos.plugin.control.connection.response.ConnectionCheckResponse;
import com.alibaba.nacos.plugin.control.connection.rule.ConnectionControlRule;
import java.util.Map;
import java.util.stream.Collectors;
/**
* Nacos default control plugin implementation.
*
* @author shiyiyue
*/
public class NacosConnectionControlManager extends ConnectionControlManager {
@Override
public String getName() {
return "nacos";
}
public NacosConnectionControlManager() {
super();
}
@Override
public void applyConnectionLimitRule(ConnectionControlRule connectionControlRule) {
super.connectionControlRule = connectionControlRule;
Loggers.CONTROL.info("Connection control rule updated to ->" + (this.connectionControlRule == null ? null
: JacksonUtils.toJson(this.connectionControlRule)));
Loggers.CONTROL.warn("Connection control updated, But connection control manager is no limit implementation.");
}
@Override
public ConnectionCheckResponse check(ConnectionCheckRequest connectionCheckRequest) {
ConnectionCheckResponse connectionCheckResponse = new ConnectionCheckResponse();
connectionCheckResponse.setSuccess(true);
connectionCheckResponse.setCode(ConnectionCheckCode.PASS_BY_TOTAL);
int totalCountLimit = connectionControlRule.getCountLimit();
// Get total connection from metrics
Map<String, Integer> metricsTotalCount = metricsCollectorList.stream().collect(
Collectors.toMap(ConnectionMetricsCollector::getName, ConnectionMetricsCollector::getTotalCount));
int totalCount = metricsTotalCount.values().stream().mapToInt(Integer::intValue).sum();
if (totalCount >= totalCountLimit) {
connectionCheckResponse.setSuccess(false);
connectionCheckResponse.setCode(ConnectionCheckCode.DENY_BY_TOTAL_OVER);
}
return connectionCheckResponse;
}
}

View File

@ -0,0 +1,44 @@
/*
* Copyright 1999-2023 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.impl;
import com.alibaba.nacos.plugin.control.connection.ConnectionControlManager;
import com.alibaba.nacos.plugin.control.spi.ControlManagerBuilder;
import com.alibaba.nacos.plugin.control.tps.TpsControlManager;
/**
* Nacos default control plugin implementation.
*
* @author xiweng.yy
*/
public class NacosControlManagerBuilder implements ControlManagerBuilder {
@Override
public String getName() {
return "nacos";
}
@Override
public ConnectionControlManager buildConnectionControlManager() {
return new NacosConnectionControlManager();
}
@Override
public TpsControlManager buildTpsControlManager() {
return new NacosTpsControlManager();
}
}

View File

@ -0,0 +1,193 @@
/*
* Copyright 1999-2023 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.impl;
import com.alibaba.nacos.common.executor.ExecutorFactory;
import com.alibaba.nacos.plugin.control.Loggers;
import com.alibaba.nacos.plugin.control.tps.TpsControlManager;
import com.alibaba.nacos.plugin.control.tps.TpsMetrics;
import com.alibaba.nacos.plugin.control.tps.barrier.TpsBarrier;
import com.alibaba.nacos.plugin.control.tps.request.TpsCheckRequest;
import com.alibaba.nacos.plugin.control.tps.response.TpsCheckResponse;
import com.alibaba.nacos.plugin.control.tps.response.TpsResultCode;
import com.alibaba.nacos.plugin.control.tps.rule.TpsControlRule;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/**
* Nacos default control plugin implementation.
*
* @author shiyiyue
*/
public class NacosTpsControlManager extends TpsControlManager {
/**
* point name -> tps barrier.
*/
protected final Map<String, TpsBarrier> points = new ConcurrentHashMap<>(16);
/**
* point name -> tps control rule.
*/
protected final Map<String, TpsControlRule> rules = new ConcurrentHashMap<>(16);
protected ScheduledExecutorService executorService;
public NacosTpsControlManager() {
super();
executorService = ExecutorFactory.newSingleScheduledExecutorService(r -> {
Thread thread = new Thread(r, "nacos.plugin.tps.control.reporter");
thread.setDaemon(true);
return thread;
});
startTpsReport();
}
protected void startTpsReport() {
executorService
.scheduleWithFixedDelay(new NacosTpsControlManager.TpsMetricsReporter(), 0, 900, TimeUnit.MILLISECONDS);
}
/**
* apple tps rule.
*
* @param pointName pointName.
*/
public synchronized void registerTpsPoint(String pointName) {
if (!points.containsKey(pointName)) {
points.put(pointName, tpsBarrierCreator.createTpsBarrier(pointName));
if (rules.containsKey(pointName)) {
points.get(pointName).applyRule(rules.get(pointName));
} else {
initTpsRule(pointName);
}
}
}
/**
* apple tps rule.
*
* @param pointName pointName.
* @param rule rule.
*/
public synchronized void applyTpsRule(String pointName, TpsControlRule rule) {
if (rule == null) {
rules.remove(pointName);
} else {
rules.put(pointName, rule);
}
if (points.containsKey(pointName)) {
points.get(pointName).applyRule(rule);
}
}
public Map<String, TpsBarrier> getPoints() {
return points;
}
public Map<String, TpsControlRule> getRules() {
return rules;
}
/**
* check tps result.
*
* @param tpsRequest TpsRequest.
* @return check current tps is allowed.
*/
public TpsCheckResponse check(TpsCheckRequest tpsRequest) {
if (points.containsKey(tpsRequest.getPointName())) {
try {
return points.get(tpsRequest.getPointName()).applyTps(tpsRequest);
} catch (Throwable throwable) {
Loggers.TPS.warn("[{}]apply tps error,error={}", tpsRequest.getPointName(), throwable);
}
}
return new TpsCheckResponse(true, TpsResultCode.CHECK_SKIP, "skip");
}
class TpsMetricsReporter implements Runnable {
long lastReportSecond = 0L;
/**
* get format string "2021-01-16 17:20:21" of timestamp.
*
* @param timeStamp timestamp milliseconds.
* @return
*/
public String getTimeFormatOfSecond(long timeStamp) {
String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(timeStamp));
return format;
}
@Override
public void run() {
try {
long now = System.currentTimeMillis();
StringBuilder stringBuilder = new StringBuilder();
Set<Map.Entry<String, TpsBarrier>> entries = points.entrySet();
long tempSecond = 0L;
long metricsTime = now - 1000L;
String formatString = getTimeFormatOfSecond(metricsTime);
for (Map.Entry<String, TpsBarrier> entry : entries) {
TpsBarrier tpsBarrier = entry.getValue();
String pointName = entry.getKey();
TpsMetrics metrics = tpsBarrier.getPointBarrier().getMetrics(metricsTime);
if (metrics != null) {
//already reported.
if (lastReportSecond != 0L && lastReportSecond == metrics.getTimeStamp()) {
continue;
}
tempSecond = metrics.getTimeStamp();
stringBuilder.append(pointName).append("|").append("point").append("|")
.append(metrics.getPeriod()).append("|").append(formatString).append("|")
.append(metrics.getCounter().getPassCount()).append("|")
.append(metrics.getCounter().getDeniedCount()).append("|").append("\n");
}
}
if (tempSecond > 0) {
lastReportSecond = tempSecond;
}
if (stringBuilder.length() > 0) {
Loggers.TPS.info("Tps reporting...\n" + stringBuilder.toString());
}
} catch (Throwable throwable) {
Loggers.TPS.error("Tps reporting error", throwable);
}
}
}
@Override
public String getName() {
return "nacos";
}
}

View File

@ -0,0 +1,17 @@
#
# Copyright 1999-2023 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.
#
com.alibaba.nacos.plugin.control.impl.NacosControlManagerBuilder

View File

@ -0,0 +1,58 @@
/*
* Copyright 1999-2023 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.impl;
import com.alibaba.nacos.plugin.control.connection.request.ConnectionCheckRequest;
import com.alibaba.nacos.plugin.control.connection.response.ConnectionCheckResponse;
import com.alibaba.nacos.plugin.control.connection.rule.ConnectionControlRule;
import org.junit.Assert;
import org.junit.Test;
public class NacosConnectionControlManagerTest {
@Test
public void testApplyConnectionLimitRule() {
NacosConnectionControlManager nacosConnectionControlManager = new NacosConnectionControlManager();
ConnectionControlRule connectionControlRule = new ConnectionControlRule();
connectionControlRule.setCountLimit(10);
nacosConnectionControlManager.applyConnectionLimitRule(connectionControlRule);
ConnectionControlRule connectionLimitRule = nacosConnectionControlManager.getConnectionLimitRule();
Assert.assertEquals(connectionControlRule, connectionLimitRule);
}
@Test
public void testCheckLimit() {
NacosConnectionControlManager nacosConnectionControlManager = new NacosConnectionControlManager();
ConnectionControlRule connectionControlRule = new ConnectionControlRule();
connectionControlRule.setCountLimit(10);
nacosConnectionControlManager.applyConnectionLimitRule(connectionControlRule);
ConnectionCheckRequest connectionCheckRequest = new ConnectionCheckRequest("127.0.0.1", "test", "test");
ConnectionCheckResponse connectionCheckResponse = nacosConnectionControlManager.check(connectionCheckRequest);
Assert.assertFalse(connectionCheckResponse.isSuccess());
}
@Test
public void testCheckUnLimit() {
NacosConnectionControlManager nacosConnectionControlManager = new NacosConnectionControlManager();
ConnectionControlRule connectionControlRule = new ConnectionControlRule();
connectionControlRule.setCountLimit(30);
nacosConnectionControlManager.applyConnectionLimitRule(connectionControlRule);
ConnectionCheckRequest connectionCheckRequest = new ConnectionCheckRequest("127.0.0.1", "test", "test");
ConnectionCheckResponse connectionCheckResponse = nacosConnectionControlManager.check(connectionCheckRequest);
Assert.assertTrue(connectionCheckResponse.isSuccess());
}
}

View File

@ -0,0 +1,37 @@
/*
* Copyright 1999-2023 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.impl;
import com.alibaba.nacos.plugin.control.connection.ConnectionControlManager;
import com.alibaba.nacos.plugin.control.tps.TpsControlManager;
import org.junit.Assert;
import org.junit.Test;
public class NacosControlManagerBuilderTest {
@Test
public void test() {
NacosControlManagerBuilder nacosControlManagerBuilder = new NacosControlManagerBuilder();
ConnectionControlManager connectionControlManager = nacosControlManagerBuilder.buildConnectionControlManager();
TpsControlManager tpsControlManager = nacosControlManagerBuilder.buildTpsControlManager();
Assert.assertEquals("nacos", tpsControlManager.getName());
Assert.assertEquals("nacos", connectionControlManager.getName());
Assert.assertEquals("nacos", nacosControlManagerBuilder.getName());
}
}

View File

@ -0,0 +1,87 @@
/*
* Copyright 1999-2023 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.impl;
import com.alibaba.nacos.plugin.control.tps.MonitorType;
import com.alibaba.nacos.plugin.control.tps.request.TpsCheckRequest;
import com.alibaba.nacos.plugin.control.tps.response.TpsCheckResponse;
import com.alibaba.nacos.plugin.control.tps.rule.RuleDetail;
import com.alibaba.nacos.plugin.control.tps.rule.TpsControlRule;
import org.junit.Assert;
import org.junit.Test;
import java.util.concurrent.TimeUnit;
public class NacosTpsControlManagerTest {
@Test
public void testRegisterTpsPoint1() {
NacosTpsControlManager nacosTpsControlManager = new NacosTpsControlManager();
nacosTpsControlManager.registerTpsPoint("test");
Assert.assertTrue(nacosTpsControlManager.getPoints().containsKey("test"));
}
@Test
public void testRegisterTpsPoint2() {
NacosTpsControlManager nacosTpsControlManager = new NacosTpsControlManager();
TpsControlRule tpsLimitRule = new TpsControlRule();
nacosTpsControlManager.applyTpsRule("test", tpsLimitRule);
nacosTpsControlManager.registerTpsPoint("test");
Assert.assertTrue(nacosTpsControlManager.getPoints().containsKey("test"));
}
@Test
public void testApplyTpsRule1() {
NacosTpsControlManager nacosTpsControlManager = new NacosTpsControlManager();
TpsControlRule tpsLimitRule = new TpsControlRule();
nacosTpsControlManager.applyTpsRule("test", tpsLimitRule);
Assert.assertTrue(nacosTpsControlManager.getRules().containsKey("test"));
}
@Test
public void testApplyTpsRule2() {
NacosTpsControlManager nacosTpsControlManager = new NacosTpsControlManager();
nacosTpsControlManager.applyTpsRule("test", null);
Assert.assertFalse(nacosTpsControlManager.getRules().containsKey("test"));
}
@Test
public void testCheck() {
NacosTpsControlManager nacosTpsControlManager = new NacosTpsControlManager();
nacosTpsControlManager.registerTpsPoint("test");
final TpsControlRule tpsLimitRule = new TpsControlRule();
RuleDetail ruleDetail = new RuleDetail();
ruleDetail.setMaxCount(5);
ruleDetail.setMonitorType(MonitorType.INTERCEPT.getType());
ruleDetail.setPeriod(TimeUnit.SECONDS);
tpsLimitRule.setPointRule(ruleDetail);
tpsLimitRule.setPointName("test");
nacosTpsControlManager.applyTpsRule("test", tpsLimitRule);
long timeMillis = System.currentTimeMillis();
TpsCheckRequest tpsCheckRequest = new TpsCheckRequest();
tpsCheckRequest.setPointName("test");
tpsCheckRequest.setTimestamp(timeMillis);
TpsCheckResponse check = nacosTpsControlManager.check(tpsCheckRequest);
Assert.assertTrue(check.isSuccess());
}
}

View File

@ -0,0 +1,37 @@
/*
* Copyright 1999-2023 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.impl;
import com.alibaba.nacos.plugin.control.connection.ConnectionMetricsCollector;
public class TestConnectionMetricsCollector implements ConnectionMetricsCollector {
@Override
public String getName() {
return "test";
}
@Override
public int getTotalCount() {
return 20;
}
@Override
public int getCountForIp(String ip) {
return 10;
}
}

View File

@ -0,0 +1,35 @@
#
# 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.
#
#
#
# Copyright 1999-2021 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.
#
#
com.alibaba.nacos.plugin.control.impl.TestConnectionMetricsCollector