Skip to content

Commit 23903ba

Browse files
chickenljcvictory
authored andcommitted
[Dubbo-3266] Change DynamicConfiguration definition to better adapt to Apollo's namespace storage model. (#3271)
1 parent 13f3238 commit 23903ba

File tree

17 files changed

+165
-85
lines changed

17 files changed

+165
-85
lines changed

dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractInterfaceConfig.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,12 +287,12 @@ private void prepareEnvironment() {
287287
return;
288288
}
289289
DynamicConfiguration dynamicConfiguration = getDynamicConfiguration(configCenter.toUrl());
290-
String configContent = dynamicConfiguration.getConfig(configCenter.getConfigFile(), configCenter.getGroup());
290+
String configContent = dynamicConfiguration.getConfigs(configCenter.getConfigFile(), configCenter.getGroup());
291291

292292
String appGroup = application != null ? application.getName() : null;
293293
String appConfigContent = null;
294294
if (StringUtils.isNotEmpty(appGroup)) {
295-
appConfigContent = dynamicConfiguration.getConfig
295+
appConfigContent = dynamicConfiguration.getConfigs
296296
(StringUtils.isNotEmpty(configCenter.getAppConfigFile()) ? configCenter.getAppConfigFile() : configCenter.getConfigFile(),
297297
appGroup
298298
);

dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ConfigCenterConfig.java

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import org.apache.dubbo.common.URL;
2020
import org.apache.dubbo.common.config.Environment;
21+
import org.apache.dubbo.common.constants.CommonConstants;
2122
import org.apache.dubbo.common.utils.StringUtils;
2223
import org.apache.dubbo.common.utils.UrlUtils;
2324
import org.apache.dubbo.config.support.Parameter;
@@ -28,15 +29,14 @@
2829
import static org.apache.dubbo.common.constants.CommonConstants.ANYHOST_VALUE;
2930
import static org.apache.dubbo.common.constants.CommonConstants.PATH_KEY;
3031
import static org.apache.dubbo.common.constants.CommonConstants.PROTOCOL_KEY;
31-
import static org.apache.dubbo.config.Constants.CONFIG_APPNAME_KEY;
3232
import static org.apache.dubbo.common.constants.ConfigConstants.CONFIG_CHECK_KEY;
3333
import static org.apache.dubbo.common.constants.ConfigConstants.CONFIG_CLUSTER_KEY;
34-
import static org.apache.dubbo.config.Constants.CONFIG_CONFIGFILE_KEY;
35-
import static org.apache.dubbo.config.Constants.CONFIG_ENABLE_KEY;
3634
import static org.apache.dubbo.common.constants.ConfigConstants.CONFIG_GROUP_KEY;
3735
import static org.apache.dubbo.common.constants.ConfigConstants.CONFIG_NAMESPACE_KEY;
38-
import static org.apache.dubbo.config.Constants.CONFIG_TIMEOUT_KEY;
3936
import static org.apache.dubbo.common.constants.ConfigConstants.ZOOKEEPER_PROTOCOL;
37+
import static org.apache.dubbo.config.Constants.CONFIG_CONFIGFILE_KEY;
38+
import static org.apache.dubbo.config.Constants.CONFIG_ENABLE_KEY;
39+
import static org.apache.dubbo.config.Constants.CONFIG_TIMEOUT_KEY;
4040

4141
/**
4242
* ConfigCenterConfig
@@ -46,20 +46,44 @@ public class ConfigCenterConfig extends AbstractConfig {
4646

4747
private String protocol;
4848
private String address;
49+
50+
/* The config center cluster, it's real meaning may very on different Config Center products. */
4951
private String cluster;
50-
private String namespace = "dubbo";
51-
private String group = "dubbo";
52+
53+
/* The namespace of the config center, generally it's used for multi-tenant,
54+
but it's real meaning depends on the actual Config Center you use.
55+
*/
56+
57+
private String namespace = CommonConstants.DUBBO;
58+
/* The group of the config center, generally it's used to identify an isolated space for a batch of config items,
59+
but it's real meaning depends on the actual Config Center you use.
60+
*/
61+
private String group = CommonConstants.DUBBO;
5262
private String username;
5363
private String password;
5464
private Long timeout = 3000L;
65+
66+
// If the Config Center is given the highest priority, it will override all the other configurations
5567
private Boolean highestPriority = true;
68+
69+
// Decide the behaviour when initial connection try fails, 'true' means interrupt the whole process once fail.
5670
private Boolean check = true;
5771

58-
private String appName;
59-
private String configFile = "dubbo.properties";
72+
/* Used to specify the key that your properties file mapping to, most of the time you do not need to change this parameter.
73+
Notice that for Apollo, this parameter is meaningless, set the 'namespace' is enough.
74+
*/
75+
private String configFile = CommonConstants.DEFAULT_DUBBO_PROPERTIES;
76+
77+
/* the .properties file under 'configFile' is global shared while .properties under this one is limited only to this application
78+
*/
6079
private String appConfigFile;
6180

62-
// customized parameters
81+
/* If the Config Center product you use have some special parameters that is not covered by this class, you can add it to here.
82+
For example, with XML:
83+
<dubbo:config-center>
84+
<dubbo:parameter key="config.{your key}" value="{your value}" />
85+
</dubbo:config-center>
86+
*/
6387
private Map<String, String> parameters;
6488

6589
public ConfigCenterConfig() {
@@ -195,15 +219,6 @@ public void setAppConfigFile(String appConfigFile) {
195219
this.appConfigFile = appConfigFile;
196220
}
197221

198-
@Parameter(key = CONFIG_APPNAME_KEY, useKeyAsProperty = false)
199-
public String getAppName() {
200-
return appName;
201-
}
202-
203-
public void setAppName(String appName) {
204-
this.appName = appName;
205-
}
206-
207222
public Map<String, String> getParameters() {
208223
return parameters;
209224
}

dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/ConfigCenterBuilder.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,6 @@ public ConfigCenterBuilder check(Boolean check) {
9494
return getThis();
9595
}
9696

97-
public ConfigCenterBuilder appName(String appName) {
98-
this.appName = appName;
99-
return getThis();
100-
}
101-
10297
public ConfigCenterBuilder configFile(String configFile) {
10398
this.configFile = configFile;
10499
return getThis();
@@ -133,7 +128,6 @@ public ConfigCenterConfig build() {
133128
configCenter.setTimeout(timeout);
134129
configCenter.setHighestPriority(highestPriority);
135130
configCenter.setCheck(check);
136-
configCenter.setAppName(appName);
137131
configCenter.setConfigFile(configFile);
138132
configCenter.setAppConfigFile(appConfigFile);
139133
configCenter.setParameters(parameters);

dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/AbstractConfigTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,26 @@ public void tetMetaData() {
467467
Assertions.assertNull(metaData.get("key2"));
468468
}
469469

470+
@Test
471+
public void testEquals() {
472+
ApplicationConfig application1 = new ApplicationConfig();
473+
ApplicationConfig application2 = new ApplicationConfig();
474+
application1.setName("app1");
475+
application2.setName("app2");
476+
Assertions.assertNotEquals(application1, application2);
477+
application1.setName("sameName");
478+
application2.setName("sameName");
479+
Assertions.assertEquals(application1, application2);
480+
481+
ProtocolConfig protocol1 = new ProtocolConfig();
482+
protocol1.setHost("127.0.0.1");// excluded
483+
protocol1.setName("dubbo");
484+
ProtocolConfig protocol2 = new ProtocolConfig();
485+
protocol2.setHost("127.0.0.2");// excluded
486+
protocol2.setName("dubbo");
487+
Assertions.assertEquals(protocol1, protocol2);
488+
}
489+
470490
@Retention(RetentionPolicy.RUNTIME)
471491
@Target({ElementType.ANNOTATION_TYPE})
472492
public @interface ConfigField {

dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/ConfigCenterBuilderTest.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,6 @@ void check() {
9696
Assertions.assertTrue(builder.build().isCheck());
9797
}
9898

99-
@Test
100-
void appName() {
101-
ConfigCenterBuilder builder = new ConfigCenterBuilder();
102-
builder.appName("appName");
103-
Assertions.assertEquals("appName", builder.build().getAppName());
104-
}
105-
10699
@Test
107100
void configFile() {
108101
ConfigCenterBuilder builder = new ConfigCenterBuilder();
@@ -146,7 +139,7 @@ void appendParameters() {
146139
@Test
147140
void build() {
148141
ConfigCenterBuilder builder = new ConfigCenterBuilder();
149-
builder.check(true).protocol("protocol").address("address").appConfigFile("appConfigFile").appName("appName")
142+
builder.check(true).protocol("protocol").address("address").appConfigFile("appConfigFile")
150143
.cluster("cluster").configFile("configFile").group("group").highestPriority(false)
151144
.namespace("namespace").password("password").timeout(1000L).username("usernama")
152145
.appendParameter("default.num", "one").id("id").prefix("prefix");
@@ -160,7 +153,6 @@ void build() {
160153
Assertions.assertEquals("protocol", config.getProtocol());
161154
Assertions.assertEquals("address", config.getAddress());
162155
Assertions.assertEquals("appConfigFile", config.getAppConfigFile());
163-
Assertions.assertEquals("appName", config.getAppName());
164156
Assertions.assertEquals("cluster", config.getCluster());
165157
Assertions.assertEquals("configFile", config.getConfigFile());
166158
Assertions.assertEquals("group", config.getGroup());

dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/ConfigCenterBean.java

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,10 @@
1818

1919
import org.apache.dubbo.common.config.ConfigurationUtils;
2020
import org.apache.dubbo.common.utils.StringUtils;
21-
import org.apache.dubbo.config.ApplicationConfig;
2221
import org.apache.dubbo.config.ConfigCenterConfig;
2322
import org.apache.dubbo.config.spring.extension.SpringExtensionFactory;
2423

25-
import org.springframework.beans.factory.BeanFactoryUtils;
2624
import org.springframework.beans.factory.DisposableBean;
27-
import org.springframework.beans.factory.InitializingBean;
2825
import org.springframework.context.ApplicationContext;
2926
import org.springframework.context.ApplicationContextAware;
3027
import org.springframework.context.EnvironmentAware;
@@ -41,40 +38,18 @@
4138
* <p>
4239
* If use ConfigCenterConfig directly, you should make sure ConfigCenterConfig.init() is called before actually export/refer any Dubbo service.
4340
*/
44-
public class ConfigCenterBean extends ConfigCenterConfig implements InitializingBean, ApplicationContextAware, DisposableBean, EnvironmentAware {
41+
public class ConfigCenterBean extends ConfigCenterConfig implements ApplicationContextAware, DisposableBean, EnvironmentAware {
4542

4643
private transient ApplicationContext applicationContext;
4744

4845
private Boolean includeSpringEnv = false;
49-
private ApplicationConfig application;
5046

5147
@Override
5248
public void setApplicationContext(ApplicationContext applicationContext) {
5349
this.applicationContext = applicationContext;
5450
SpringExtensionFactory.addApplicationContext(applicationContext);
5551
}
5652

57-
@Override
58-
public void afterPropertiesSet() throws Exception {
59-
if (getApplication() == null) {
60-
Map<String, ApplicationConfig> applicationConfigMap = applicationContext == null ? null : BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, ApplicationConfig.class, false, false);
61-
if (applicationConfigMap != null && applicationConfigMap.size() > 0) {
62-
ApplicationConfig applicationConfig = null;
63-
for (ApplicationConfig config : applicationConfigMap.values()) {
64-
if (config.isDefault() == null || config.isDefault()) {
65-
if (applicationConfig != null) {
66-
throw new IllegalStateException("Duplicate application configs: " + applicationConfig + " and " + config);
67-
}
68-
applicationConfig = config;
69-
}
70-
}
71-
if (applicationConfig != null) {
72-
setApplication(applicationConfig);
73-
}
74-
}
75-
}
76-
}
77-
7853
@Override
7954
public void destroy() throws Exception {
8055

@@ -83,8 +58,11 @@ public void destroy() throws Exception {
8358
@Override
8459
public void setEnvironment(Environment environment) {
8560
if (includeSpringEnv) {
61+
// Get PropertySource mapped to 'dubbo.properties' in Spring Environment.
8662
Map<String, String> externalProperties = getConfigurations(getConfigFile(), environment);
87-
Map<String, String> appExternalProperties = getConfigurations(StringUtils.isNotEmpty(getAppConfigFile()) ? getAppConfigFile() : (StringUtils.isEmpty(getAppName()) ? ("application." + getConfigFile()) : (getAppName() + "." + getConfigFile())), environment);
63+
// Get PropertySource mapped to 'application.dubbo.properties' in Spring Environment.
64+
Map<String, String> appExternalProperties = getConfigurations(StringUtils.isNotEmpty(getAppConfigFile()) ? getAppConfigFile() : ("application." + getConfigFile()), environment);
65+
// Refresh Dubbo Environment
8866
org.apache.dubbo.common.config.Environment.getInstance().setExternalConfigMap(externalProperties);
8967
org.apache.dubbo.common.config.Environment.getInstance().setAppExternalConfigMap(appExternalProperties);
9068
}
@@ -130,11 +108,4 @@ public void setIncludeSpringEnv(Boolean includeSpringEnv) {
130108
this.includeSpringEnv = includeSpringEnv;
131109
}
132110

133-
public ApplicationConfig getApplication() {
134-
return application;
135-
}
136-
137-
public void setApplication(ApplicationConfig application) {
138-
this.application = application;
139-
}
140111
}

dubbo-config/dubbo-config-spring/src/main/resources/META-INF/compat/dubbo.xsd

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -650,18 +650,19 @@
650650
</xsd:attribute>
651651
<xsd:attribute name="cluster" type="xsd:string" use="optional">
652652
<xsd:annotation>
653-
<xsd:documentation><![CDATA[ The config center cluster. ]]></xsd:documentation>
653+
<xsd:documentation><![CDATA[ The config center cluster, it's real meaning may very on different Config Center products. ]]></xsd:documentation>
654654
</xsd:annotation>
655655
</xsd:attribute>
656656
<xsd:attribute name="namespace" type="xsd:string" use="optional">
657657
<xsd:annotation>
658658
<xsd:documentation>
659-
<![CDATA[ The group of the config center, an isolated space for config items in the same config center. ]]></xsd:documentation>
659+
<![CDATA[ The namespace of the config center, generally it's used for multi-tenant, but it's real meaning depends on the actual Config Center you use. ]]></xsd:documentation>
660660
</xsd:annotation>
661661
</xsd:attribute>
662-
<xsd:attribute name="app-name" type="xsd:string" use="optional">
662+
<xsd:attribute name="group" type="xsd:string" use="optional">
663663
<xsd:annotation>
664-
<xsd:documentation><![CDATA[ The group of the config center. ]]></xsd:documentation>
664+
<xsd:documentation>
665+
<![CDATA[ The group of the config center, generally it's used to identify an isolated space for a batch of config items, but it's real meaning depends on the actual Config Center you use.. ]]></xsd:documentation>
665666
</xsd:annotation>
666667
</xsd:attribute>
667668
<xsd:attribute name="config-file" type="xsd:string" use="optional">

dubbo-config/dubbo-config-spring/src/main/resources/META-INF/dubbo.xsd

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -644,18 +644,19 @@
644644
</xsd:attribute>
645645
<xsd:attribute name="cluster" type="xsd:string" use="optional">
646646
<xsd:annotation>
647-
<xsd:documentation><![CDATA[ The config center cluster. ]]></xsd:documentation>
647+
<xsd:documentation><![CDATA[ The config center cluster, it's real meaning may very on different Config Center products. ]]></xsd:documentation>
648648
</xsd:annotation>
649649
</xsd:attribute>
650650
<xsd:attribute name="namespace" type="xsd:string" use="optional">
651651
<xsd:annotation>
652652
<xsd:documentation>
653-
<![CDATA[ The group of the config center, an isolated space for config items in the same config center. ]]></xsd:documentation>
653+
<![CDATA[ The namespace of the config center, generally it's used for multi-tenant, but it's real meaning depends on the actual Config Center you use. ]]></xsd:documentation>
654654
</xsd:annotation>
655655
</xsd:attribute>
656-
<xsd:attribute name="app-name" type="xsd:string" use="optional">
656+
<xsd:attribute name="group" type="xsd:string" use="optional">
657657
<xsd:annotation>
658-
<xsd:documentation><![CDATA[ The group of the config center. ]]></xsd:documentation>
658+
<xsd:documentation>
659+
<![CDATA[ The group of the config center, generally it's used to identify an isolated space for a batch of config items, but it's real meaning depends on the actual Config Center you use. ]]></xsd:documentation>
659660
</xsd:annotation>
660661
</xsd:attribute>
661662
<xsd:attribute name="config-file" type="xsd:string" use="optional">

dubbo-configcenter/dubbo-configcenter-api/src/main/java/org/apache/dubbo/configcenter/DynamicConfiguration.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,14 @@
2424
import static org.apache.dubbo.common.extension.ExtensionLoader.getExtensionLoader;
2525

2626
/**
27-
* Dynamic configuration
27+
* Dynamic Configuration
28+
* <br/>
29+
* From the use scenario internally in framework, there're mainly three kinds of methods:
30+
* <ul>
31+
* <li>1. getConfig, get governance rules or single config item from Config Center.</li>
32+
* <li>2. getConfigFile, get configuration file from Config Center at start up.</li>
33+
* <li>3. addListener/removeListener, add or remove listeners for governance rules or config items that need to watch.</li>
34+
* </ul>
2835
*/
2936
public interface DynamicConfiguration extends Configuration {
3037
String DEFAULT_GROUP = "dubbo";
@@ -78,7 +85,7 @@ default void removeListener(String key, ConfigurationListener listener) {
7885
* @return target configuration mapped to the given key
7986
*/
8087
default String getConfig(String key) {
81-
return getConfig(key, null, 0L);
88+
return getConfig(key, null, -1L);
8289
}
8390

8491
/**
@@ -89,7 +96,7 @@ default String getConfig(String key) {
8996
* @return target configuration mapped to the given key and the given group
9097
*/
9198
default String getConfig(String key, String group) {
92-
return getConfig(key, group, 0L);
99+
return getConfig(key, group, -1L);
93100
}
94101

95102
/**
@@ -104,6 +111,22 @@ default String getConfig(String key, String group) {
104111
*/
105112
String getConfig(String key, String group, long timeout) throws IllegalStateException;
106113

114+
/**
115+
* {@see #getConfig(String, String, long)}
116+
*
117+
* This method are mostly used to get a compound config file, such as a complete dubbo.properties file.
118+
*/
119+
default String getConfigs(String key, String group) throws IllegalStateException {
120+
return getConfigs(key, group, -1L);
121+
}
122+
123+
/**
124+
* {@see #getConfig(String, String, long)}
125+
*
126+
* This method are mostly used to get a compound config file, such as a complete dubbo.properties file.
127+
*/
128+
String getConfigs(String key, String group, long timeout) throws IllegalStateException;
129+
107130
/**
108131
* Find DynamicConfiguration instance
109132
*

dubbo-configcenter/dubbo-configcenter-api/src/main/java/org/apache/dubbo/configcenter/support/nop/NopDynamicConfiguration.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,9 @@ public void removeListener(String key, String group, ConfigurationListener liste
5050
public String getConfig(String key, String group, long timeout) throws IllegalStateException {
5151
return null;
5252
}
53+
54+
@Override
55+
public String getConfigs(String key, String group, long timeout) throws IllegalStateException {
56+
return null;
57+
}
5358
}

0 commit comments

Comments
 (0)