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

unify config-center model #4388

Merged
merged 4 commits into from
Jul 4, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -119,7 +119,7 @@ private synchronized void init(String ruleKey) {
}
String routerKey = ruleKey + RULE_SUFFIX;
configuration.addListener(routerKey, this);
String rule = configuration.getConfig(routerKey);
String rule = configuration.getRule(routerKey, DynamicConfiguration.DEFAULT_GROUP);
if (StringUtils.isNotEmpty(rule)) {
this.process(new ConfigChangeEvent(routerKey, rule));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class ServiceRouter extends ListenableRouter {
private static final int SERVICE_ROUTER_DEFAULT_PRIORITY = 140;

public ServiceRouter(DynamicConfiguration configuration, URL url) {
super(configuration, url, url.getEncodedServiceKey());
super(configuration, url, DynamicConfiguration.getRuleKey(url));
this.priority = SERVICE_ROUTER_DEFAULT_PRIORITY;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
import java.util.function.Predicate;
import java.util.stream.Collectors;

import static org.apache.dubbo.rpc.cluster.Constants.TAG_KEY;
import static org.apache.dubbo.rpc.Constants.FORCE_USE_TAG;
import static org.apache.dubbo.rpc.cluster.Constants.TAG_KEY;

/**
* TagRouter, "application.tag-router"
Expand Down Expand Up @@ -249,7 +249,7 @@ public <T> void notify(List<Invoker<T>> invokers) {
String key = providerApplication + RULE_SUFFIX;
configuration.addListener(key, this);
application = providerApplication;
String rawRule = configuration.getConfig(key);
String rawRule = configuration.getRule(key, DynamicConfiguration.DEFAULT_GROUP);
if (StringUtils.isNotEmpty(rawRule)) {
this.process(new ConfigChangeEvent(key, rawRule));
}
Expand Down
33 changes: 23 additions & 10 deletions dubbo-common/src/main/java/org/apache/dubbo/common/URL.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,20 @@
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;

import static org.apache.dubbo.common.constants.CommonConstants.HOST_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.PORT_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.ANYHOST_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.ANYHOST_VALUE;
import static org.apache.dubbo.common.constants.CommonConstants.COMMA_SPLIT_PATTERN;
import static org.apache.dubbo.common.constants.CommonConstants.DEFAULT_KEY_PREFIX;
import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.HOST_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.INTERFACE_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.LOCALHOST_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.PASSWORD_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.PATH_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.PORT_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.PROTOCOL_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.PASSWORD_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.USERNAME_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY;

/**
* URL - Uniform Resource Locator (Immutable, ThreadSafe)
Expand Down Expand Up @@ -1246,14 +1246,27 @@ public InetSocketAddress toInetSocketAddress() {
}

/**
* The format is '{group}*{interfaceName}:{version}'
*
* The format is "{interface}:[version]:[group]"
* @return
*/
public String getEncodedServiceKey() {
String serviceKey = this.getServiceKey();
serviceKey = serviceKey.replaceFirst("/", "*");
return serviceKey;
public String getColonSeparatedKey() {
StringBuilder serviceNameBuilder = new StringBuilder();
append(serviceNameBuilder, INTERFACE_KEY, true);
append(serviceNameBuilder, VERSION_KEY, false);
append(serviceNameBuilder, GROUP_KEY, false);
return serviceNameBuilder.toString();
}

private void append(StringBuilder target, String parameterName, boolean first) {
String parameterValue = this.getParameter(parameterName);
if (!StringUtils.isBlank(parameterValue)) {
if (!first) {
target.append(":");
}
target.append(parameterValue);
} else {
target.append(":");
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public interface CommonConstants {

String COMMA_SEPARATOR = ",";

String DOT_SEPARATOR = ".";

Pattern COMMA_SPLIT_PATTERN = Pattern.compile("\\s*[,]+\\s*");

public final static String PATH_SEPARATOR = "/";
Expand Down
15 changes: 15 additions & 0 deletions dubbo-common/src/test/java/org/apache/dubbo/common/URLTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -705,4 +705,19 @@ public void testGetServiceKey () {
URL url5 = URL.valueOf("10.20.130.230:20880/context/path?interface=org.apache.dubbo.test.interfaceName&group=group1&version=1.0.0");
Assertions.assertEquals("group1/context/path:1.0.0", url5.getPathKey());
}

@Test
public void testGetColonSeparatedKey() {
URL url1 = URL.valueOf("10.20.130.230:20880/context/path?interface=org.apache.dubbo.test.interfaceName&group=group&version=1.0.0");
Assertions.assertEquals("org.apache.dubbo.test.interfaceName:1.0.0:group", url1.getColonSeparatedKey());

URL url2 = URL.valueOf("10.20.130.230:20880/context/path?interface=org.apache.dubbo.test.interfaceName&version=1.0.0");
Assertions.assertEquals("org.apache.dubbo.test.interfaceName:1.0.0:", url2.getColonSeparatedKey());

URL url3 = URL.valueOf("10.20.130.230:20880/context/path?interface=org.apache.dubbo.test.interfaceName&group=group");
Assertions.assertEquals("org.apache.dubbo.test.interfaceName::group", url3.getColonSeparatedKey());

URL url4 = URL.valueOf("10.20.130.230:20880/context/path?interface=org.apache.dubbo.test.interfaceName");
Assertions.assertEquals("org.apache.dubbo.test.interfaceName::", url4.getColonSeparatedKey());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,31 +53,30 @@
import java.util.Set;

import static org.apache.dubbo.common.config.ConfigurationUtils.parseProperties;
import static org.apache.dubbo.rpc.cluster.Constants.TAG_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.ANYHOST_VALUE;
import static org.apache.dubbo.common.constants.CommonConstants.CLUSTER_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.COMMA_SEPARATOR;
import static org.apache.dubbo.common.constants.CommonConstants.COMMA_SPLIT_PATTERN;
import static org.apache.dubbo.common.constants.CommonConstants.DUBBO_PROTOCOL;
import static org.apache.dubbo.common.constants.CommonConstants.FILE_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.INTERFACE_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.PATH_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.PID_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.PROTOCOL_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.RELEASE_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.SHUTDOWN_WAIT_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.SHUTDOWN_WAIT_SECONDS_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.TIMESTAMP_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.CLUSTER_KEY;
import static org.apache.dubbo.common.constants.RegistryConstants.REGISTRY_KEY;
import static org.apache.dubbo.common.constants.RegistryConstants.REGISTRY_PROTOCOL;
import static org.apache.dubbo.common.extension.ExtensionLoader.getExtensionLoader;
import static org.apache.dubbo.config.Constants.DUBBO_IP_TO_REGISTRY;
import static org.apache.dubbo.common.constants.CommonConstants.DUBBO_PROTOCOL;
import static org.apache.dubbo.config.Constants.LAYER_KEY;
import static org.apache.dubbo.config.Constants.LISTENER_KEY;
import static org.apache.dubbo.rpc.cluster.Constants.REFER_KEY;
import static org.apache.dubbo.registry.Constants.REGISTER_IP_KEY;
import static org.apache.dubbo.config.Constants.REGISTRIES_SUFFIX;
import static org.apache.dubbo.common.constants.CommonConstants.SHUTDOWN_WAIT_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.SHUTDOWN_WAIT_SECONDS_KEY;
import static org.apache.dubbo.monitor.Constants.LOGSTAT_PROTOCOL;
import static org.apache.dubbo.registry.Constants.REGISTER_IP_KEY;
import static org.apache.dubbo.registry.Constants.REGISTER_KEY;
import static org.apache.dubbo.common.constants.RegistryConstants.REGISTRY_KEY;
import static org.apache.dubbo.common.constants.RegistryConstants.REGISTRY_PROTOCOL;
import static org.apache.dubbo.registry.Constants.SUBSCRIBE_KEY;
import static org.apache.dubbo.remoting.Constants.DUBBO_VERSION_KEY;
import static org.apache.dubbo.rpc.Constants.INVOKER_LISTENER_KEY;
Expand All @@ -86,7 +85,8 @@
import static org.apache.dubbo.rpc.Constants.REFERENCE_FILTER_KEY;
import static org.apache.dubbo.rpc.Constants.RETURN_PREFIX;
import static org.apache.dubbo.rpc.Constants.THROW_PREFIX;
import static org.apache.dubbo.common.extension.ExtensionLoader.getExtensionLoader;
import static org.apache.dubbo.rpc.cluster.Constants.REFER_KEY;
import static org.apache.dubbo.rpc.cluster.Constants.TAG_KEY;

/**
* AbstractDefaultConfig
Expand Down Expand Up @@ -287,12 +287,12 @@ private void prepareEnvironment() {
return;
}
DynamicConfiguration dynamicConfiguration = getDynamicConfiguration(configCenter.toUrl());
String configContent = dynamicConfiguration.getConfigs(configCenter.getConfigFile(), configCenter.getGroup());
String configContent = dynamicConfiguration.getProperties(configCenter.getConfigFile(), configCenter.getGroup());

String appGroup = application != null ? application.getName() : null;
String appConfigContent = null;
if (StringUtils.isNotEmpty(appGroup)) {
appConfigContent = dynamicConfiguration.getConfigs
appConfigContent = dynamicConfiguration.getProperties
(StringUtils.isNotEmpty(configCenter.getAppConfigFile()) ? configCenter.getAppConfigFile() : configCenter.getConfigFile(),
appGroup
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.dubbo.configcenter;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.config.Configuration;
import org.apache.dubbo.common.config.Environment;

Expand Down Expand Up @@ -79,53 +80,41 @@ default void removeListener(String key, ConfigurationListener listener) {
void removeListener(String key, String group, ConfigurationListener listener);

/**
* Get the configuration mapped to the given key
*
* @param key the key to represent a configuration
* @return target configuration mapped to the given key
*/
default String getConfig(String key) {
return getConfig(key, null, -1L);
}

/**
* Get the configuration mapped to the given key and the given group
* Get the governance rule mapped to the given key and the given group
*
* @param key the key to represent a configuration
* @param group the group where the key belongs to
* @return target configuration mapped to the given key and the given group
*/
default String getConfig(String key, String group) {
return getConfig(key, group, -1L);
default String getRule(String key, String group) {
return getRule(key, group, -1L);
}

/**
* Get the configuration mapped to the given key and the given group. If the
* configuration fails to fetch after timeout exceeds, IllegalStateException will be thrown.
* Get the governance rule mapped to the given key and the given group. If the
* rule fails to return after timeout exceeds, IllegalStateException will be thrown.
*
* @param key the key to represent a configuration
* @param group the group where the key belongs to
* @param timeout timeout value for fetching the target config
* @return target configuration mapped to the given key and the given group, IllegalStateException will be thrown
* if timeout exceeds.
*/
String getConfig(String key, String group, long timeout) throws IllegalStateException;
String getRule(String key, String group, long timeout) throws IllegalStateException;

/**
* {@see #getConfig(String, String, long)}
*
* This method are mostly used to get a compound config file, such as a complete dubbo.properties file.
* Also {@see #getConfig(String, String)}
*/
default String getConfigs(String key, String group) throws IllegalStateException {
return getConfigs(key, group, -1L);
default String getProperties(String key, String group) throws IllegalStateException {
return getProperties(key, group, -1L);
}

/**
* {@see #getConfig(String, String, long)}
*
* This method are mostly used to get a compound config file, such as a complete dubbo.properties file.
* Also {@see #getConfig(String, String, long)}
*/
String getConfigs(String key, String group, long timeout) throws IllegalStateException;
String getProperties(String key, String group, long timeout) throws IllegalStateException;

/**
* Find DynamicConfiguration instance
Expand All @@ -138,4 +127,13 @@ static DynamicConfiguration getDynamicConfiguration() {
.getDefaultExtension()
.getDynamicConfiguration(null));
}

/**
* The format is '{interfaceName}:[version]:[group]'
*
* @return
*/
static String getRuleKey(URL url) {
return url.getColonSeparatedKey();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ public void removeListener(String key, String group, ConfigurationListener liste
}

@Override
public String getConfig(String key, String group, long timeout) throws IllegalStateException {
public String getRule(String key, String group, long timeout) throws IllegalStateException {
return null;
}

@Override
public String getConfigs(String key, String group, long timeout) throws IllegalStateException {
public String getProperties(String key, String group, long timeout) throws IllegalStateException {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ public void removeListener(String key, String group, ConfigurationListener liste
}

@Override
public String getConfig(String key, String group, long timeout) throws IllegalStateException {
public String getRule(String key, String group, long timeout) throws IllegalStateException {
return null;
}

@Override
public String getConfigs(String key, String group, long timeout) throws IllegalStateException {
public String getProperties(String key, String group, long timeout) throws IllegalStateException {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,8 @@ public void removeListener(String key, String group, ConfigurationListener liste
}
}

/**
* This method will be used to:
* 1. get configuration file at startup phase
* 2. get all kinds of Dubbo rules
*/
@Override
public String getConfig(String key, String group, long timeout) throws IllegalStateException {
public String getRule(String key, String group, long timeout) throws IllegalStateException {
if (StringUtils.isNotEmpty(group)) {
if (group.equals(url.getParameter(APPLICATION_KEY))) {
return ConfigService.getAppConfig().getProperty(key, null);
Expand All @@ -152,7 +147,7 @@ public String getConfig(String key, String group, long timeout) throws IllegalSt
}

@Override
public String getConfigs(String key, String group, long timeout) throws IllegalStateException {
public String getProperties(String key, String group, long timeout) throws IllegalStateException {
if(StringUtils.isEmpty(group)) {
return dubboConfigFile.getContent();
}
Expand All @@ -177,7 +172,6 @@ public String getInternalProperty(String key) {
return dubboConfig.getProperty(key, null);
}


/**
* Ignores the group parameter.
*
Expand Down
Loading