Refactor control plugin (#11174)

* Refactor control plugin and add default implementation.

* Move auth default plugin into other modules.

* Fix control plugin config not effect problem.

* Fix UT error.
This commit is contained in:
杨翊 SionYang 2023-09-22 16:59:25 +08:00 committed by GitHub
parent a9d303cbe3
commit 51b6a443d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
122 changed files with 1030 additions and 661 deletions

View File

@ -41,7 +41,7 @@
<dependency> <dependency>
<groupId>com.alibaba.nacos</groupId> <groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-plugin-default-impl</artifactId> <artifactId>nacos-default-plugin-all</artifactId>
<version>${project.version}</version> <version>${project.version}</version>
</dependency> </dependency>
<dependency> <dependency>

View File

@ -144,19 +144,30 @@ nacos.core.auth.plugin.nacos.token.secret.key=
#nacos.core.auth.ldap.case.sensitive=true #nacos.core.auth.ldap.case.sensitive=true
#nacos.core.auth.ldap.ignore.partial.result.exception=false #nacos.core.auth.ldap.ignore.partial.result.exception=false
#*************** Control Plugin Related Configurations ***************#
# plugin type
#nacos.plugin.control.manager.type=nacos
# local control rule storage dir, default ${nacos.home}/data/connection and ${nacos.home}/data/tps
#nacos.plugin.control.rule.local.basedir=${nacos.home}
# external control rule storage type, if exist
#nacos.plugin.control.rule.external.storage=
#*************** Config Change Plugin Related Configurations ***************# #*************** Config Change Plugin Related Configurations ***************#
# webhook # webhook
nacos.core.config.plugin.webhook.enabled=false #nacos.core.config.plugin.webhook.enabled=false
# It is recommended to use EB https://help.aliyun.com/document_detail/413974.html # It is recommended to use EB https://help.aliyun.com/document_detail/413974.html
nacos.core.config.plugin.webhook.url=http://localhost:8080/webhook/send?token=*** #nacos.core.config.plugin.webhook.url=http://localhost:8080/webhook/send?token=***
# The content push max capacity ,byte # The content push max capacity ,byte
nacos.core.config.plugin.webhook.contentMaxCapacity=102400 #nacos.core.config.plugin.webhook.contentMaxCapacity=102400
# whitelist # whitelist
nacos.core.config.plugin.whitelist.enabled=false #nacos.core.config.plugin.whitelist.enabled=false
# The import file suffixs # The import file suffixs
nacos.core.config.plugin.whitelist.suffixs=xml,text,properties,yaml,html #nacos.core.config.plugin.whitelist.suffixs=xml,text,properties,yaml,html
# fileformatcheck,which validate the import file of type and content # fileformatcheck,which validate the import file of type and content
nacos.core.config.plugin.fileformatcheck.enabled=false #nacos.core.config.plugin.fileformatcheck.enabled=false
#*************** Istio Related Configurations ***************# #*************** Istio Related Configurations ***************#
### If turn on the MCP server: ### If turn on the MCP server:

View File

@ -20,57 +20,37 @@ import com.alibaba.nacos.common.utils.StringUtils;
import com.alibaba.nacos.plugin.control.configs.ControlConfigs; import com.alibaba.nacos.plugin.control.configs.ControlConfigs;
import com.alibaba.nacos.plugin.control.configs.ControlConfigsInitializer; import com.alibaba.nacos.plugin.control.configs.ControlConfigsInitializer;
import com.alibaba.nacos.sys.env.EnvUtil; import com.alibaba.nacos.sys.env.EnvUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/** /**
* spring value for control configs. * spring value for control configs.
* *
* @author shiyiyue * @author shiyiyue
*/ */
@Component
public class SpringValueConfigsInitializer implements ControlConfigsInitializer { public class SpringValueConfigsInitializer implements ControlConfigsInitializer {
@Value("${nacos.plugin.control.tps.barrier.creator:nacos}") private static final String PREFIX = "nacos.plugin.control.";
private String tpsBarrierCreator = "nacos";
@Value("${nacos.plugin.control.tps.barrier.rule.creator:nacos}") private static final String CONNECTION_RUNTIME_EJECTOR = PREFIX + "connection.runtime.ejector";
private String tpsRuleBarrierCreator = "nacos";
@Value("${nacos.plugin.control.connection.runtime.ejector:nacos}") private static final String CONTROL_MANAGER_TYPE = PREFIX + "manager.type";
private String connectionRuntimeEjector = "nacos";
@Value("${nacos.plugin.control.connection.manager:nacos}") private static final String RULE_EXTERNAL_STORAGE = PREFIX + "rule.external.storage";
private String connectionManager = "nacos";
@Value("${nacos.plugin.control.tps.manager:nacos}") private static final String LOCAL_RULE_STORAGE_BASE_DIR = PREFIX + "rule.local.basedir";
private String tpsManager = "nacos";
@Value("${nacos.plugin.control.rule.external.storage:}") private static final String DEFAULT_CONNECTION_RUNTIME_EJECTOR = "nacos";
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 @Override
public void initialize(ControlConfigs controlConfigs) { public void initialize(ControlConfigs controlConfigs) {
controlConfigs.setTpsManager(tpsManager); controlConfigs.setConnectionRuntimeEjector(
controlConfigs.setTpsBarrierCreator(tpsBarrierCreator); EnvUtil.getProperty(CONNECTION_RUNTIME_EJECTOR, DEFAULT_CONNECTION_RUNTIME_EJECTOR));
controlConfigs.setTpsRuleBarrierCreator(tpsRuleBarrierCreator); String localRuleStorageBaseDir = EnvUtil.getProperty(LOCAL_RULE_STORAGE_BASE_DIR);
controlConfigs.setConnectionRuntimeEjector(connectionRuntimeEjector);
controlConfigs.setConnectionManager(connectionManager);
controlConfigs.setRuleParser(ruleParser);
if (StringUtils.isNotBlank(localRuleStorageBaseDir)) { if (StringUtils.isNotBlank(localRuleStorageBaseDir)) {
controlConfigs.setLocalRuleStorageBaseDir(localRuleStorageBaseDir); controlConfigs.setLocalRuleStorageBaseDir(localRuleStorageBaseDir);
} else { } else {
controlConfigs.setLocalRuleStorageBaseDir(EnvUtil.getNacosHome()); controlConfigs.setLocalRuleStorageBaseDir(EnvUtil.getNacosHome());
} }
controlConfigs.setRuleExternalStorage(ruleExternalStorage); controlConfigs.setRuleExternalStorage(EnvUtil.getProperty(RULE_EXTERNAL_STORAGE));
controlConfigs.setControlManagerType(EnvUtil.getProperty(CONTROL_MANAGER_TYPE));
} }
} }

View File

@ -30,7 +30,7 @@ public class TpsControlConfig {
* tps control is enabled. * tps control is enabled.
* @return true/false. * @return true/false.
*/ */
public static final boolean isTpsControlEnabled() { public static boolean isTpsControlEnabled() {
return true; return true;
} }
} }

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.core.control.SpringValueConfigsInitializer

View File

@ -172,6 +172,30 @@ nacos.core.auth.plugin.nacos.token.secret.key=
#nacos.core.auth.ldap.case.sensitive=true #nacos.core.auth.ldap.case.sensitive=true
#nacos.core.auth.ldap.ignore.partial.result.exception=false #nacos.core.auth.ldap.ignore.partial.result.exception=false
#*************** Control Plugin Related Configurations ***************#
# plugin type
#nacos.plugin.control.manager.type=nacos
# local control rule storage dir, default ${nacos.home}/data/connection and ${nacos.home}/data/tps
#nacos.plugin.control.rule.local.basedir=${nacos.home}
# external control rule storage type, if exist
#nacos.plugin.control.rule.external.storage=
#*************** Config Change Plugin Related Configurations ***************#
# webhook
#nacos.core.config.plugin.webhook.enabled=false
# It is recommended to use EB https://help.aliyun.com/document_detail/413974.html
#nacos.core.config.plugin.webhook.url=http://localhost:8080/webhook/send?token=***
# The content push max capacity ,byte
#nacos.core.config.plugin.webhook.contentMaxCapacity=102400
# whitelist
#nacos.core.config.plugin.whitelist.enabled=false
# The import file suffixs
#nacos.core.config.plugin.whitelist.suffixs=xml,text,properties,yaml,html
# fileformatcheck,which validate the import file of type and content
#nacos.core.config.plugin.fileformatcheck.enabled=false
#*************** Istio Related Configurations ***************# #*************** Istio Related Configurations ***************#
### If turn on the MCP server: ### If turn on the MCP server:

View File

@ -21,6 +21,7 @@ import com.alibaba.nacos.api.naming.pojo.ServiceInfo;
import com.alibaba.nacos.naming.monitor.MetricsMonitor; import com.alibaba.nacos.naming.monitor.MetricsMonitor;
import com.alibaba.nacos.naming.pojo.Subscriber; import com.alibaba.nacos.naming.pojo.Subscriber;
import com.alibaba.nacos.plugin.control.tps.TpsControlManager; import com.alibaba.nacos.plugin.control.tps.TpsControlManager;
import com.alibaba.nacos.sys.env.EnvUtil;
import com.alibaba.nacos.sys.utils.ApplicationUtils; import com.alibaba.nacos.sys.utils.ApplicationUtils;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -28,6 +29,7 @@ import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.mock.env.MockEnvironment;
import java.util.ArrayList; import java.util.ArrayList;
@ -63,6 +65,7 @@ public class NacosMonitorPushResultHookTest {
@Before @Before
public void setUp() { public void setUp() {
EnvUtil.setEnvironment(new MockEnvironment());
MetricsMonitor.resetAll(); MetricsMonitor.resetAll();
serviceInfo.setHosts(new ArrayList<>()); serviceInfo.setHosts(new ArrayList<>());
subscriber.setIp("0.0.0.0"); subscriber.setIp("0.0.0.0");

View File

@ -26,6 +26,7 @@ import com.alibaba.nacos.naming.core.v2.pojo.Service;
import com.alibaba.nacos.naming.monitor.MetricsMonitor; import com.alibaba.nacos.naming.monitor.MetricsMonitor;
import com.alibaba.nacos.naming.pojo.Subscriber; import com.alibaba.nacos.naming.pojo.Subscriber;
import com.alibaba.nacos.naming.push.v2.NoRequiredRetryException; import com.alibaba.nacos.naming.push.v2.NoRequiredRetryException;
import com.alibaba.nacos.sys.env.EnvUtil;
import com.alibaba.nacos.sys.utils.ApplicationUtils; import com.alibaba.nacos.sys.utils.ApplicationUtils;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -33,6 +34,7 @@ import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.mock.env.MockEnvironment;
import java.util.Collections; import java.util.Collections;
import java.util.Optional; import java.util.Optional;
@ -79,6 +81,7 @@ public class PushExecuteTaskTest {
@Before @Before
public void setUp() { public void setUp() {
EnvUtil.setEnvironment(new MockEnvironment());
MetricsMonitor.resetAll(); MetricsMonitor.resetAll();
when(indexesManager.getAllClientsSubscribeService(service)).thenReturn(Collections.singletonList(clientId)); when(indexesManager.getAllClientsSubscribeService(service)).thenReturn(Collections.singletonList(clientId));
when(clientManager.getClient(clientId)).thenReturn(client); when(clientManager.getClient(clientId)).thenReturn(client);

View File

@ -0,0 +1,58 @@
<?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-auth-plugin</artifactId>
<name>nacos-default-auth-plugin ${project.version}</name>
<dependencies>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-auth-plugin</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-common</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-sys</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-config</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.ldap</groupId>
<artifactId>spring-ldap-core</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 1999-2018 Alibaba Group Holding Ltd. * Copyright 1999-2023 Alibaba Group Holding Ltd.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 1999-2021 Alibaba Group Holding Ltd. * Copyright 1999-2023 Alibaba Group Holding Ltd.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.

View File

@ -77,7 +77,7 @@ public class NacosUserDetailsServiceImpl implements UserDetailsService {
} }
if (user == null) { if (user == null) {
throw new UsernameNotFoundException(username); throw new UsernameNotFoundException(String.format("User %s not found", username));
} }
return new NacosUserDetails(user); return new NacosUserDetails(user);
} }

View File

@ -1,5 +1,5 @@
# #
# Copyright 1999-2021 Alibaba Group Holding Ltd. # Copyright 1999-2023 Alibaba Group Holding Ltd.
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. # you may not use this file except in compliance with the License.

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 1999-2021 Alibaba Group Holding Ltd. * Copyright 1999-2023 Alibaba Group Holding Ltd.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 1999-2021 Alibaba Group Holding Ltd. * Copyright 1999-2023 Alibaba Group Holding Ltd.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 1999-2021 Alibaba Group Holding Ltd. * Copyright 1999-2023 Alibaba Group Holding Ltd.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 1999-2021 Alibaba Group Holding Ltd. * Copyright 1999-2023 Alibaba Group Holding Ltd.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -50,10 +50,10 @@ public class JwtTokenManagerTest {
when(authConfigs.isAuthEnabled()).thenReturn(true); when(authConfigs.isAuthEnabled()).thenReturn(true);
MockEnvironment mockEnvironment = new MockEnvironment(); MockEnvironment mockEnvironment = new MockEnvironment();
mockEnvironment.setProperty(AuthConstants.TOKEN_SECRET_KEY, Base64.getEncoder().encodeToString( mockEnvironment.setProperty(AuthConstants.TOKEN_SECRET_KEY, Base64.getEncoder().encodeToString(
"SecretKey0123$567890$234567890123456789012345678901234567890123456789".getBytes( "SecretKey0123$567890$234567890123456789012345678901234567890123456789"
StandardCharsets.UTF_8))); .getBytes(StandardCharsets.UTF_8)));
mockEnvironment.setProperty(AuthConstants.TOKEN_EXPIRE_SECONDS, mockEnvironment
AuthConstants.DEFAULT_TOKEN_EXPIRE_SECONDS.toString()); .setProperty(AuthConstants.TOKEN_EXPIRE_SECONDS, AuthConstants.DEFAULT_TOKEN_EXPIRE_SECONDS.toString());
EnvUtil.setEnvironment(mockEnvironment); EnvUtil.setEnvironment(mockEnvironment);
jwtTokenManager = new JwtTokenManager(authConfigs); jwtTokenManager = new JwtTokenManager(authConfigs);
@ -73,8 +73,8 @@ public class JwtTokenManagerTest {
MockEnvironment mockEnvironment = new MockEnvironment(); MockEnvironment mockEnvironment = new MockEnvironment();
mockEnvironment.setProperty(AuthConstants.TOKEN_SECRET_KEY, mockEnvironment.setProperty(AuthConstants.TOKEN_SECRET_KEY,
Base64.getEncoder().encodeToString(secretKey.getBytes(StandardCharsets.UTF_8))); Base64.getEncoder().encodeToString(secretKey.getBytes(StandardCharsets.UTF_8)));
mockEnvironment.setProperty(AuthConstants.TOKEN_EXPIRE_SECONDS, mockEnvironment
AuthConstants.DEFAULT_TOKEN_EXPIRE_SECONDS.toString()); .setProperty(AuthConstants.TOKEN_EXPIRE_SECONDS, AuthConstants.DEFAULT_TOKEN_EXPIRE_SECONDS.toString());
EnvUtil.setEnvironment(mockEnvironment); EnvUtil.setEnvironment(mockEnvironment);
@ -119,8 +119,8 @@ public class JwtTokenManagerTest {
MockEnvironment mockEnvironment = new MockEnvironment(); MockEnvironment mockEnvironment = new MockEnvironment();
mockEnvironment.setProperty(AuthConstants.TOKEN_SECRET_KEY, mockEnvironment.setProperty(AuthConstants.TOKEN_SECRET_KEY,
Base64.getEncoder().encodeToString(secretKey.getBytes(StandardCharsets.UTF_8))); Base64.getEncoder().encodeToString(secretKey.getBytes(StandardCharsets.UTF_8)));
mockEnvironment.setProperty(AuthConstants.TOKEN_EXPIRE_SECONDS, mockEnvironment
AuthConstants.DEFAULT_TOKEN_EXPIRE_SECONDS.toString()); .setProperty(AuthConstants.TOKEN_EXPIRE_SECONDS, AuthConstants.DEFAULT_TOKEN_EXPIRE_SECONDS.toString());
EnvUtil.setEnvironment(mockEnvironment); EnvUtil.setEnvironment(mockEnvironment);

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 1999-2021 Alibaba Group Holding Ltd. * Copyright 1999-2023 Alibaba Group Holding Ltd.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -26,6 +26,7 @@ import org.junit.Test;
* @date 2023/8/8 * @date 2023/8/8
*/ */
public class Base64DecodeTest { public class Base64DecodeTest {
@Test @Test
public void testStandardDecode() { public void testStandardDecode() {
String origin = "aGVsbG8sbmFjb3MhdGVzdEJhc2U2NGVuY29kZQ=="; String origin = "aGVsbG8sbmFjb3MhdGVzdEJhc2U2NGVuY29kZQ==";
@ -33,7 +34,7 @@ public class Base64DecodeTest {
byte[] decodeOrigin = Base64Decode.decode(origin); byte[] decodeOrigin = Base64Decode.decode(origin);
Assert.assertArrayEquals(decodeOrigin, expectDecodeOrigin.getBytes()); Assert.assertArrayEquals(decodeOrigin, expectDecodeOrigin.getBytes());
} }
@Test @Test
public void testNotStandardDecode() { public void testNotStandardDecode() {
String notStandardOrigin = "SecretKey012345678901234567890123456789012345678901234567890123456789"; String notStandardOrigin = "SecretKey012345678901234567890123456789012345678901234567890123456789";

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 1999-2021 Alibaba Group Holding Ltd. * Copyright 1999-2023 Alibaba Group Holding Ltd.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.

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

@ -1,5 +1,5 @@
/* /*
* Copyright 1999-2020 Alibaba Group Holding Ltd. * Copyright 1999-2023 Alibaba Group Holding Ltd.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -14,12 +14,13 @@
* limitations under the License. * limitations under the License.
*/ */
package com.alibaba.nacos.plugin.control.tps.nacos; package com.alibaba.nacos.plugin.control.impl;
import com.alibaba.nacos.common.executor.ExecutorFactory; import com.alibaba.nacos.common.executor.ExecutorFactory;
import com.alibaba.nacos.plugin.control.Loggers; import com.alibaba.nacos.plugin.control.Loggers;
import com.alibaba.nacos.plugin.control.tps.TpsBarrier; import com.alibaba.nacos.plugin.control.tps.barrier.TpsBarrier;
import com.alibaba.nacos.plugin.control.tps.TpsBarrierCreatorProxy; import com.alibaba.nacos.plugin.control.tps.barrier.creator.DefaultNacosTpsBarrierCreator;
import com.alibaba.nacos.plugin.control.tps.barrier.creator.TpsBarrierCreator;
import com.alibaba.nacos.plugin.control.tps.TpsControlManager; import com.alibaba.nacos.plugin.control.tps.TpsControlManager;
import com.alibaba.nacos.plugin.control.tps.TpsMetrics; import com.alibaba.nacos.plugin.control.tps.TpsMetrics;
import com.alibaba.nacos.plugin.control.tps.request.TpsCheckRequest; import com.alibaba.nacos.plugin.control.tps.request.TpsCheckRequest;
@ -36,7 +37,7 @@ import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
/** /**
* nacos tps control manager. * Nacos default control plugin implementation.
* *
* @author shiyiyue * @author shiyiyue
*/ */
@ -55,15 +56,20 @@ public class NacosTpsControlManager extends TpsControlManager {
protected ScheduledExecutorService executorService; protected ScheduledExecutorService executorService;
public NacosTpsControlManager() { public NacosTpsControlManager() {
super();
executorService = ExecutorFactory.newSingleScheduledExecutorService(r -> { executorService = ExecutorFactory.newSingleScheduledExecutorService(r -> {
Thread thread = new Thread(r, "nacos.plugin.tps.control.reporter"); Thread thread = new Thread(r, "nacos.plugin.tps.control.reporter");
thread.setDaemon(true); thread.setDaemon(true);
return thread; return thread;
}); });
startTpsReport(); startTpsReport();
} }
@Override
protected TpsBarrierCreator buildTpsBarrierCreator() {
return new DefaultNacosTpsBarrierCreator();
}
protected void startTpsReport() { protected void startTpsReport() {
executorService executorService
.scheduleWithFixedDelay(new NacosTpsControlManager.TpsMetricsReporter(), 0, 900, TimeUnit.MILLISECONDS); .scheduleWithFixedDelay(new NacosTpsControlManager.TpsMetricsReporter(), 0, 900, TimeUnit.MILLISECONDS);
@ -76,7 +82,7 @@ public class NacosTpsControlManager extends TpsControlManager {
*/ */
public synchronized void registerTpsPoint(String pointName) { public synchronized void registerTpsPoint(String pointName) {
if (!points.containsKey(pointName)) { if (!points.containsKey(pointName)) {
points.put(pointName, TpsBarrierCreatorProxy.getTpsBarrierCreator().createTpsBarrier(pointName)); points.put(pointName, tpsBarrierCreator.createTpsBarrier(pointName));
if (rules.containsKey(pointName)) { if (rules.containsKey(pointName)) {
points.get(pointName).applyRule(rules.get(pointName)); points.get(pointName).applyRule(rules.get(pointName));
} else { } else {

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,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>nacos-default-plugin-all</artifactId>
<dependencies>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>default-auth-plugin</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>default-control-plugin</artifactId>
<version>${revision}</version>
</dependency>
</dependencies>
</project>

View File

@ -26,35 +26,12 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>nacos-plugin-default-impl</artifactId> <artifactId>nacos-plugin-default-impl</artifactId>
<packaging>jar</packaging> <packaging>pom</packaging>
<name>nacos-plugin-default-impl ${project.version}</name> <name>nacos-plugin-default-impl ${project.version}</name>
<url>https://nacos.io</url> <url>https://nacos.io</url>
<modules>
<dependencies> <module>nacos-default-control-plugin</module>
<dependency> <module>nacos-default-auth-plugin</module>
<groupId>com.alibaba.nacos</groupId> <module>nacos-default-plugin-all</module>
<artifactId>nacos-auth-plugin</artifactId> </modules>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-common</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-sys</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-config</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.ldap</groupId>
<artifactId>spring-ldap-core</artifactId>
</dependency>
</dependencies>
</project> </project>

View File

@ -36,13 +36,7 @@
<artifactId>nacos-common</artifactId> <artifactId>nacos-common</artifactId>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-sys</artifactId>
<scope>provided</scope>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -18,18 +18,18 @@ package com.alibaba.nacos.plugin.control;
import com.alibaba.nacos.common.notify.NotifyCenter; import com.alibaba.nacos.common.notify.NotifyCenter;
import com.alibaba.nacos.common.spi.NacosServiceLoader; import com.alibaba.nacos.common.spi.NacosServiceLoader;
import com.alibaba.nacos.common.utils.StringUtils;
import com.alibaba.nacos.plugin.control.configs.ControlConfigs; import com.alibaba.nacos.plugin.control.configs.ControlConfigs;
import com.alibaba.nacos.plugin.control.connection.ConnectionControlManager; import com.alibaba.nacos.plugin.control.connection.ConnectionControlManager;
import com.alibaba.nacos.plugin.control.connection.nacos.NacosConnectionControlManager; import com.alibaba.nacos.plugin.control.connection.DefaultConnectionControlManager;
import com.alibaba.nacos.plugin.control.event.ConnectionLimitRuleChangeEvent; import com.alibaba.nacos.plugin.control.event.ConnectionLimitRuleChangeEvent;
import com.alibaba.nacos.plugin.control.event.TpsControlRuleChangeEvent; import com.alibaba.nacos.plugin.control.event.TpsControlRuleChangeEvent;
import com.alibaba.nacos.plugin.control.ruleactivator.RuleParser; import com.alibaba.nacos.plugin.control.rule.storage.RuleStorageProxy;
import com.alibaba.nacos.plugin.control.ruleactivator.RuleParserProxy; import com.alibaba.nacos.plugin.control.spi.ControlManagerBuilder;
import com.alibaba.nacos.plugin.control.ruleactivator.RuleStorageProxy;
import com.alibaba.nacos.plugin.control.tps.TpsControlManager; import com.alibaba.nacos.plugin.control.tps.TpsControlManager;
import com.alibaba.nacos.plugin.control.tps.nacos.NacosTpsControlManager; import com.alibaba.nacos.plugin.control.tps.DefaultTpsControlManager;
import java.util.Collection; import java.util.Optional;
/** /**
* control manager center. * control manager center.
@ -40,67 +40,70 @@ public class ControlManagerCenter {
static volatile ControlManagerCenter instance = null; static volatile ControlManagerCenter instance = null;
private final RuleStorageProxy ruleStorageProxy;
private TpsControlManager tpsControlManager; private TpsControlManager tpsControlManager;
private ConnectionControlManager connectionControlManager; private ConnectionControlManager connectionControlManager;
private RuleStorageProxy ruleStorageProxy;
private void initConnectionManager() {
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 connection control manager of name={},class={}", connectionManagerName,
connectionControlManagerInternal.getClass().getSimpleName());
connectionControlManager = connectionControlManagerInternal;
break;
}
}
if (connectionControlManager == null) {
Loggers.CONTROL.warn("Fail to connection control manager of name " + connectionManagerName);
connectionControlManager = new NacosConnectionControlManager();
}
}
private void initTpsControlManager() {
Collection<TpsControlManager> tpsControlManagers = NacosServiceLoader.load(TpsControlManager.class);
String tpsManagerName = ControlConfigs.getInstance().getTpsManager();
for (TpsControlManager tpsControlManagerInternal : tpsControlManagers) {
if (tpsControlManagerInternal.getName().equalsIgnoreCase(tpsManagerName)) {
Loggers.CONTROL.info("Found tps control manager of name={},class={}", tpsManagerName,
tpsControlManagerInternal.getClass().getSimpleName());
tpsControlManager = tpsControlManagerInternal;
break;
}
}
if (tpsControlManager == null) {
Loggers.CONTROL.warn("Fail to found tps control manager of name " + tpsManagerName);
tpsControlManager = new NacosTpsControlManager();
}
}
private ControlManagerCenter() { private ControlManagerCenter() {
initTpsControlManager(); ruleStorageProxy = RuleStorageProxy.getInstance();
initConnectionManager(); Optional<ControlManagerBuilder> controlManagerBuilder = findTargetControlManagerBuilder();
ruleStorageProxy = new RuleStorageProxy(); if (controlManagerBuilder.isPresent()) {
initConnectionManager(controlManagerBuilder.get());
initTpsControlManager(controlManagerBuilder.get());
} else {
buildNoLimitControlManagers();
}
}
private void initConnectionManager(ControlManagerBuilder controlManagerBuilder) {
try {
connectionControlManager = controlManagerBuilder.buildConnectionControlManager();
Loggers.CONTROL.info("Build connection control manager, class={}",
connectionControlManager.getClass().getCanonicalName());
} catch (Exception e) {
Loggers.CONTROL.warn("Build connection control manager failed, use no limit manager replaced.", e);
connectionControlManager = new DefaultConnectionControlManager();
}
}
private void initTpsControlManager(ControlManagerBuilder controlManagerBuilder) {
try {
tpsControlManager = controlManagerBuilder.buildTpsControlManager();
Loggers.CONTROL
.info("Build tps control manager, class={}", tpsControlManager.getClass().getCanonicalName());
} catch (Exception e) {
Loggers.CONTROL.warn("Build tps control manager failed, use no limit manager replaced.", e);
tpsControlManager = new DefaultTpsControlManager();
}
}
private Optional<ControlManagerBuilder> findTargetControlManagerBuilder() {
String controlManagerType = ControlConfigs.getInstance().getControlManagerType();
if (StringUtils.isEmpty(controlManagerType)) {
Loggers.CONTROL.info("Not configure type of control plugin, no limit control for current node.");
return Optional.empty();
}
for (ControlManagerBuilder each : NacosServiceLoader.load(ControlManagerBuilder.class)) {
Loggers.CONTROL.info("Found control manager plugin of name={}", each.getName());
if (controlManagerType.equalsIgnoreCase(each.getName())) {
return Optional.of(each);
}
}
Loggers.CONTROL.warn("Not found control manager plugin of name");
return Optional.empty();
}
private void buildNoLimitControlManagers() {
connectionControlManager = new DefaultConnectionControlManager();
tpsControlManager = new DefaultTpsControlManager();
} }
public RuleStorageProxy getRuleStorageProxy() { public RuleStorageProxy getRuleStorageProxy() {
return ruleStorageProxy; return ruleStorageProxy;
} }
public RuleParser getRuleParser() {
return RuleParserProxy.getInstance();
}
public TpsControlManager getTpsControlManager() { public TpsControlManager getTpsControlManager() {
return tpsControlManager; return tpsControlManager;
} }

View File

@ -50,37 +50,13 @@ public class ControlConfigs {
ControlConfigs.instance = instance; ControlConfigs.instance = instance;
} }
private String tpsBarrierCreator = "nacos";
private String tpsRuleBarrierCreator = "nacos";
private String connectionRuntimeEjector = "nacos"; private String connectionRuntimeEjector = "nacos";
private String connectionManager = "nacos";
private String tpsManager = "nacos";
private String ruleExternalStorage = ""; private String ruleExternalStorage = "";
private String ruleParser = "nacos";
private String localRuleStorageBaseDir = ""; private String localRuleStorageBaseDir = "";
public String getTpsBarrierCreator() { private String controlManagerType = "";
return tpsBarrierCreator;
}
public void setTpsBarrierCreator(String tpsBarrierCreator) {
this.tpsBarrierCreator = tpsBarrierCreator;
}
public String getTpsRuleBarrierCreator() {
return tpsRuleBarrierCreator;
}
public void setTpsRuleBarrierCreator(String tpsRuleBarrierCreator) {
this.tpsRuleBarrierCreator = tpsRuleBarrierCreator;
}
public String getRuleExternalStorage() { public String getRuleExternalStorage() {
return ruleExternalStorage; return ruleExternalStorage;
@ -90,22 +66,6 @@ public class ControlConfigs {
this.ruleExternalStorage = ruleExternalStorage; this.ruleExternalStorage = ruleExternalStorage;
} }
public String getRuleParser() {
return ruleParser;
}
public void setRuleParser(String ruleParser) {
this.ruleParser = ruleParser;
}
public String getConnectionManager() {
return connectionManager;
}
public void setConnectionManager(String connectionManager) {
this.connectionManager = connectionManager;
}
public String getConnectionRuntimeEjector() { public String getConnectionRuntimeEjector() {
return connectionRuntimeEjector; return connectionRuntimeEjector;
} }
@ -114,14 +74,6 @@ public class ControlConfigs {
this.connectionRuntimeEjector = connectionRuntimeEjector; this.connectionRuntimeEjector = connectionRuntimeEjector;
} }
public String getTpsManager() {
return tpsManager;
}
public void setTpsManager(String tpsManager) {
this.tpsManager = tpsManager;
}
public String getLocalRuleStorageBaseDir() { public String getLocalRuleStorageBaseDir() {
return localRuleStorageBaseDir; return localRuleStorageBaseDir;
} }
@ -129,4 +81,12 @@ public class ControlConfigs {
public void setLocalRuleStorageBaseDir(String localRuleStorageBaseDir) { public void setLocalRuleStorageBaseDir(String localRuleStorageBaseDir) {
this.localRuleStorageBaseDir = localRuleStorageBaseDir; this.localRuleStorageBaseDir = localRuleStorageBaseDir;
} }
public String getControlManagerType() {
return controlManagerType;
}
public void setControlManagerType(String controlManagerType) {
this.controlManagerType = controlManagerType;
}
} }

View File

@ -23,8 +23,9 @@ import com.alibaba.nacos.plugin.control.Loggers;
import com.alibaba.nacos.plugin.control.connection.request.ConnectionCheckRequest; 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.response.ConnectionCheckResponse;
import com.alibaba.nacos.plugin.control.connection.rule.ConnectionControlRule; import com.alibaba.nacos.plugin.control.connection.rule.ConnectionControlRule;
import com.alibaba.nacos.plugin.control.ruleactivator.RuleParserProxy; import com.alibaba.nacos.plugin.control.rule.parser.ConnectionControlRuleParser;
import com.alibaba.nacos.plugin.control.ruleactivator.RuleStorageProxy; import com.alibaba.nacos.plugin.control.rule.parser.NacosConnectionControlRuleParser;
import com.alibaba.nacos.plugin.control.rule.storage.RuleStorageProxy;
import java.util.Collection; import java.util.Collection;
import java.util.Map; import java.util.Map;
@ -40,10 +41,26 @@ import java.util.stream.Collectors;
@SuppressWarnings("PMD.AbstractClassShouldStartWithAbstractNamingRule") @SuppressWarnings("PMD.AbstractClassShouldStartWithAbstractNamingRule")
public abstract class ConnectionControlManager { public abstract class ConnectionControlManager {
private final ConnectionControlRuleParser connectionControlRuleParser;
protected ConnectionControlRule connectionControlRule; protected ConnectionControlRule connectionControlRule;
protected Collection<ConnectionMetricsCollector> metricsCollectorList; protected Collection<ConnectionMetricsCollector> metricsCollectorList;
private ScheduledExecutorService executorService;
public ConnectionControlManager() {
metricsCollectorList = NacosServiceLoader.load(ConnectionMetricsCollector.class);
Loggers.CONTROL.info("Load connection metrics collector,size={},{}", metricsCollectorList.size(),
metricsCollectorList);
this.connectionControlRuleParser = buildConnectionControlRuleParser();
initConnectionRule();
if (!metricsCollectorList.isEmpty()) {
initExecuteService();
startConnectionMetricsReport();
}
}
/** /**
* get manager name. * get manager name.
* *
@ -51,18 +68,12 @@ public abstract class ConnectionControlManager {
*/ */
public abstract String getName(); public abstract String getName();
private ScheduledExecutorService executorService; protected ConnectionControlRuleParser buildConnectionControlRuleParser() {
return new NacosConnectionControlRuleParser();
}
public ConnectionControlManager() { public ConnectionControlRuleParser getConnectionControlRuleParser() {
metricsCollectorList = NacosServiceLoader.load(ConnectionMetricsCollector.class); return connectionControlRuleParser;
Loggers.CONTROL.info("Load connection metrics collector,size={},{}", metricsCollectorList.size(),
metricsCollectorList);
initConnectionRule();
if (!metricsCollectorList.isEmpty()) {
initExecuteService();
startConnectionMetricsReport();
}
} }
private void initExecuteService() { private void initExecuteService() {
@ -73,10 +84,6 @@ public abstract class ConnectionControlManager {
}); });
} }
private void startConnectionMetricsReport() {
executorService.scheduleWithFixedDelay(new ConnectionMetricsReporter(), 0, 3000, TimeUnit.MILLISECONDS);
}
private void initConnectionRule() { private void initConnectionRule() {
RuleStorageProxy ruleStorageProxy = RuleStorageProxy.getInstance(); RuleStorageProxy ruleStorageProxy = RuleStorageProxy.getInstance();
String localRuleContent = ruleStorageProxy.getLocalDiskStorage().getConnectionRule(); String localRuleContent = ruleStorageProxy.getLocalDiskStorage().getConnectionRule();
@ -92,7 +99,7 @@ public abstract class ConnectionControlManager {
} }
if (StringUtils.isNotBlank(localRuleContent)) { if (StringUtils.isNotBlank(localRuleContent)) {
connectionControlRule = RuleParserProxy.getInstance().parseConnectionRule(localRuleContent); connectionControlRule = connectionControlRuleParser.parseRule(localRuleContent);
Loggers.CONTROL.info("init connection rule end"); Loggers.CONTROL.info("init connection rule end");
} else { } else {
@ -101,6 +108,10 @@ public abstract class ConnectionControlManager {
} }
} }
private void startConnectionMetricsReport() {
executorService.scheduleWithFixedDelay(new ConnectionMetricsReporter(), 0, 3000, TimeUnit.MILLISECONDS);
}
public ConnectionControlRule getConnectionLimitRule() { public ConnectionControlRule getConnectionLimitRule() {
return connectionControlRule; return connectionControlRule;
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 1999-2020 Alibaba Group Holding Ltd. * Copyright 1999-2023 Alibaba Group Holding Ltd.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -14,29 +14,28 @@
* limitations under the License. * limitations under the License.
*/ */
package com.alibaba.nacos.plugin.control.connection.nacos; package com.alibaba.nacos.plugin.control.connection;
import com.alibaba.nacos.common.utils.JacksonUtils; import com.alibaba.nacos.common.utils.JacksonUtils;
import com.alibaba.nacos.plugin.control.Loggers; import com.alibaba.nacos.plugin.control.Loggers;
import com.alibaba.nacos.plugin.control.connection.ConnectionControlManager;
import com.alibaba.nacos.plugin.control.connection.request.ConnectionCheckRequest; 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.ConnectionCheckCode;
import com.alibaba.nacos.plugin.control.connection.response.ConnectionCheckResponse; import com.alibaba.nacos.plugin.control.connection.response.ConnectionCheckResponse;
import com.alibaba.nacos.plugin.control.connection.rule.ConnectionControlRule; import com.alibaba.nacos.plugin.control.connection.rule.ConnectionControlRule;
/** /**
* connection control manager. * default connection control manager, no limit control.
* *
* @author shiyiyue * @author shiyiyue
*/ */
public class NacosConnectionControlManager extends ConnectionControlManager { public class DefaultConnectionControlManager extends ConnectionControlManager {
@Override @Override
public String getName() { public String getName() {
return "nacos"; return "noLimit";
} }
public NacosConnectionControlManager() { public DefaultConnectionControlManager() {
super(); super();
} }
@ -45,7 +44,7 @@ public class NacosConnectionControlManager extends ConnectionControlManager {
super.connectionControlRule = connectionControlRule; super.connectionControlRule = connectionControlRule;
Loggers.CONTROL.info("Connection control rule updated to ->" + (this.connectionControlRule == null ? null Loggers.CONTROL.info("Connection control rule updated to ->" + (this.connectionControlRule == null ? null
: JacksonUtils.toJson(this.connectionControlRule))); : JacksonUtils.toJson(this.connectionControlRule)));
Loggers.CONTROL.warn("Connection control updated, But connection control manager is no limit implementation.");
} }
@Override @Override

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 1999-2020 Alibaba Group Holding Ltd. * Copyright 1999-2023 Alibaba Group Holding Ltd.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
package com.alibaba.nacos.plugin.control.ruleactivator; package com.alibaba.nacos.plugin.control.rule;
import com.alibaba.nacos.common.notify.Event; import com.alibaba.nacos.common.notify.Event;
import com.alibaba.nacos.common.notify.NotifyCenter; import com.alibaba.nacos.common.notify.NotifyCenter;
@ -22,9 +22,12 @@ import com.alibaba.nacos.common.notify.listener.Subscriber;
import com.alibaba.nacos.common.utils.StringUtils; import com.alibaba.nacos.common.utils.StringUtils;
import com.alibaba.nacos.plugin.control.ControlManagerCenter; import com.alibaba.nacos.plugin.control.ControlManagerCenter;
import com.alibaba.nacos.plugin.control.Loggers; import com.alibaba.nacos.plugin.control.Loggers;
import com.alibaba.nacos.plugin.control.connection.ConnectionControlManager;
import com.alibaba.nacos.plugin.control.connection.rule.ConnectionControlRule; import com.alibaba.nacos.plugin.control.connection.rule.ConnectionControlRule;
import com.alibaba.nacos.plugin.control.event.ConnectionLimitRuleChangeEvent; import com.alibaba.nacos.plugin.control.event.ConnectionLimitRuleChangeEvent;
import com.alibaba.nacos.plugin.control.event.TpsControlRuleChangeEvent; import com.alibaba.nacos.plugin.control.event.TpsControlRuleChangeEvent;
import com.alibaba.nacos.plugin.control.rule.storage.RuleStorageProxy;
import com.alibaba.nacos.plugin.control.tps.TpsControlManager;
import com.alibaba.nacos.plugin.control.tps.rule.TpsControlRule; import com.alibaba.nacos.plugin.control.tps.rule.TpsControlRule;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -46,14 +49,14 @@ public class ControlRuleChangeActivator {
NotifyCenter.registerSubscriber(connectionRuleChangeSubscriber); NotifyCenter.registerSubscriber(connectionRuleChangeSubscriber);
} }
class TpsRuleChangeSubscriber extends Subscriber<TpsControlRuleChangeEvent> { static class TpsRuleChangeSubscriber extends Subscriber<TpsControlRuleChangeEvent> {
@Override @Override
public void onEvent(TpsControlRuleChangeEvent event) { public void onEvent(TpsControlRuleChangeEvent event) {
String pointName = event.getPointName(); String pointName = event.getPointName();
LOGGER.info("Tps control rule change event receive,pointName={}, external={} ", pointName, LOGGER.info("Tps control rule change event receive,pointName={}, external={} ", pointName,
event.isExternal()); event.isExternal());
if (event == null || event.getPointName() == null) { if (event.getPointName() == null) {
return; return;
} }
try { try {
@ -71,11 +74,11 @@ public class ControlRuleChangeActivator {
} }
String tpsRuleContent = ruleStorageProxy.getLocalDiskStorage().getTpsRule(pointName); String tpsRuleContent = ruleStorageProxy.getLocalDiskStorage().getTpsRule(pointName);
TpsControlManager tpsControlManager = ControlManagerCenter.getInstance().getTpsControlManager();
TpsControlRule tpsControlRule = StringUtils.isBlank(tpsRuleContent) ? new TpsControlRule() TpsControlRule tpsControlRule = StringUtils.isBlank(tpsRuleContent) ? new TpsControlRule()
: ControlManagerCenter.getInstance().getRuleParser().parseTpsRule(tpsRuleContent); : tpsControlManager.getTpsControlRuleParser().parseRule(tpsRuleContent);
ControlManagerCenter.getInstance().getTpsControlManager().applyTpsRule(pointName, tpsControlRule); tpsControlManager.applyTpsRule(pointName, tpsControlRule);
} catch (Exception e) { } catch (Exception e) {
LOGGER.warn("Tps control rule apply error ,error= ", e); LOGGER.warn("Tps control rule apply error ,error= ", e);
@ -89,7 +92,7 @@ public class ControlRuleChangeActivator {
} }
} }
class ConnectionRuleChangeSubscriber extends Subscriber<ConnectionLimitRuleChangeEvent> { static class ConnectionRuleChangeSubscriber extends Subscriber<ConnectionLimitRuleChangeEvent> {
@Override @Override
public void onEvent(ConnectionLimitRuleChangeEvent event) { public void onEvent(ConnectionLimitRuleChangeEvent event) {
@ -112,15 +115,15 @@ public class ControlRuleChangeActivator {
String limitRule = ruleStorageProxy.getLocalDiskStorage().getConnectionRule(); String limitRule = ruleStorageProxy.getLocalDiskStorage().getConnectionRule();
Loggers.CONTROL.info("start to apply connection rule content " + limitRule); Loggers.CONTROL.info("start to apply connection rule content " + limitRule);
ConnectionControlManager controlManager = ControlManagerCenter.getInstance()
.getConnectionControlManager();
ConnectionControlRule connectionControlRule = ConnectionControlRule connectionControlRule =
StringUtils.isBlank(limitRule) ? new ConnectionControlRule() StringUtils.isBlank(limitRule) ? new ConnectionControlRule()
: ControlManagerCenter.getInstance().getRuleParser().parseConnectionRule(limitRule); : controlManager.getConnectionControlRuleParser().parseRule(limitRule);
Loggers.CONTROL.info("end to apply connection rule content "); Loggers.CONTROL.info("end to apply connection rule content ");
if (connectionControlRule != null) { if (connectionControlRule != null) {
ControlManagerCenter.getInstance().getConnectionControlManager() controlManager.applyConnectionLimitRule(connectionControlRule);
.applyConnectionLimitRule(connectionControlRule);
} else { } else {
LOGGER.info("Parse rule is null,Ignore illegal rule :{}", limitRule); LOGGER.info("Parse rule is null,Ignore illegal rule :{}", limitRule);
} }

View File

@ -0,0 +1,28 @@
/*
* 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.rule.parser;
import com.alibaba.nacos.plugin.control.connection.rule.ConnectionControlRule;
/**
* Connection control rule parser.
*
* @author xiweng.yy
*/
public interface ConnectionControlRuleParser extends RuleParser<ConnectionControlRule> {
}

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 1999-2020 Alibaba Group Holding Ltd. * Copyright 1999-2023 Alibaba Group Holding Ltd.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -14,36 +14,23 @@
* limitations under the License. * limitations under the License.
*/ */
package com.alibaba.nacos.plugin.control.ruleactivator; package com.alibaba.nacos.plugin.control.rule.parser;
import com.alibaba.nacos.common.utils.JacksonUtils; import com.alibaba.nacos.common.utils.JacksonUtils;
import com.alibaba.nacos.common.utils.StringUtils; import com.alibaba.nacos.common.utils.StringUtils;
import com.alibaba.nacos.plugin.control.connection.rule.ConnectionControlRule; import com.alibaba.nacos.plugin.control.connection.rule.ConnectionControlRule;
import com.alibaba.nacos.plugin.control.tps.rule.TpsControlRule;
/** /**
* nacos rule parser. * Nacos default connection control rule parser with json.
* *
* @author shiyiyue * @author xiweng.yy
*/ */
public class NacosRuleParser implements RuleParser { public class NacosConnectionControlRuleParser implements ConnectionControlRuleParser {
@Override @Override
public TpsControlRule parseTpsRule(String ruleContent) { public ConnectionControlRule parseRule(String ruleContent) {
return StringUtils.isBlank(ruleContent) ? new TpsControlRule()
: JacksonUtils.toObj(ruleContent, TpsControlRule.class);
}
@Override
public ConnectionControlRule parseConnectionRule(String ruleContent) {
return StringUtils.isBlank(ruleContent) ? new ConnectionControlRule() return StringUtils.isBlank(ruleContent) ? new ConnectionControlRule()
: JacksonUtils.toObj(ruleContent, ConnectionControlRule.class); : JacksonUtils.toObj(ruleContent, ConnectionControlRule.class);
} }
@Override
public String getName() {
return "nacos";
}
} }

View File

@ -0,0 +1,36 @@
/*
* 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.rule.parser;
import com.alibaba.nacos.common.utils.JacksonUtils;
import com.alibaba.nacos.common.utils.StringUtils;
import com.alibaba.nacos.plugin.control.tps.rule.TpsControlRule;
/**
* Nacos default tps control rule parser with json.
*
* @author xiweng.yy
*/
public class NacosTpsControlRuleParser implements TpsControlRuleParser {
@Override
public TpsControlRule parseRule(String ruleContent) {
return StringUtils.isBlank(ruleContent) ? new TpsControlRule()
: JacksonUtils.toObj(ruleContent, TpsControlRule.class);
}
}

View File

@ -0,0 +1,33 @@
/*
* 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.rule.parser;
/**
* parse rule content from raw string.
*
* @author shiyiyue
*/
public interface RuleParser<R> {
/**
* Parse control rule.
*
* @param ruleContent rule raw string
* @return target rule
*/
R parseRule(String ruleContent);
}

View File

@ -0,0 +1,28 @@
/*
* 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.rule.parser;
import com.alibaba.nacos.plugin.control.tps.rule.TpsControlRule;
/**
* Tps control rule parser.
*
* @author xiweng.yy
*/
public interface TpsControlRuleParser extends RuleParser<TpsControlRule> {
}

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 1999-2020 Alibaba Group Holding Ltd. * Copyright 1999-2023 Alibaba Group Holding Ltd.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
package com.alibaba.nacos.plugin.control.ruleactivator; package com.alibaba.nacos.plugin.control.rule.storage;
/** /**
* external rule storage. * external rule storage.

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 1999-2020 Alibaba Group Holding Ltd. * Copyright 1999-2023 Alibaba Group Holding Ltd.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -14,11 +14,12 @@
* limitations under the License. * limitations under the License.
*/ */
package com.alibaba.nacos.plugin.control.ruleactivator; package com.alibaba.nacos.plugin.control.rule.storage;
import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.plugin.control.Loggers; import com.alibaba.nacos.plugin.control.Loggers;
import com.alibaba.nacos.sys.env.EnvUtil; import com.alibaba.nacos.plugin.control.utils.DiskUtils;
import com.alibaba.nacos.plugin.control.utils.EnvUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import java.io.File; import java.io.File;
@ -37,30 +38,26 @@ public class LocalDiskRuleStorage implements RuleStorage {
private static final Logger LOGGER = Loggers.CONTROL; private static final Logger LOGGER = Loggers.CONTROL;
private String localRruleBaseDir = defaultBaseDir(); private String localRuleBaseDir = defaultBaseDir();
private File checkTpsBaseDir() { private File checkTpsBaseDir() {
File baseDir = new File(localRruleBaseDir, "data" + File.separator + "tps" + File.separator); File baseDir = new File(localRuleBaseDir, "data" + File.separator + "tps" + File.separator);
if (!baseDir.exists()) { if (!baseDir.exists()) {
baseDir.mkdirs(); baseDir.mkdirs();
} }
return baseDir; return baseDir;
} }
public String getLocalRruleBaseDir() { public void setLocalRuleBaseDir(String localRruleBaseDir) {
return localRruleBaseDir; this.localRuleBaseDir = localRruleBaseDir;
}
public void setLocalRruleBaseDir(String localRruleBaseDir) {
this.localRruleBaseDir = localRruleBaseDir;
} }
private static String defaultBaseDir() { private static String defaultBaseDir() {
return EnvUtil.getNacosHome(); return EnvUtils.getNacosHome();
} }
private File getConnectionRuleFile() { private File getConnectionRuleFile() {
File baseDir = new File(localRruleBaseDir, "data" + File.separator + "connection" + File.separator); File baseDir = new File(localRuleBaseDir, "data" + File.separator + "connection" + File.separator);
if (!baseDir.exists()) { if (!baseDir.exists()) {
baseDir.mkdir(); baseDir.mkdir();
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 1999-2020 Alibaba Group Holding Ltd. * Copyright 1999-2023 Alibaba Group Holding Ltd.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
package com.alibaba.nacos.plugin.control.ruleactivator; package com.alibaba.nacos.plugin.control.rule.storage;
/** /**
* rule storage. * rule storage.

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 1999-2020 Alibaba Group Holding Ltd. * Copyright 1999-2023 Alibaba Group Holding Ltd.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -14,12 +14,14 @@
* limitations under the License. * limitations under the License.
*/ */
package com.alibaba.nacos.plugin.control.ruleactivator; package com.alibaba.nacos.plugin.control.rule.storage;
import com.alibaba.nacos.common.spi.NacosServiceLoader; import com.alibaba.nacos.common.spi.NacosServiceLoader;
import com.alibaba.nacos.common.utils.StringUtils; import com.alibaba.nacos.common.utils.StringUtils;
import com.alibaba.nacos.plugin.control.Loggers; import com.alibaba.nacos.plugin.control.Loggers;
import com.alibaba.nacos.plugin.control.configs.ControlConfigs; import com.alibaba.nacos.plugin.control.configs.ControlConfigs;
import com.alibaba.nacos.plugin.control.rule.ControlRuleChangeActivator;
import com.alibaba.nacos.plugin.control.spi.ExternalRuleStorageBuilder;
import org.slf4j.Logger; import org.slf4j.Logger;
import java.util.Collection; import java.util.Collection;
@ -41,30 +43,40 @@ public class RuleStorageProxy {
ControlRuleChangeActivator controlRuleChangeActivator = null; ControlRuleChangeActivator controlRuleChangeActivator = null;
public RuleStorageProxy() { private RuleStorageProxy() {
String externalStorageType = ControlConfigs.getInstance().getRuleExternalStorage();
Collection<ExternalRuleStorage> persistRuleActivators = NacosServiceLoader.load(ExternalRuleStorage.class); if (StringUtils.isNotEmpty(externalStorageType)) {
String rulePersistActivator = ControlConfigs.getInstance().getRuleExternalStorage(); buildExternalStorage(externalStorageType);
}
for (ExternalRuleStorage persistRuleActivator : persistRuleActivators) { initLocalStorage();
if (persistRuleActivator.getName().equalsIgnoreCase(rulePersistActivator)) { controlRuleChangeActivator = new ControlRuleChangeActivator();
LOGGER.info("Found persist rule storage of name " + rulePersistActivator); }
externalRuleStorage = persistRuleActivator;
private void buildExternalStorage(String externalStorageType) {
Collection<ExternalRuleStorageBuilder> externalRuleStorageBuilders = NacosServiceLoader
.load(ExternalRuleStorageBuilder.class);
for (ExternalRuleStorageBuilder each : externalRuleStorageBuilders) {
LOGGER.info("Found persist rule storage of name : {}", externalStorageType);
if (externalStorageType.equalsIgnoreCase(each.getName())) {
try {
externalRuleStorage = each.buildExternalRuleStorage();
} catch (Exception e) {
LOGGER.warn("Build external rule storage failed, the rules will not be persisted", e);
}
LOGGER.info("Build external rule storage of name {} finished", externalStorageType);
break; break;
} }
} }
if (externalRuleStorage == null && StringUtils.isNotBlank(rulePersistActivator)) { if (externalRuleStorage == null && StringUtils.isNotBlank(externalStorageType)) {
LOGGER.error("Fail to found persist rule storage of name " + rulePersistActivator); LOGGER.error("Fail to found persist rule storage of name : {}", externalStorageType);
} }
}
//local disk storage.
private void initLocalStorage() {
localDiskRuleStorage = new LocalDiskRuleStorage(); localDiskRuleStorage = new LocalDiskRuleStorage();
if (StringUtils.isNotBlank(ControlConfigs.getInstance().getLocalRuleStorageBaseDir())) { if (StringUtils.isNotBlank(ControlConfigs.getInstance().getLocalRuleStorageBaseDir())) {
localDiskRuleStorage.setLocalRruleBaseDir(ControlConfigs.getInstance().getLocalRuleStorageBaseDir()); localDiskRuleStorage.setLocalRuleBaseDir(ControlConfigs.getInstance().getLocalRuleStorageBaseDir());
} }
controlRuleChangeActivator = new ControlRuleChangeActivator();
} }
public RuleStorage getLocalDiskStorage() { public RuleStorage getLocalDiskStorage() {
@ -75,7 +87,7 @@ public class RuleStorageProxy {
return externalRuleStorage; return externalRuleStorage;
} }
public static final RuleStorageProxy getInstance() { public static RuleStorageProxy getInstance() {
return INSTANCE; return INSTANCE;
} }
} }

View File

@ -1,51 +0,0 @@
/*
* 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.ruleactivator;
import com.alibaba.nacos.plugin.control.connection.rule.ConnectionControlRule;
import com.alibaba.nacos.plugin.control.tps.rule.TpsControlRule;
/**
* parse rule content from raw string.
*
* @author shiyiyue
*/
public interface RuleParser {
/**
* parse tps rule content.
*
* @param ruleContent ruleContent.
* @return
*/
TpsControlRule parseTpsRule(String ruleContent);
/**
* parse connection rule.
*
* @param ruleContent ruleContent.
* @return
*/
ConnectionControlRule parseConnectionRule(String ruleContent);
/**
* get name.
*
* @return
*/
String getName();
}

View File

@ -1,58 +0,0 @@
/*
* 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.ruleactivator;
import com.alibaba.nacos.common.spi.NacosServiceLoader;
import com.alibaba.nacos.plugin.control.Loggers;
import com.alibaba.nacos.plugin.control.configs.ControlConfigs;
import org.slf4j.Logger;
import java.util.Collection;
/**
* rule parser proxy.
*
* @author shiyiyue
*/
public class RuleParserProxy {
private static final Logger LOGGER = Loggers.CONTROL;
private static RuleParser instance;
static {
Collection<RuleParser> ruleParsers = NacosServiceLoader.load(RuleParser.class);
String ruleParserName = ControlConfigs.getInstance().getRuleParser();
for (RuleParser ruleParser : ruleParsers) {
if (ruleParser.getName().equalsIgnoreCase(ruleParserName)) {
LOGGER.info("Found rule parser of name={},class={}", ruleParserName,
ruleParser.getClass().getSimpleName());
instance = ruleParser;
break;
}
}
if (instance == null) {
LOGGER.warn("Fail to rule parser of name " + ruleParserName);
instance = new NacosRuleParser();
}
}
public static RuleParser getInstance() {
return instance;
}
}

View File

@ -0,0 +1,49 @@
/*
* 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.spi;
import com.alibaba.nacos.plugin.control.connection.ConnectionControlManager;
import com.alibaba.nacos.plugin.control.tps.TpsControlManager;
/**
* Nacos control plugin manager builder SPI.
*
* @author xiweng.yy
*/
public interface ControlManagerBuilder {
/**
* Get plugin name.
*
* @return name of plugin
*/
String getName();
/**
* Build {@link ConnectionControlManager} implementation for current plugin.
*
* @return ConnectionControlManager implementation
*/
ConnectionControlManager buildConnectionControlManager();
/**
* Build {@link TpsControlManager} implementation for current plugin.
*
* @return TpsControlManager implementation
*/
TpsControlManager buildTpsControlManager();
}

View File

@ -0,0 +1,41 @@
/*
* 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.spi;
import com.alibaba.nacos.plugin.control.rule.storage.ExternalRuleStorage;
/**
* Nacos control plugin external rule storage builder SPI.
*
* @author xiweng.yy
*/
public interface ExternalRuleStorageBuilder {
/**
* Get plugin name.
*
* @return name of plugin
*/
String getName();
/**
* Build {@link ExternalRuleStorage} implementation for current plugin if necessary.
*
* @return ExternalRuleStorage implementation
*/
ExternalRuleStorage buildExternalRuleStorage();
}

Some files were not shown because too many files have changed in this diff Show More