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

feat: support nacos namespace mapping #1122

Merged
merged 9 commits into from
Oct 26, 2023
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- [fix:fix feign url bug when using sleuth.](https://github.com/Tencent/spring-cloud-tencent/pull/1096)
- [fix: dynamic routing using cookies.](https://github.com/Tencent/spring-cloud-tencent/pull/1097)
- [Refactoring:remove invalid @AutoConfigureAfter and @AutoConfigureBefore from discovery client automatic configuration.](https://github.com/Tencent/spring-cloud-tencent/pull/1115)
- [feat: support nacos namespace mapping](https://github.com/Tencent/spring-cloud-tencent/pull/1122)
- [feat: support log path configuration parameters.](https://github.com/Tencent/spring-cloud-tencent/pull/1128)
- [refactor:optimize the order and condition matching of service registration automatic configuration.](https://github.com/Tencent/spring-cloud-tencent/pull/1129)
- [feat: add circuit breaker actuator.](https://github.com/Tencent/spring-cloud-tencent/pull/1136)
Expand All @@ -13,6 +14,7 @@
- [feat:add swagger exposure filters.](https://github.com/Tencent/spring-cloud-tencent/pull/1146)
- [feat:add swagger report switch.](https://github.com/Tencent/spring-cloud-tencent/pull/1148)
- [fix:fix retry loadbalancer not working bug.](https://github.com/Tencent/spring-cloud-tencent/pull/1154)
- [feat: support nacos namespace mapping](https://github.com/Tencent/spring-cloud-tencent/pull/1122)
SkyeBeFreeman marked this conversation as resolved.
Show resolved Hide resolved
- [fix:fix header validation when using Chinese char.](https://github.com/Tencent/spring-cloud-tencent/pull/1166)
- [fix:remove bcprov-jdk15on dependency.](https://github.com/Tencent/spring-cloud-tencent/pull/1177)
- [feat:support configuration encryption.](https://github.com/Tencent/spring-cloud-tencent/pull/1181)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,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;
Expand Down Expand Up @@ -115,6 +123,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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,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'}}")
Expand Down Expand Up @@ -81,6 +86,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() {
Expand Down Expand Up @@ -155,6 +163,14 @@ public void setContextPath(String contextPath) {
this.contextPath = contextPath;
}

public String getNamespace() {
return namespace;
}

public void setNamespace(String namespace) {
this.namespace = namespace;
}

@Override
public String toString() {
return "NacosContextProperties{" +
Expand All @@ -167,6 +183,7 @@ public String toString() {
", clusterName='" + clusterName + '\'' +
", group='" + group + '\'' +
", contextPath='" + contextPath + '\'' +
", namespace='" + namespace + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 for nacos.",
SkyeBeFreeman marked this conversation as resolved.
Show resolved Hide resolved
"sourceType": "com.tencent.cloud.polaris.extend.nacos.NacosContextProperties",
"defaultValue": "public"
},
{
"name": "spring.cloud.nacos.discovery.password",
"type": "java.lang.String",
Expand Down Expand Up @@ -123,7 +130,7 @@
"sourceType": "com.tencent.cloud.polaris.extend.nacos.NacosContextProperties"
}
],
"hints" : [
"hints": [
{
"name": "spring.cloud.polaris.loadbalancer.strategy",
"values": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public void testDefaultInitialization() {
assertThat(nacosContextProperties.isDiscoveryEnabled()).isTrue();
assertThat(nacosContextProperties.getGroup()).isNotBlank();
assertThat(nacosContextProperties.getClusterName()).isNotBlank();
assertThat(nacosContextProperties.getNamespace()).isNotBlank();
}

@Test
Expand All @@ -85,6 +86,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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -62,6 +64,29 @@ public static String rewriteServiceId(String serviceId) {
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;
Expand All @@ -75,6 +100,7 @@ private static void init() {
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;
}
}
Expand Down