From c2973a8ea9585119b4e0da3e1d9142673f2d71d3 Mon Sep 17 00:00:00 2001 From: wenxuan70 Date: Thu, 26 Oct 2023 16:14:09 +0800 Subject: [PATCH] fix: set nacos group and namespace for serverConnectorConfig metadata --- .../extend/nacos/NacosConfigModifier.java | 16 ++++++++++++++++ .../extend/nacos/NacosContextPropertiesTest.java | 2 ++ 2 files changed, 18 insertions(+) diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/extend/nacos/NacosConfigModifier.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/extend/nacos/NacosConfigModifier.java index 671a4fc37b..ff189195db 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/extend/nacos/NacosConfigModifier.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/extend/nacos/NacosConfigModifier.java @@ -54,6 +54,14 @@ public class NacosConfigModifier implements PolarisConfigModifier { * nacos contextPath. */ public static final String CONTEXT_PATH = "contextPath"; + /** + * nacos namespace. + */ + public static final String NAMESPACE = "namespace"; + /** + * nacos group. + */ + public static final String GROUP = "group"; private static final Logger LOGGER = LoggerFactory.getLogger(NacosConfigModifier.class); private static final String ID = "nacos"; private final NacosContextProperties nacosContextProperties; @@ -114,6 +122,14 @@ public void modify(ConfigurationImpl configuration) { metadata.put(CONTEXT_PATH, nacosContextProperties.getContextPath()); } + if (StringUtils.isNotBlank(nacosContextProperties.getNamespace())) { + metadata.put(NAMESPACE, nacosContextProperties.getNamespace()); + } + + if (StringUtils.isNotBlank(nacosContextProperties.getGroup())) { + metadata.put(GROUP, nacosContextProperties.getGroup()); + } + configuration.getGlobal().getServerConnectors().add(serverConnectorConfig); DiscoveryConfigImpl discoveryConfig = new DiscoveryConfigImpl(); discoveryConfig.setServerConnectorId(ID); diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/extend/nacos/NacosContextPropertiesTest.java b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/extend/nacos/NacosContextPropertiesTest.java index acdf55d581..0d5d1a2b24 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/extend/nacos/NacosContextPropertiesTest.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/extend/nacos/NacosContextPropertiesTest.java @@ -84,6 +84,8 @@ public void testModify() { assertThat(metadata.get(NacosConfigModifier.USERNAME)).isEqualTo(nacosContextProperties.getUsername()); assertThat(metadata.get(NacosConfigModifier.PASSWORD)).isEqualTo(nacosContextProperties.getPassword()); assertThat(metadata.get(NacosConfigModifier.CONTEXT_PATH)).isEqualTo(nacosContextProperties.getContextPath()); + assertThat(metadata.get(NacosConfigModifier.NAMESPACE)).isEqualTo(nacosContextProperties.getNamespace()); + assertThat(metadata.get(NacosConfigModifier.GROUP)).isEqualTo(nacosContextProperties.getGroup()); } @SpringBootApplication