This is an automated email from the ASF dual-hosted git repository.
liubao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-java-chassis.git
The following commit(s) were added to refs/heads/master by this push:
new c1ddf4a5c [SCB-2813]Nacos dynamic properties support and improvement
(#3984)
c1ddf4a5c is described below
commit c1ddf4a5c16bf68cce953e0d37e5af4adff33d3e
Author: liubao68 <[email protected]>
AuthorDate: Mon Oct 23 17:00:29 2023 +0800
[SCB-2813]Nacos dynamic properties support and improvement (#3984)
---
.../config/nacos/ConfigurationAction.java | 24 --
.../servicecomb/config/nacos/NacosClient.java | 282 +++++++++++++++------
.../servicecomb/config/nacos/NacosConfig.java | 56 +++-
.../config/nacos/NacosConfigConfiguration.java | 21 --
.../config/nacos/NacosDynamicPropertiesSource.java | 44 +---
...rk.boot.autoconfigure.AutoConfiguration.imports | 18 --
.../servicecomb/config/nacos/NacosClientTest.java | 80 ------
.../registry/nacos/NacosDiscoveryProperties.java | 2 +-
.../registry/nacos/NacosRegistration.java | 3 +-
.../registry/nacos/NacosRegistrationInstance.java | 11 +-
.../registry/nacos/NamingServiceManager.java | 21 +-
11 files changed, 280 insertions(+), 282 deletions(-)
diff --git
a/dynamic-config/config-nacos/src/main/java/org/apache/servicecomb/config/nacos/ConfigurationAction.java
b/dynamic-config/config-nacos/src/main/java/org/apache/servicecomb/config/nacos/ConfigurationAction.java
deleted file mode 100644
index a4dc8f53c..000000000
---
a/dynamic-config/config-nacos/src/main/java/org/apache/servicecomb/config/nacos/ConfigurationAction.java
+++ /dev/null
@@ -1,24 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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 org.apache.servicecomb.config.nacos;
-
-public enum ConfigurationAction {
- CREATE,
- SET,
- DELETE
-}
diff --git
a/dynamic-config/config-nacos/src/main/java/org/apache/servicecomb/config/nacos/NacosClient.java
b/dynamic-config/config-nacos/src/main/java/org/apache/servicecomb/config/nacos/NacosClient.java
index 2f774571a..2fd17523c 100644
---
a/dynamic-config/config-nacos/src/main/java/org/apache/servicecomb/config/nacos/NacosClient.java
+++
b/dynamic-config/config-nacos/src/main/java/org/apache/servicecomb/config/nacos/NacosClient.java
@@ -17,126 +17,246 @@
package org.apache.servicecomb.config.nacos;
-import static org.apache.servicecomb.config.nacos.ConfigurationAction.CREATE;
-import static org.apache.servicecomb.config.nacos.ConfigurationAction.DELETE;
-import static org.apache.servicecomb.config.nacos.ConfigurationAction.SET;
-
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
-import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;
import org.apache.commons.lang3.StringUtils;
+import org.apache.servicecomb.config.BootStrapProperties;
import
org.apache.servicecomb.config.nacos.NacosDynamicPropertiesSource.UpdateHandler;
import org.apache.servicecomb.config.parser.Parser;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
import org.springframework.core.env.Environment;
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
+import com.alibaba.nacos.api.exception.NacosException;
public class NacosClient {
+ private final UpdateHandler updateHandler;
+
+ private final NacosConfig nacosConfig;
- private static final Logger LOGGER =
LoggerFactory.getLogger(NacosClient.class);
+ private final Environment environment;
- private static final Map<String, Object> originalConfigMap = new
ConcurrentHashMap<>();
+ private final Object lock = new Object();
- private final UpdateHandler updateHandler;
+ private Map<String, Object> application = new HashMap<>();
- private final NacosConfig nacosConfig;
+ private Map<String, Object> service = new HashMap<>();
+
+ private Map<String, Object> version = new HashMap<>();
+
+ private Map<String, Object> profile = new HashMap<>();
+
+ private Map<String, Object> custom = new HashMap<>();
+
+ private Map<String, Object> allLast = new HashMap<>();
public NacosClient(UpdateHandler updateHandler, Environment environment) {
this.updateHandler = updateHandler;
this.nacosConfig = new NacosConfig(environment);
+ this.environment = environment;
}
- public void refreshNacosConfig() {
- new ConfigRefresh().refreshConfig();
+ public void refreshNacosConfig() throws NacosException {
+ Properties properties = nacosProperties(environment, nacosConfig);
+
+ ConfigService configService = NacosFactory.createConfigService(properties);
+ addApplicationConfig(configService);
+ addServiceConfig(configService);
+ addVersionConfig(configService);
+ addProfileConfig(configService);
+ addCustomConfig(configService);
+
+ refreshConfigItems();
}
- class ConfigRefresh {
- Parser contentParser = Parser.findParser(nacosConfig.getContentType());
+ private void addApplicationConfig(ConfigService configService) throws
NacosException {
+ String content =
configService.getConfig(BootStrapProperties.readApplication(environment),
+ BootStrapProperties.readApplication(environment), 5000);
+ processApplicationConfig(content);
+ configService.addListener(BootStrapProperties.readApplication(environment),
+ BootStrapProperties.readApplication(environment), new Listener() {
+ @Override
+ public void receiveConfigInfo(String configInfo) {
+ processApplicationConfig(configInfo);
+ refreshConfigItems();
+ }
- String keyPrefix = nacosConfig.getGroup() + "." +
- nacosConfig.getDataId();
+ @Override
+ public Executor getExecutor() {
+ return null;
+ }
+ });
+ }
- ConfigRefresh() {
+ private void processApplicationConfig(String content) {
+ if (StringUtils.isEmpty(content)) {
+ this.application = new HashMap<>();
+ return;
}
+ Parser contentParser = Parser.findParser("yaml");
+ this.application = contentParser.parse(content, "", false);
+ }
+
+ private void addServiceConfig(ConfigService configService) throws
NacosException {
+ String content =
configService.getConfig(BootStrapProperties.readServiceName(environment),
+ BootStrapProperties.readApplication(environment),
+ 5000);
+ processServiceConfig(content);
+ configService.addListener(BootStrapProperties.readServiceName(environment),
+ BootStrapProperties.readApplication(environment), new Listener() {
+ @Override
+ public void receiveConfigInfo(String configInfo) {
+ processServiceConfig(configInfo);
+ refreshConfigItems();
+ }
- @SuppressWarnings("unchecked")
- void refreshConfig() {
- Properties properties = new Properties();
- properties.put("serverAddr", nacosConfig.getServerAddr());
- properties.put("namespace", nacosConfig.getNameSpace());
-
- try {
- ConfigService configService =
NacosFactory.createConfigService(properties);
- String content = configService.getConfig(nacosConfig.getDataId(),
- nacosConfig.getGroup(), 5000);
- processContent(content);
- configService.addListener(nacosConfig.getDataId(),
- nacosConfig.getGroup(), new Listener() {
- @Override
- public void receiveConfigInfo(String configInfo) {
- LOGGER.info("receive from nacos:" + configInfo);
- processContent(configInfo);
- }
-
- @Override
- public Executor getExecutor() {
- return null;
- }
- });
- } catch (Exception e) {
- LOGGER.error("Receive nacos config error: ", e);
- }
+ @Override
+ public Executor getExecutor() {
+ return null;
+ }
+ });
+ }
+
+ private void processServiceConfig(String content) {
+ if (StringUtils.isEmpty(content)) {
+ this.service = new HashMap<>();
+ return;
}
+ Parser contentParser = Parser.findParser("yaml");
+ this.service = contentParser.parse(content, "", false);
+ }
- private void processContent(String content) {
- if (StringUtils.isEmpty(content)) {
- return;
- }
+ private void addVersionConfig(ConfigService configService) throws
NacosException {
+ String content = configService.getConfig(
+ BootStrapProperties.readServiceName(environment) + "-" +
+ BootStrapProperties.readServiceVersion(environment),
+ BootStrapProperties.readApplication(environment),
+ 5000);
+ processVersionConfig(content);
+ configService.addListener(BootStrapProperties.readServiceName(environment)
+ "-" +
+ BootStrapProperties.readServiceVersion(environment),
+ BootStrapProperties.readApplication(environment), new Listener() {
+ @Override
+ public void receiveConfigInfo(String configInfo) {
+ processVersionConfig(configInfo);
+ refreshConfigItems();
+ }
+
+ @Override
+ public Executor getExecutor() {
+ return null;
+ }
+ });
+ }
- refreshConfigItems(contentParser.parse(content, keyPrefix,
nacosConfig.getAddPrefix()));
+ private void processVersionConfig(String content) {
+ if (StringUtils.isEmpty(content)) {
+ this.version = new HashMap<>();
+ return;
}
+ Parser contentParser = Parser.findParser("yaml");
+ this.version = contentParser.parse(content, "", false);
+ }
+
+ private void addProfileConfig(ConfigService configService) throws
NacosException {
+ String profile = environment.getProperty("spring.profiles.active");
+ if (StringUtils.isEmpty(profile)) {
+ return;
+ }
+ String content =
configService.getConfig(BootStrapProperties.readServiceName(environment) + "-"
+ profile,
+ BootStrapProperties.readApplication(environment), 5000);
+ processProfileConfig(content);
+ configService.addListener(BootStrapProperties.readServiceName(environment)
+ "-" + profile,
+ BootStrapProperties.readApplication(environment), new Listener() {
+ @Override
+ public void receiveConfigInfo(String configInfo) {
+ processProfileConfig(configInfo);
+ refreshConfigItems();
+ }
+
+ @Override
+ public Executor getExecutor() {
+ return null;
+ }
+ });
+ }
+
+ private void processProfileConfig(String content) {
+ if (StringUtils.isEmpty(content)) {
+ this.profile = new HashMap<>();
+ return;
+ }
+ Parser contentParser = Parser.findParser("yaml");
+ this.profile = contentParser.parse(content, "", false);
+ }
+
+ private void addCustomConfig(ConfigService configService) throws
NacosException {
+ if (StringUtils.isEmpty(nacosConfig.getDataId()) ||
StringUtils.isEmpty(nacosConfig.getGroup())) {
+ return;
+ }
+ String content = configService.getConfig(nacosConfig.getDataId(),
+ nacosConfig.getGroup(), 5000);
+ processCustomConfig(content);
+ configService.addListener(nacosConfig.getDataId(),
+ nacosConfig.getGroup(), new Listener() {
+ @Override
+ public void receiveConfigInfo(String configInfo) {
+ processCustomConfig(configInfo);
+ refreshConfigItems();
+ }
+
+ @Override
+ public Executor getExecutor() {
+ return null;
+ }
+ });
+ }
- private void refreshConfigItems(Map<String, Object> map) {
- compareChangedConfig(originalConfigMap, map);
- originalConfigMap.clear();
- originalConfigMap.putAll(map);
+ private void processCustomConfig(String content) {
+ if (StringUtils.isEmpty(content)) {
+ this.custom = new HashMap<>();
+ return;
}
+ Parser contentParser = Parser.findParser(nacosConfig.getContentType());
+ String keyPrefix = nacosConfig.getGroup() + "." +
+ nacosConfig.getDataId();
+ this.custom = contentParser.parse(content, keyPrefix,
nacosConfig.getAddPrefix());
+ }
- void compareChangedConfig(Map<String, Object> before, Map<String, Object>
after) {
- Map<String, Object> itemsCreated = new HashMap<>();
- Map<String, Object> itemsDeleted = new HashMap<>();
- Map<String, Object> itemsModified = new HashMap<>();
- if (before == null || before.isEmpty()) {
- updateHandler.handle(CREATE, after);
- return;
- }
- if (after == null || after.isEmpty()) {
- updateHandler.handle(DELETE, before);
- return;
- }
- after.forEach((itemKey, itemValue) -> {
- if (!before.containsKey(itemKey)) {
- itemsCreated.put(itemKey, itemValue);
- } else if (!itemValue.equals(before.get(itemKey))) {
- itemsModified.put(itemKey, itemValue);
- }
- });
- for (String itemKey : before.keySet()) {
- if (!after.containsKey(itemKey)) {
- itemsDeleted.put(itemKey, "");
- }
- }
- updateHandler.handle(CREATE, itemsCreated);
- updateHandler.handle(SET, itemsModified);
- updateHandler.handle(DELETE, itemsDeleted);
+ private void refreshConfigItems() {
+ synchronized (lock) {
+ Map<String, Object> all = new HashMap<>();
+ all.putAll(application);
+ all.putAll(service);
+ all.putAll(version);
+ all.putAll(profile);
+ all.putAll(custom);
+ updateHandler.handle(all, allLast);
+ this.allLast = all;
+ }
+ }
+
+ private static Properties nacosProperties(Environment environment,
NacosConfig nacosConfig) {
+ Properties properties = new Properties();
+ properties.put(NacosConfig.PROP_NAMESPACE,
BootStrapProperties.readServiceEnvironment(environment));
+ properties.put(NacosConfig.PROP_ADDRESS, nacosConfig.getServerAddr());
+ if (nacosConfig.getUsername() != null) {
+ properties.put(NacosConfig.PROP_USERNAME, nacosConfig.getUsername());
+ }
+ if (nacosConfig.getPassword() != null) {
+ properties.put(NacosConfig.PROP_PASSWORD, nacosConfig.getPassword());
+ }
+ if (nacosConfig.getAccessKey() != null) {
+ properties.put(NacosConfig.PROP_ACCESS_KEY, nacosConfig.getAccessKey());
+ }
+ if (nacosConfig.getSecretKey() != null) {
+ properties.put(NacosConfig.PROP_SECRET_KEY, nacosConfig.getSecretKey());
}
+ return properties;
}
}
diff --git
a/dynamic-config/config-nacos/src/main/java/org/apache/servicecomb/config/nacos/NacosConfig.java
b/dynamic-config/config-nacos/src/main/java/org/apache/servicecomb/config/nacos/NacosConfig.java
index 3e6a40e55..8963bd75c 100644
---
a/dynamic-config/config-nacos/src/main/java/org/apache/servicecomb/config/nacos/NacosConfig.java
+++
b/dynamic-config/config-nacos/src/main/java/org/apache/servicecomb/config/nacos/NacosConfig.java
@@ -20,17 +20,35 @@ package org.apache.servicecomb.config.nacos;
import org.springframework.core.env.Environment;
public class NacosConfig {
- public static final String DATA_ID = "servicecomb.nacos.dataId";
+ public static final String PROPERTY_DATA_ID = "servicecomb.nacos.dataId";
- public static final String SERVER_ADDR = "servicecomb.nacos.serverAddr";
+ public static final String PROPERTY_SERVER_ADDR =
"servicecomb.nacos.serverAddr";
- public static final String GROUP = "servicecomb.nacos.group";
+ public static final String PROPERTY_GROUP = "servicecomb.nacos.group";
- public static final String ADD_PREFIX = "servicecomb.nacos.addPrefix";
+ public static final String PROPERTY_ADD_PREFIX =
"servicecomb.nacos.addPrefix";
- public static final String NAME_SPACE = "servicecomb.nacos.namespace";
+ public static final String PROPERTY_CONTENT_TYPE =
"servicecomb.nacos.contentType";
- public static final String CONTENT_TYPE = "servicecomb.nacos.contentType";
+ public static final String PROPERTY_USERNAME = "servicecomb.nacos.username";
+
+ public static final String PROPERTY_PASSWORD = "servicecomb.nacos.password";
+
+ public static final String PROPERTY_ACCESS_KEY =
"servicecomb.nacos.accessKey";
+
+ public static final String PROPERTY_SECRET_KEY =
"servicecomb.nacos.secretKey";
+
+ public static final String PROP_NAMESPACE = "namespace";
+
+ public static final String PROP_ADDRESS = "serverAddr";
+
+ public static final String PROP_USERNAME = "username";
+
+ public static final String PROP_PASSWORD = "password";
+
+ public static final String PROP_ACCESS_KEY = "accessKey";
+
+ public static final String PROP_SECRET_KEY = "secretKey";
private final Environment environment;
@@ -39,26 +57,38 @@ public class NacosConfig {
}
public String getServerAddr() {
- return environment.getProperty(SERVER_ADDR);
+ return environment.getProperty(PROPERTY_SERVER_ADDR,
"http://127.0.0.1:8848");
}
public String getDataId() {
- return environment.getProperty(DATA_ID);
+ return environment.getProperty(PROPERTY_DATA_ID);
}
public String getGroup() {
- return environment.getProperty(GROUP);
+ return environment.getProperty(PROPERTY_GROUP);
+ }
+
+ public String getUsername() {
+ return environment.getProperty(PROPERTY_USERNAME);
+ }
+
+ public String getPassword() {
+ return environment.getProperty(PROPERTY_PASSWORD);
+ }
+
+ public String getAccessKey() {
+ return environment.getProperty(PROPERTY_ACCESS_KEY);
}
- public String getNameSpace() {
- return environment.getProperty(NAME_SPACE, "public");
+ public String getSecretKey() {
+ return environment.getProperty(PROPERTY_SECRET_KEY);
}
public String getContentType() {
- return environment.getProperty(CONTENT_TYPE, "yaml");
+ return environment.getProperty(PROPERTY_CONTENT_TYPE, "yaml");
}
public boolean getAddPrefix() {
- return environment.getProperty(ADD_PREFIX, boolean.class, true);
+ return environment.getProperty(PROPERTY_ADD_PREFIX, boolean.class, false);
}
}
diff --git
a/dynamic-config/config-nacos/src/main/java/org/apache/servicecomb/config/nacos/NacosConfigConfiguration.java
b/dynamic-config/config-nacos/src/main/java/org/apache/servicecomb/config/nacos/NacosConfigConfiguration.java
deleted file mode 100644
index 969d621b1..000000000
---
a/dynamic-config/config-nacos/src/main/java/org/apache/servicecomb/config/nacos/NacosConfigConfiguration.java
+++ /dev/null
@@ -1,21 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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 org.apache.servicecomb.config.nacos;
-
-public class NacosConfigConfiguration {
-
-}
diff --git
a/dynamic-config/config-nacos/src/main/java/org/apache/servicecomb/config/nacos/NacosDynamicPropertiesSource.java
b/dynamic-config/config-nacos/src/main/java/org/apache/servicecomb/config/nacos/NacosDynamicPropertiesSource.java
index a533d22bd..f170408d4 100644
---
a/dynamic-config/config-nacos/src/main/java/org/apache/servicecomb/config/nacos/NacosDynamicPropertiesSource.java
+++
b/dynamic-config/config-nacos/src/main/java/org/apache/servicecomb/config/nacos/NacosDynamicPropertiesSource.java
@@ -16,22 +16,17 @@
*/
package org.apache.servicecomb.config.nacos;
-import static org.apache.servicecomb.config.nacos.ConfigurationAction.CREATE;
-import static org.apache.servicecomb.config.nacos.ConfigurationAction.DELETE;
-import static org.apache.servicecomb.config.nacos.ConfigurationAction.SET;
-
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
-import org.apache.servicecomb.config.ConfigMapping;
+import org.apache.servicecomb.config.ConfigurationChangedEvent;
import org.apache.servicecomb.config.DynamicPropertiesSource;
+import org.apache.servicecomb.foundation.common.event.EventManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MapPropertySource;
-import com.google.common.annotations.VisibleForTesting;
-
public class NacosDynamicPropertiesSource implements DynamicPropertiesSource {
public static final String SOURCE_NAME = "nacos";
@@ -44,34 +39,23 @@ public class NacosDynamicPropertiesSource implements
DynamicPropertiesSource {
private final UpdateHandler updateHandler = new UpdateHandler();
- @VisibleForTesting
- UpdateHandler getUpdateHandler() {
- return updateHandler;
- }
-
-
private void init(Environment environment) {
NacosClient nacosClient = new NacosClient(updateHandler, environment);
- nacosClient.refreshNacosConfig();
+ try {
+ nacosClient.refreshNacosConfig();
+ } catch (Exception e) {
+ throw new IllegalStateException("Set up nacos config failed.", e);
+ }
}
public class UpdateHandler {
- public void handle(ConfigurationAction action, Map<String, Object> config)
{
- if (config == null || config.isEmpty()) {
- return;
- }
- Map<String, Object> configuration =
ConfigMapping.getConvertedMap(config);
- if (CREATE.equals(action)) {
- valueCache.putAll(configuration);
- } else if (SET.equals(action)) {
- valueCache.putAll(configuration);
- } else if (DELETE.equals(action)) {
- configuration.keySet().forEach(valueCache::remove);
- } else {
- LOGGER.error("action: {} is invalid.", action.name());
- return;
- }
- LOGGER.warn("Config value cache changed: action:{}; item:{}",
action.name(), configuration.keySet());
+ public void handle(Map<String, Object> current, Map<String, Object> last) {
+ ConfigurationChangedEvent event =
ConfigurationChangedEvent.createIncremental(current, last);
+ LOGGER.info("Dynamic configuration changed: {}", event.getChanged());
+ valueCache.putAll(event.getAdded());
+ valueCache.putAll(event.getUpdated());
+ event.getDeleted().forEach((k, v) -> valueCache.remove(k));
+ EventManager.post(event);
}
}
diff --git
a/dynamic-config/config-nacos/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
b/dynamic-config/config-nacos/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
deleted file mode 100644
index 3f7419678..000000000
---
a/dynamic-config/config-nacos/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
+++ /dev/null
@@ -1,18 +0,0 @@
-## ---------------------------------------------------------------------------
-## Licensed to the Apache Software Foundation (ASF) under one or more
-## contributor license agreements. See the NOTICE file distributed with
-## this work for additional information regarding copyright ownership.
-## The ASF licenses this file to You 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.
-## ---------------------------------------------------------------------------
-
-org.apache.servicecomb.config.nacos.NacosConfigConfiguration
diff --git
a/dynamic-config/config-nacos/src/test/java/org/apache/servicecomb/config/nacos/NacosClientTest.java
b/dynamic-config/config-nacos/src/test/java/org/apache/servicecomb/config/nacos/NacosClientTest.java
deleted file mode 100644
index 718c45d22..000000000
---
a/dynamic-config/config-nacos/src/test/java/org/apache/servicecomb/config/nacos/NacosClientTest.java
+++ /dev/null
@@ -1,80 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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 org.apache.servicecomb.config.nacos;
-
-import java.util.HashMap;
-import java.util.Map;
-
-import
org.apache.servicecomb.config.nacos.NacosDynamicPropertiesSource.UpdateHandler;
-import org.junit.jupiter.api.Assertions;
-import org.junit.jupiter.api.Test;
-import org.mockito.Mockito;
-import org.springframework.core.env.Environment;
-
-public class NacosClientTest {
-
- @Test
- public void testCompareChangedConfig() {
- boolean status = true;
- Map<String, Object> before = new HashMap<>();
- Map<String, Object> after = new HashMap<>();
-
- Environment environment = Mockito.mock(Environment.class);
- Mockito.when(environment.getProperty(NacosConfig.CONTENT_TYPE,
"yaml")).thenReturn("yaml");
- NacosDynamicPropertiesSource impl = new NacosDynamicPropertiesSource();
- UpdateHandler updateHandler = impl.new UpdateHandler();
- NacosClient nacosClient = new NacosClient(updateHandler, environment);
- NacosConfig nacosConfig = Mockito.mock(NacosConfig.class);
- Mockito.when(nacosConfig.getContentType()).thenReturn("yaml");
-
- NacosClient.ConfigRefresh cr = nacosClient.new ConfigRefresh();
-
- try {
- cr.compareChangedConfig(before, after);
- } catch (Exception e) {
- status = false;
- }
- Assertions.assertTrue(status);
-
- status = true;
- before.put("test", "testValue");
- try {
- cr.compareChangedConfig(before, after);
- } catch (Exception e) {
- status = false;
- }
- Assertions.assertTrue(status);
-
- status = true;
- after.put("test", "testValue2");
- try {
- cr.compareChangedConfig(before, after);
- } catch (Exception e) {
- status = false;
- }
- Assertions.assertTrue(status);
-
- status = true;
- try {
- cr.compareChangedConfig(before, after);
- } catch (Exception e) {
- status = false;
- }
- Assertions.assertTrue(status);
- }
-}
diff --git
a/service-registry/registry-nacos/src/main/java/org/apache/servicecomb/registry/nacos/NacosDiscoveryProperties.java
b/service-registry/registry-nacos/src/main/java/org/apache/servicecomb/registry/nacos/NacosDiscoveryProperties.java
index 5d27b7ec0..6651f1e63 100644
---
a/service-registry/registry-nacos/src/main/java/org/apache/servicecomb/registry/nacos/NacosDiscoveryProperties.java
+++
b/service-registry/registry-nacos/src/main/java/org/apache/servicecomb/registry/nacos/NacosDiscoveryProperties.java
@@ -23,7 +23,7 @@ import java.util.Map;
public class NacosDiscoveryProperties {
private boolean enabled = true;
- private String serverAddr;
+ private String serverAddr = "http://127.0.0.1:8848";
private Map<String, String> metadata = new HashMap<>();
diff --git
a/service-registry/registry-nacos/src/main/java/org/apache/servicecomb/registry/nacos/NacosRegistration.java
b/service-registry/registry-nacos/src/main/java/org/apache/servicecomb/registry/nacos/NacosRegistration.java
index 97d016a97..f9bb35843 100644
---
a/service-registry/registry-nacos/src/main/java/org/apache/servicecomb/registry/nacos/NacosRegistration.java
+++
b/service-registry/registry-nacos/src/main/java/org/apache/servicecomb/registry/nacos/NacosRegistration.java
@@ -64,8 +64,7 @@ public class NacosRegistration implements
Registration<NacosRegistrationInstance
instance =
NacosMicroserviceHandler.createMicroserviceInstance(dataCenterProperties,
nacosDiscoveryProperties,
environment);
instance.setInstanceId(instanceId);
- nacosRegistrationInstance = new NacosRegistrationInstance(instance,
nacosDiscoveryProperties,
- environment);
+ nacosRegistrationInstance = new NacosRegistrationInstance(instance,
environment);
namingService = NamingServiceManager.buildNamingService(environment,
nacosDiscoveryProperties);
}
diff --git
a/service-registry/registry-nacos/src/main/java/org/apache/servicecomb/registry/nacos/NacosRegistrationInstance.java
b/service-registry/registry-nacos/src/main/java/org/apache/servicecomb/registry/nacos/NacosRegistrationInstance.java
index 9b5cd3a16..653ce3d89 100644
---
a/service-registry/registry-nacos/src/main/java/org/apache/servicecomb/registry/nacos/NacosRegistrationInstance.java
+++
b/service-registry/registry-nacos/src/main/java/org/apache/servicecomb/registry/nacos/NacosRegistrationInstance.java
@@ -33,18 +33,15 @@ import com.alibaba.nacos.api.naming.pojo.Instance;
public class NacosRegistrationInstance implements RegistrationInstance {
private final Instance instance;
- private final NacosDiscoveryProperties nacosDiscoveryProperties;
-
private final Map<String, String> schemas = new HashMap<>();
private final List<String> endpoints = new ArrayList<>();
private final Environment environment;
- public NacosRegistrationInstance(Instance instance, NacosDiscoveryProperties
nacosDiscoveryProperties,
+ public NacosRegistrationInstance(Instance instance,
Environment environment) {
this.instance = instance;
- this.nacosDiscoveryProperties = nacosDiscoveryProperties;
this.environment = environment;
}
@@ -70,14 +67,14 @@ public class NacosRegistrationInstance implements
RegistrationInstance {
@Override
public String getVersion() {
- return instance.getMetadata().get("version");
+ return instance.getMetadata().get(NacosConst.PROPERTY_VERSION);
}
@Override
public DataCenterInfo getDataCenterInfo() {
DataCenterInfo dataCenterInfo = new DataCenterInfo();
- dataCenterInfo.setRegion(instance.getMetadata().get("region"));
- dataCenterInfo.setAvailableZone(instance.getMetadata().get("zone"));
+
dataCenterInfo.setRegion(instance.getMetadata().get(NacosConst.PROPERTY_REGION));
+
dataCenterInfo.setAvailableZone(instance.getMetadata().get(NacosConst.PROPERTY_ZONE));
return dataCenterInfo;
}
diff --git
a/service-registry/registry-nacos/src/main/java/org/apache/servicecomb/registry/nacos/NamingServiceManager.java
b/service-registry/registry-nacos/src/main/java/org/apache/servicecomb/registry/nacos/NamingServiceManager.java
index 67f5ee9e9..635a7c485 100644
---
a/service-registry/registry-nacos/src/main/java/org/apache/servicecomb/registry/nacos/NamingServiceManager.java
+++
b/service-registry/registry-nacos/src/main/java/org/apache/servicecomb/registry/nacos/NamingServiceManager.java
@@ -50,11 +50,22 @@ public class NamingServiceManager {
Properties properties = new Properties();
properties.put(NacosConst.NAMESPACE,
BootStrapProperties.readServiceEnvironment(environment));
properties.put(NacosConst.SERVER_ADDR,
nacosDiscoveryProperties.getServerAddr());
- properties.put(NacosConst.USERNAME,
Objects.toString(nacosDiscoveryProperties.getUsername(), ""));
- properties.put(NacosConst.PASSWORD,
Objects.toString(nacosDiscoveryProperties.getPassword(), ""));
- properties.put(NacosConst.NACOS_NAMING_LOG_NAME,
Objects.toString(nacosDiscoveryProperties.getLogName(), ""));
- properties.put(NacosConst.ACCESS_KEY,
Objects.toString(nacosDiscoveryProperties.getAccessKey(), ""));
- properties.put(NacosConst.SECRET_KEY,
Objects.toString(nacosDiscoveryProperties.getSecretKey(), ""));
+ if (nacosDiscoveryProperties.getUsername() != null) {
+ properties.put(NacosConst.USERNAME,
nacosDiscoveryProperties.getUsername());
+ }
+ if (nacosDiscoveryProperties.getPassword() != null) {
+ properties.put(NacosConst.PASSWORD,
nacosDiscoveryProperties.getPassword());
+ }
+ if (nacosDiscoveryProperties.getAccessKey() != null) {
+ properties.put(NacosConst.ACCESS_KEY,
nacosDiscoveryProperties.getAccessKey());
+ }
+ if (nacosDiscoveryProperties.getSecretKey() != null) {
+ properties.put(NacosConst.SECRET_KEY,
nacosDiscoveryProperties.getSecretKey());
+ }
+ if (nacosDiscoveryProperties.getLogName() != null) {
+ properties.put(NacosConst.NACOS_NAMING_LOG_NAME,
nacosDiscoveryProperties.getLogName());
+ }
+
properties.put(NacosConst.CLUSTER_NAME,
nacosDiscoveryProperties.getClusterName());
properties.put(NacosConst.NAMING_LOAD_CACHE_AT_START,
nacosDiscoveryProperties.getNamingLoadCacheAtStart());
return properties;