Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add plugin handler and configuration classes #1531

Merged
merged 1 commit into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,16 @@ public class CommonConst {
+ "> throwable: %s " + System.lineSeparator()
+ "--- " + System.lineSeparator();

/**
* Service name for global configuration
*/
public static final String GLOBAL_CONFIGURATION_SERVICE_NAME = "ALL_SERVICE";

/**
* Default regular expression for configuration item Group
*/
public static final String CONFIGURATION_DEFAULT_PATTERN = "^(app=[^&]*+&environment=[^&]*+(&service=[^&]*)?)?$";

private CommonConst() {

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* Copyright (C) 2024-2024 Sermant Authors. All rights reserved.
*
* 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 io.sermant.backend.common.conf;

import lombok.Getter;
import lombok.Setter;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

/**
* Configuration class for dynamic configuration function
*
* @author zhp
* @since 2024-05-21
*/
@Configuration
@Getter
@Setter
@Component
public class DynamicConfig {
/**
* Connection address of configuration center
*/
@Value("${dynamic.config.serverAddress}")
private String serverAddress;

/**
* Type of Configuration Center
*/
@Value("${dynamic.config.dynamicConfigType}")
private String dynamicConfigType;

/**
* Request timeout
*/
@Value("${dynamic.config.requestTimeout}")
private long requestTimeout;

/**
* Connection timeout
*/
@Value("${dynamic.config.connectTimeout}")
private long connectTimeout;

/**
* Session timeout
*/
@Value("${dynamic.config.timeout}")
private int timeout;

/**
* Authorized switch
*/
@Value("${dynamic.config.enableAuth}")
private boolean enableAuth;

/**
* userName, used for configuration center connection authorization
*/
@Value("${dynamic.config.userName}")
private String userName;

/**
* password, used for configuration center connection authorization
*/
@Value("${dynamic.config.password}")
private String password;

/**
* Encryption and decryption keys for passwords
*/
@Value("${dynamic.config.secretKey}")
private String secretKey;

/**
* Namespaces, only used by the nacos configuration center
*/
@Value("${dynamic.config.namespace}")
private String namespace = "default";

/**
* Switch for configuration management
*/
@Value("${dynamic.config.dynamicConfigEnable}")
private boolean dynamicConfigEnable;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* Copyright (C) 2024-2024 Sermant Authors. All rights reserved.
*
* 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 io.sermant.backend.entity.config;

import io.sermant.backend.handler.config.DatabaseWriteProhibitionPluginHandler;
import io.sermant.backend.handler.config.FlowControlPluginHandler;
import io.sermant.backend.handler.config.LoadbalancerPluginHandler;
import io.sermant.backend.handler.config.MqConsumeProhibitionPluginHandler;
import io.sermant.backend.handler.config.OtherPluginHandler;
import io.sermant.backend.handler.config.PluginConfigHandler;
import io.sermant.backend.handler.config.RemovalPluginHandler;
import io.sermant.backend.handler.config.RouterPluginHandler;
import io.sermant.backend.handler.config.ServiceRegistryPluginHandler;
import io.sermant.backend.handler.config.SpringBootRegistryPluginHandler;
import io.sermant.backend.handler.config.TagTransmissionPluginHandler;
import lombok.Getter;

import java.util.Optional;

/**
* Plugin type
*
* @author zhp
* @since 2024-05-16
*/
@Getter
public enum PluginType {
/**
* Label routing plugin
*/
ROUTER("router", new RouterPluginHandler()),

/**
* Springboot registration plugin
*/
SPRINGBOOT_REGISTRY("springboot-registry", new SpringBootRegistryPluginHandler()),

/**
* Register migration plugin
*/
SERVICE_REGISTRY("service-registry", new ServiceRegistryPluginHandler()),

/**
* flowcontrol plugin
*/
FLOW_CONTROL("flowcontrol", new FlowControlPluginHandler()),

/**
* Outlier instance removal plugin
*/
REMOVAL("removal", new RemovalPluginHandler()),

/**
* Load balancing plugin
*/
LOADBALANCER("loadbalancer", new LoadbalancerPluginHandler()),

/**
* Traffic tag transparency plugin
*/
TAG_TRANSMISSION("tag-transmission", new TagTransmissionPluginHandler()),

/**
* Message queue prohibited consumption plugin
*/
MQ_CONSUME_PROHIBITION("mq-consume-prohibition", new MqConsumeProhibitionPluginHandler()),

/**
* Database write prohibited plugin
*/
DATABASE_WRITE_PROHIBITION("database-write-prohibition", new DatabaseWriteProhibitionPluginHandler()),

/**
* other plugin
*/
OTHER("other", new OtherPluginHandler());

private final String pluginName;

private final PluginConfigHandler handler;

PluginType(String pluginName, PluginConfigHandler handler) {
this.pluginName = pluginName;
this.handler = handler;
}

/**
* Obtain plugin type based on plugin name
*
* @param pluginName plugin name
* @return plugin type
*/
public static Optional<PluginType> getPluginType(String pluginName) {
for (PluginType pluginType : PluginType.values()) {
if (pluginType.pluginName.equals(pluginName)) {
return Optional.of(pluginType);
}
}
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright (C) 2024-2024 Sermant Authors. All rights reserved.
*
* 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 io.sermant.backend.handler.config;

import io.sermant.backend.common.conf.CommonConst;
import io.sermant.backend.entity.config.ConfigInfo;
import io.sermant.backend.entity.config.PluginType;

import org.apache.commons.lang.StringUtils;

import java.util.Map;

/**
* Database Write Prohibition plugin handler
*
* @author zhp
* @since 2024-05-16
*/
public class DatabaseWriteProhibitionPluginHandler extends PluginConfigHandler {
private static final String GLOBAL_CONFIGURATION_NAME = "sermant.database.write.globalConfig";

private static final String SERVICE_CONFIGURATION_NAME_PREFIX = "sermant.database.write.";

private static final String PATTERN = "^(app=[^&]*+&environment=[^&]*+(&zone=[^&]*)?)?$";

@Override
public ConfigInfo parsePluginInfo(String key, String group) {
Map<String, String> map = parseGroup(group);
ConfigInfo configInfo = new ConfigInfo();
if (StringUtils.equals(key, GLOBAL_CONFIGURATION_NAME)) {
configInfo.setServiceName(CommonConst.GLOBAL_CONFIGURATION_SERVICE_NAME);
} else if (key.startsWith(SERVICE_CONFIGURATION_NAME_PREFIX)) {
configInfo.setServiceName(key.replace(SERVICE_CONFIGURATION_NAME_PREFIX, ""));
}
configInfo.setAppName(map.get(APP_KEY));
configInfo.setEnvironment(map.get(ENVIRONMENT_KEY));
configInfo.setZone(map.get(ZONE_KEY));
configInfo.setKey(key);
configInfo.setGroup(group);
configInfo.setPluginType(PluginType.DATABASE_WRITE_PROHIBITION.getPluginName());
return configInfo;
}

@Override
public boolean verifyConfiguration(String key, String group) {
if (StringUtils.isBlank(key) || StringUtils.isBlank(group)) {
return false;
}
if (!group.matches(PATTERN)) {
return false;
}
return key.equals(GLOBAL_CONFIGURATION_NAME) || key.startsWith(SERVICE_CONFIGURATION_NAME_PREFIX);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright (C) 2024-2024 Sermant Authors. All rights reserved.
*
* 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 io.sermant.backend.handler.config;

import io.sermant.backend.entity.config.ConfigInfo;
import io.sermant.backend.entity.config.PluginType;

import org.apache.commons.lang.StringUtils;

import java.util.Map;

/**
* Flow control plugin handler
*
* @author zhp
* @since 2024-05-16
*/
public class FlowControlPluginHandler extends PluginConfigHandler {
private static final String[] CONFIGURATION_NAME_PREFIX_ARRAY = {"servicecomb.rateLimiting.",
"servicecomb.matchGroup.", "servicecomb.circuitBreaker.", "servicecomb.bulkhead.",
"servicecomb.faultInjection.", "servicecomb.retry.", "servicecomb.system."};

private static final String PATTERN = "^service=[^&]*$";

@Override
public ConfigInfo parsePluginInfo(String key, String group) {
Map<String, String> map = parseGroup(group);
ConfigInfo configInfo = new ConfigInfo();
configInfo.setServiceName(map.get(SERVICE_KEY));
configInfo.setKey(key);
configInfo.setGroup(group);
configInfo.setPluginType(PluginType.FLOW_CONTROL.getPluginName());
return configInfo;
}

@Override
public boolean verifyConfiguration(String key, String group) {
if (StringUtils.isBlank(key) || StringUtils.isBlank(group)) {
return false;
}
if (!group.matches(PATTERN)) {
return false;
}
for (String name : CONFIGURATION_NAME_PREFIX_ARRAY) {
if (key.startsWith(name)) {
return true;
}
}
return false;
}
}
Loading
Loading