From 1b5424bbdca846e2268f02e8de005b77c93a7188 Mon Sep 17 00:00:00 2001 From: wenxuan70 Date: Fri, 27 Oct 2023 19:46:24 +0800 Subject: [PATCH] feat: support nacos namespace mapping (#1192) --- CHANGELOG.md | 1 + .../extend/nacos/NacosConfigModifier.java | 16 ++++++++++++ .../extend/nacos/NacosContextProperties.java | 17 ++++++++++++ ...itional-spring-configuration-metadata.json | 9 ++++++- .../nacos/NacosContextPropertiesTest.java | 3 +++ .../common/metadata/MetadataContext.java | 2 +- .../cloud/common/util/DiscoveryUtil.java | 26 +++++++++++++++++++ 7 files changed, 72 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1d412e2b4..f583bce90 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,3 +17,4 @@ - [fix:remove bcprov-jdk15on dependency.](https://github.com/Tencent/spring-cloud-tencent/pull/1179) - [feat:support configuration encryption.](https://github.com/Tencent/spring-cloud-tencent/pull/1183) - [feat:optimize examples.](https://github.com/Tencent/spring-cloud-tencent/pull/1187) +- [feat: support nacos namespace mapping](https://github.com/Tencent/spring-cloud-tencent/pull/1192) 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 671a4fc37..ff189195d 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 class NacosConfigModifier implements PolarisConfigModifier { 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/main/java/com/tencent/cloud/polaris/extend/nacos/NacosContextProperties.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/extend/nacos/NacosContextProperties.java index 48d007ef0..b468ef989 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/extend/nacos/NacosContextProperties.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/extend/nacos/NacosContextProperties.java @@ -38,6 +38,11 @@ public class NacosContextProperties { */ public static final String DEFAULT_CLUSTER = "DEFAULT"; + /** + * Nacos default namespace name. + */ + public static final String DEFAULT_NAMESPACE = "public"; + private boolean enabled = false; @Value("${spring.cloud.nacos.discovery.enabled:#{'true'}}") @@ -80,6 +85,9 @@ public class NacosContextProperties { @Value("${spring.cloud.nacos.discovery.group:DEFAULT_GROUP}") private String group = DEFAULT_GROUP; + @Value("${spring.cloud.nacos.discovery.namespace:public}") + private String namespace = DEFAULT_NAMESPACE; + private String contextPath; public boolean isEnabled() { @@ -154,6 +162,14 @@ public class NacosContextProperties { this.contextPath = contextPath; } + public String getNamespace() { + return namespace; + } + + public void setNamespace(String namespace) { + this.namespace = namespace; + } + @Override public String toString() { return "NacosContextProperties{" + @@ -166,6 +182,7 @@ public class NacosContextProperties { ", clusterName='" + clusterName + '\'' + ", group='" + group + '\'' + ", contextPath='" + contextPath + '\'' + + ", namespace='" + namespace + '\'' + '}'; } } diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-cloud-starter-tencent-polaris-discovery/src/main/resources/META-INF/additional-spring-configuration-metadata.json index a16b6b8e4..8e5b628af 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -91,6 +91,13 @@ "sourceType": "com.tencent.cloud.polaris.extend.nacos.NacosContextProperties", "defaultValue": "DEFAULT_GROUP" }, + { + "name": "spring.cloud.nacos.discovery.namespace", + "type": "java.lang.String", + "description": "namespace id for nacos.", + "sourceType": "com.tencent.cloud.polaris.extend.nacos.NacosContextProperties", + "defaultValue": "public" + }, { "name": "spring.cloud.nacos.discovery.password", "type": "java.lang.String", @@ -123,7 +130,7 @@ "sourceType": "com.tencent.cloud.polaris.extend.nacos.NacosContextProperties" } ], - "hints" : [ + "hints": [ { "name": "spring.cloud.polaris.loadbalancer.strategy", "values": [ 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 acdf55d58..34cb28f81 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 @@ -61,6 +61,7 @@ public class NacosContextPropertiesTest { assertThat(nacosContextProperties.isDiscoveryEnabled()).isTrue(); assertThat(nacosContextProperties.getGroup()).isNotBlank(); assertThat(nacosContextProperties.getClusterName()).isNotBlank(); + assertThat(nacosContextProperties.getNamespace()).isNotBlank(); } @Test @@ -84,6 +85,8 @@ public class NacosContextPropertiesTest { 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 diff --git a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/MetadataContext.java b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/MetadataContext.java index 75481aa5a..8fcd89855 100644 --- a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/MetadataContext.java +++ b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/MetadataContext.java @@ -88,7 +88,7 @@ public class MetadataContext { throw new RuntimeException("namespace should not be blank. please configure spring.cloud.polaris.namespace or " + "spring.cloud.polaris.discovery.namespace"); } - + namespace = DiscoveryUtil.rewriteNamespace(namespace); LOCAL_NAMESPACE = namespace; String serviceName = ApplicationContextAwareUtils diff --git a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/DiscoveryUtil.java b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/DiscoveryUtil.java index e5e5237bc..1e14c6c9a 100644 --- a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/DiscoveryUtil.java +++ b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/DiscoveryUtil.java @@ -32,6 +32,8 @@ public final class DiscoveryUtil { private static String NACOS_GROUP; + private static String NACOS_NAMESPACE; + private static final Object MUTEX = new Object(); private static boolean INITIALIZE = false; @@ -62,6 +64,29 @@ public final class DiscoveryUtil { return serviceId; } + /** + * rewrite namespace when open double registry and discovery by nacos and polaris. + * + * @param namespace namespace + * @return new namespace + */ + public static String rewriteNamespace(String namespace) { + init(); + if (Boolean.parseBoolean(ENABLE_NACOS)) { + boolean rewrite = false; + if (Boolean.parseBoolean(ENABLE_NACOS_REGISTRY) && Boolean.parseBoolean(ENABLE_POLARIS_DISCOVERY)) { + rewrite = true; + } + if (Boolean.parseBoolean(ENABLE_NACOS_DISCOVERY) || Boolean.parseBoolean(ENABLE_POLARIS_DISCOVERY)) { + rewrite = true; + } + if (rewrite) { + namespace = NACOS_NAMESPACE; + } + } + return namespace; + } + private static void init() { if (INITIALIZE) { return; @@ -75,6 +100,7 @@ public final class DiscoveryUtil { ENABLE_NACOS_REGISTRY = ApplicationContextAwareUtils.getProperties("spring.cloud.nacos.discovery.register-enabled"); ENABLE_POLARIS_DISCOVERY = ApplicationContextAwareUtils.getProperties("spring.cloud.polaris.discovery.enabled"); NACOS_GROUP = ApplicationContextAwareUtils.getProperties("spring.cloud.nacos.discovery.group", "DEFAULT_GROUP"); + NACOS_NAMESPACE = ApplicationContextAwareUtils.getProperties("spring.cloud.nacos.discovery.namespace", "public"); INITIALIZE = true; } }