From 5f4c7e9f70db77d2865bfed5d57fe8c80fb5f580 Mon Sep 17 00:00:00 2001 From: lepdou Date: Fri, 9 Dec 2022 17:32:39 +0800 Subject: [PATCH] support nacos registry (#758) Co-authored-by: Haotian Zhang <928016560@qq.com> --- CHANGELOG.md | 1 + .../DiscoveryPropertiesAutoConfiguration.java | 14 +- .../discovery/DiscoveryEnabledCondition.java | 6 +- .../extend/nacos/NacosConfigModifier.java | 113 ++++++++++++ .../extend/nacos/NacosContextProperties.java | 171 ++++++++++++++++++ .../polaris/registry/PolarisRegistration.java | 42 ++++- ...larisServiceRegistryAutoConfiguration.java | 6 +- ...itional-spring-configuration-metadata.json | 44 +++++ .../nacos/NacosContextPropertiesTest.java | 90 +++++++++ .../registry/PolarisRegistrationTest.java | 26 ++- .../src/test/resources/application-test.yml | 11 ++ .../src/main/resources/bootstrap.yml | 11 ++ spring-cloud-tencent-polaris-context/pom.xml | 5 + 13 files changed, 525 insertions(+), 15 deletions(-) create mode 100644 spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/extend/nacos/NacosConfigModifier.java create mode 100644 spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/extend/nacos/NacosContextProperties.java create mode 100644 spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/extend/nacos/NacosContextPropertiesTest.java diff --git a/CHANGELOG.md b/CHANGELOG.md index f65c7c9c6..7d61f2a1d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,4 +15,5 @@ - [adapt polaris-java 1.10.1 version](https://github.com/Tencent/spring-cloud-tencent/pull/747) - [Optimize: change RouteArgument.buildCustom to RouteArgument.fromLabel](https://github.com/Tencent/spring-cloud-tencent/pull/751) - [Optimize: support delete config for ConfigurationProperties bean ](https://github.com/Tencent/spring-cloud-tencent/pull/756) +- [Feature: support nacos registry](https://github.com/Tencent/spring-cloud-tencent/pull/758) - [Test:add sct-stater-polaris-router junit.](https://github.com/Tencent/spring-cloud-tencent/pull/768) diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/DiscoveryPropertiesAutoConfiguration.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/DiscoveryPropertiesAutoConfiguration.java index c98f64dfb..258ba7799 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/DiscoveryPropertiesAutoConfiguration.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/DiscoveryPropertiesAutoConfiguration.java @@ -21,6 +21,8 @@ import com.tencent.cloud.polaris.context.ConditionalOnPolarisEnabled; import com.tencent.cloud.polaris.discovery.PolarisDiscoveryHandler; import com.tencent.cloud.polaris.extend.consul.ConsulConfigModifier; import com.tencent.cloud.polaris.extend.consul.ConsulContextProperties; +import com.tencent.cloud.polaris.extend.nacos.NacosConfigModifier; +import com.tencent.cloud.polaris.extend.nacos.NacosContextProperties; import com.tencent.polaris.api.core.ConsumerAPI; import com.tencent.polaris.api.core.ProviderAPI; import com.tencent.polaris.client.api.SDKContext; @@ -38,14 +40,14 @@ import org.springframework.context.annotation.Import; */ @Configuration(proxyBeanMethods = false) @ConditionalOnPolarisEnabled -@Import({PolarisDiscoveryProperties.class, ConsulContextProperties.class}) +@Import({PolarisDiscoveryProperties.class, ConsulContextProperties.class, NacosContextProperties.class}) public class DiscoveryPropertiesAutoConfiguration { @Bean @ConditionalOnMissingBean public PolarisDiscoveryHandler polarisDiscoveryHandler(PolarisDiscoveryProperties polarisDiscoveryProperties, - ProviderAPI providerAPI, SDKContext sdkContext, - ConsumerAPI polarisConsumer) { + ProviderAPI providerAPI, SDKContext sdkContext, + ConsumerAPI polarisConsumer) { return new PolarisDiscoveryHandler(polarisDiscoveryProperties, providerAPI, sdkContext, polarisConsumer); } @@ -61,6 +63,12 @@ public class DiscoveryPropertiesAutoConfiguration { return new ConsulConfigModifier(consulContextProperties); } + @Bean + @ConditionalOnMissingBean + public NacosConfigModifier nacosConfigModifier(@Autowired(required = false) NacosContextProperties nacosContextProperties) { + return new NacosConfigModifier(nacosContextProperties); + } + @Bean @ConditionalOnMissingBean public PolarisDiscoveryConfigModifier polarisDiscoveryConfigModifier(PolarisDiscoveryProperties polarisDiscoveryProperties) { diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/DiscoveryEnabledCondition.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/DiscoveryEnabledCondition.java index 1963537c2..f63959187 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/DiscoveryEnabledCondition.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/DiscoveryEnabledCondition.java @@ -39,7 +39,9 @@ public class DiscoveryEnabledCondition implements Condition { && Boolean.parseBoolean(conditionContext.getEnvironment() .getProperty("spring.cloud.consul.discovery.enabled", "true")); - isDiscoveryEnabled |= isConsulDiscoveryEnabled; - return isDiscoveryEnabled; + boolean isNacosEnabled = Boolean.parseBoolean( + conditionContext.getEnvironment().getProperty("spring.cloud.nacos.discovery.enabled", "false")); + + return isDiscoveryEnabled || isConsulDiscoveryEnabled || isNacosEnabled; } } 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 new file mode 100644 index 000000000..570f44779 --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/extend/nacos/NacosConfigModifier.java @@ -0,0 +1,113 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * 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 com.tencent.cloud.polaris.extend.nacos; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Map; +import java.util.Objects; + +import com.tencent.cloud.common.constant.ContextConstant; +import com.tencent.cloud.polaris.context.PolarisConfigModifier; +import com.tencent.polaris.api.config.plugin.DefaultPlugins; +import com.tencent.polaris.factory.config.ConfigurationImpl; +import com.tencent.polaris.factory.config.consumer.DiscoveryConfigImpl; +import com.tencent.polaris.factory.config.global.ServerConnectorConfigImpl; +import com.tencent.polaris.factory.config.provider.RegisterConfigImpl; +import org.apache.commons.lang.StringUtils; + +import org.springframework.util.CollectionUtils; + +/** + * {@link PolarisConfigModifier} impl of Nacos. + * + * @author lingxiao.wlx + */ +public class NacosConfigModifier implements PolarisConfigModifier { + private static final String ID = "nacos"; + /** + * nacos username. + */ + public static final String USERNAME = "username"; + /** + * nacos password. + */ + public static final String PASSWORD = "password"; + /** + * nacos contextPath. + */ + public static final String CONTEXT_PATH = "contextPath"; + + private final NacosContextProperties nacosContextProperties; + + public NacosConfigModifier(NacosContextProperties nacosContextProperties) { + this.nacosContextProperties = nacosContextProperties; + } + + @Override + public void modify(ConfigurationImpl configuration) { + if (Objects.isNull(nacosContextProperties) || !nacosContextProperties.isEnabled()) { + return; + } + if (CollectionUtils.isEmpty(configuration.getGlobal().getServerConnectors())) { + configuration.getGlobal().setServerConnectors(new ArrayList<>()); + } + if (CollectionUtils.isEmpty(configuration.getGlobal().getServerConnectors()) + && null != configuration.getGlobal().getServerConnector()) { + configuration.getGlobal().getServerConnectors().add(configuration.getGlobal().getServerConnector()); + } + ServerConnectorConfigImpl serverConnectorConfig = new ServerConnectorConfigImpl(); + serverConnectorConfig.setId(ID); + if (StringUtils.isBlank(nacosContextProperties.getServerAddr())) { + throw new IllegalArgumentException("nacos server addr must not be empty, please set it by" + + "spring.cloud.nacos.discovery.server-addr"); + } + serverConnectorConfig.setAddresses( + Collections.singletonList(nacosContextProperties.getServerAddr())); + serverConnectorConfig.setProtocol(DefaultPlugins.SERVER_CONNECTOR_NACOS); + + Map metadata = serverConnectorConfig.getMetadata(); + if (StringUtils.isNotBlank(nacosContextProperties.getUsername())) { + metadata.put(USERNAME, nacosContextProperties.getUsername()); + } + + if (StringUtils.isNotBlank(nacosContextProperties.getPassword())) { + metadata.put(PASSWORD, nacosContextProperties.getPassword()); + } + + if (StringUtils.isNotBlank(nacosContextProperties.getContextPath())) { + metadata.put(CONTEXT_PATH, nacosContextProperties.getContextPath()); + } + + configuration.getGlobal().getServerConnectors().add(serverConnectorConfig); + DiscoveryConfigImpl discoveryConfig = new DiscoveryConfigImpl(); + discoveryConfig.setServerConnectorId(ID); + discoveryConfig.setEnable(nacosContextProperties.isDiscoveryEnabled()); + configuration.getConsumer().getDiscoveries().add(discoveryConfig); + + RegisterConfigImpl registerConfig = new RegisterConfigImpl(); + registerConfig.setServerConnectorId(ID); + registerConfig.setEnable(nacosContextProperties.isRegisterEnabled()); + configuration.getProvider().getRegisters().add(registerConfig); + } + + @Override + public int getOrder() { + return ContextConstant.ModifierOrder.LAST; + } +} 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 new file mode 100644 index 000000000..48d007ef0 --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/extend/nacos/NacosContextProperties.java @@ -0,0 +1,171 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * 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 com.tencent.cloud.polaris.extend.nacos; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.context.properties.ConfigurationProperties; + +/** + * Discovery configuration of Nacos. + * + * @author lingxiao.wlx + */ +@ConfigurationProperties("spring.cloud.nacos") +public class NacosContextProperties { + + /** + * Nacos default group name. + */ + public static final String DEFAULT_GROUP = "DEFAULT_GROUP"; + + /** + * Nacos default cluster name. + */ + public static final String DEFAULT_CLUSTER = "DEFAULT"; + + private boolean enabled = false; + + @Value("${spring.cloud.nacos.discovery.enabled:#{'true'}}") + private boolean discoveryEnabled; + + /** + * if you just want to subscribe on nacos , but don't want to register your service, set it to + * false. + */ + @Value("${spring.cloud.nacos.discovery.register-enabled:#{'true'}}") + private boolean registerEnabled; + + /** + * nacos discovery server address. + */ + @Value("${spring.cloud.nacos.discovery.server-addr:}") + private String serverAddr; + + /** + * the nacos authentication username. + */ + @Value("${spring.cloud.nacos.discovery.username:}") + private String username; + + /** + * the nacos authentication password. + */ + @Value("${spring.cloud.nacos.discovery.password:}") + private String password; + + /** + * cluster name for nacos . + */ + @Value("${spring.cloud.nacos.discovery.cluster-name:DEFAULT}") + private String clusterName = DEFAULT_CLUSTER; + + /** + * group name for nacos. + */ + @Value("${spring.cloud.nacos.discovery.group:DEFAULT_GROUP}") + private String group = DEFAULT_GROUP; + + private String contextPath; + + public boolean isEnabled() { + return enabled; + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } + + public boolean isRegisterEnabled() { + return registerEnabled; + } + + public void setRegisterEnabled(boolean registerEnabled) { + this.registerEnabled = registerEnabled; + } + + public boolean isDiscoveryEnabled() { + return discoveryEnabled; + } + + public void setDiscoveryEnabled(boolean discoveryEnabled) { + this.discoveryEnabled = discoveryEnabled; + } + + public String getServerAddr() { + return serverAddr; + } + + public void setServerAddr(String serverAddr) { + this.serverAddr = serverAddr; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getClusterName() { + return clusterName; + } + + public void setClusterName(String clusterName) { + this.clusterName = clusterName; + } + + public String getGroup() { + return group; + } + + public void setGroup(String group) { + this.group = group; + } + + public String getContextPath() { + return contextPath; + } + + public void setContextPath(String contextPath) { + this.contextPath = contextPath; + } + + @Override + public String toString() { + return "NacosContextProperties{" + + "enabled=" + enabled + + ", discoveryEnabled=" + discoveryEnabled + + ", registerEnabled=" + registerEnabled + + ", serverAddr='" + serverAddr + '\'' + + ", username='" + username + '\'' + + ", password='" + password + '\'' + + ", clusterName='" + clusterName + '\'' + + ", group='" + group + '\'' + + ", contextPath='" + contextPath + '\'' + + '}'; + } +} diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisRegistration.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisRegistration.java index be7a3ce03..49516fa14 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisRegistration.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisRegistration.java @@ -21,10 +21,12 @@ package com.tencent.cloud.polaris.registry; import java.net.URI; import java.util.HashMap; import java.util.Map; +import java.util.Objects; import com.tencent.cloud.common.metadata.StaticMetadataManager; import com.tencent.cloud.polaris.PolarisDiscoveryProperties; import com.tencent.cloud.polaris.extend.consul.ConsulContextProperties; +import com.tencent.cloud.polaris.extend.nacos.NacosContextProperties; import com.tencent.polaris.client.api.SDKContext; import org.apache.commons.lang.StringUtils; @@ -33,6 +35,8 @@ import org.springframework.cloud.client.serviceregistry.Registration; import org.springframework.lang.Nullable; import org.springframework.util.CollectionUtils; +import static com.tencent.cloud.polaris.extend.nacos.NacosContextProperties.DEFAULT_CLUSTER; +import static com.tencent.cloud.polaris.extend.nacos.NacosContextProperties.DEFAULT_GROUP; /** * Registration object of Polaris. * @@ -40,14 +44,17 @@ import org.springframework.util.CollectionUtils; */ public class PolarisRegistration implements Registration { - private final static String METADATA_KEY_IP = "internal-ip"; - - private final static String METADATA_KEY_ADDRESS = "internal-address"; + private static final String METADATA_KEY_IP = "internal-ip"; + private static final String METADATA_KEY_ADDRESS = "internal-address"; + private static final String GROUP_SERVER_ID_FORMAT = "%s__%s"; + private static final String NACOS_CLUSTER = "nacos.cluster"; private final PolarisDiscoveryProperties polarisDiscoveryProperties; private final ConsulContextProperties consulContextProperties; + private final NacosContextProperties nacosContextProperties; + private final SDKContext polarisContext; private final StaticMetadataManager staticMetadataManager; @@ -61,9 +68,11 @@ public class PolarisRegistration implements Registration { public PolarisRegistration( PolarisDiscoveryProperties polarisDiscoveryProperties, @Nullable ConsulContextProperties consulContextProperties, + @Nullable NacosContextProperties nacosContextProperties, SDKContext context, StaticMetadataManager staticMetadataManager) { this.polarisDiscoveryProperties = polarisDiscoveryProperties; this.consulContextProperties = consulContextProperties; + this.nacosContextProperties = nacosContextProperties; this.polarisContext = context; this.staticMetadataManager = staticMetadataManager; @@ -72,7 +81,18 @@ public class PolarisRegistration implements Registration { @Override public String getServiceId() { - return polarisDiscoveryProperties.getService(); + if (Objects.isNull(nacosContextProperties)) { + return polarisDiscoveryProperties.getService(); + } + else { + String group = nacosContextProperties.getGroup(); + if (StringUtils.isNotBlank(group) && !DEFAULT_GROUP.equals(group)) { + return String.format(GROUP_SERVER_ID_FORMAT, group, polarisDiscoveryProperties.getService()); + } + else { + return polarisDiscoveryProperties.getService(); + } + } } @Override @@ -112,6 +132,12 @@ public class PolarisRegistration implements Registration { instanceMetadata.put(METADATA_KEY_IP, host); instanceMetadata.put(METADATA_KEY_ADDRESS, host + ":" + polarisDiscoveryProperties.getPort()); + // put internal-nacos-cluster if necessary + String clusterName = nacosContextProperties.getClusterName(); + if (StringUtils.isNotBlank(clusterName) && !DEFAULT_CLUSTER.equals(clusterName)) { + instanceMetadata.put(NACOS_CLUSTER, clusterName); + } + instanceMetadata.putAll(staticMetadataManager.getMergedStaticMetadata()); this.metadata = instanceMetadata; @@ -137,19 +163,21 @@ public class PolarisRegistration implements Registration { boolean registerEnabled = false; if (null != polarisDiscoveryProperties) { - registerEnabled |= polarisDiscoveryProperties.isRegisterEnabled(); + registerEnabled = polarisDiscoveryProperties.isRegisterEnabled(); } if (null != consulContextProperties && consulContextProperties.isEnabled()) { registerEnabled |= consulContextProperties.isRegister(); } - + if (null != nacosContextProperties && nacosContextProperties.isEnabled()) { + registerEnabled |= nacosContextProperties.isRegisterEnabled(); + } return registerEnabled; } @Override public String toString() { return "PolarisRegistration{" + - "polarisDiscoveryProperties=" + polarisDiscoveryProperties + + " polarisDiscoveryProperties=" + polarisDiscoveryProperties + ", polarisContext=" + polarisContext + ", staticMetadataManager=" + staticMetadataManager + ", metadata=" + metadata + diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistryAutoConfiguration.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistryAutoConfiguration.java index 6533edf93..edd3844e6 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistryAutoConfiguration.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistryAutoConfiguration.java @@ -23,6 +23,7 @@ import com.tencent.cloud.polaris.PolarisDiscoveryProperties; import com.tencent.cloud.polaris.discovery.PolarisDiscoveryAutoConfiguration; import com.tencent.cloud.polaris.discovery.PolarisDiscoveryHandler; import com.tencent.cloud.polaris.extend.consul.ConsulContextProperties; +import com.tencent.cloud.polaris.extend.nacos.NacosContextProperties; import com.tencent.polaris.client.api.SDKContext; import org.springframework.beans.factory.annotation.Autowired; @@ -62,8 +63,9 @@ public class PolarisServiceRegistryAutoConfiguration { public PolarisRegistration polarisRegistration( PolarisDiscoveryProperties polarisDiscoveryProperties, @Autowired(required = false) ConsulContextProperties consulContextProperties, - SDKContext context, StaticMetadataManager staticMetadataManager) { - return new PolarisRegistration(polarisDiscoveryProperties, consulContextProperties, context, staticMetadataManager); + SDKContext context, StaticMetadataManager staticMetadataManager, NacosContextProperties nacosContextProperties) { + return new PolarisRegistration(polarisDiscoveryProperties, consulContextProperties, nacosContextProperties, context, + staticMetadataManager); } @Bean 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 115585d91..12a535855 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 @@ -65,6 +65,50 @@ "type": "java.lang.Long", "defaultValue": 60000, "description": "Millis interval of refresh of service info list. Default: 60000." + }, + { + "name": "spring.cloud.nacos.discovery.enabled", + "type": "java.lang.Boolean", + "sourceType": "com.tencent.cloud.polaris.extend.nacos.NacosContextProperties", + "defaultValue": false + }, + { + "name": "spring.cloud.nacos.discovery.group", + "type": "java.lang.String", + "description": "group name for nacos.", + "sourceType": "com.tencent.cloud.polaris.extend.nacos.NacosContextProperties", + "defaultValue": "DEFAULT_GROUP" + }, + { + "name": "spring.cloud.nacos.discovery.password", + "type": "java.lang.String", + "description": "the nacos authentication password.", + "sourceType": "com.tencent.cloud.polaris.extend.nacos.NacosContextProperties" + }, + { + "name": "spring.cloud.nacos.discovery.register-enabled", + "type": "java.lang.Boolean", + "description": "if you just want to subscribe on nacos , but don't want to register your service, set it to false.", + "sourceType": "com.tencent.cloud.polaris.extend.nacos.NacosContextProperties", + "defaultValue": false + }, + { + "name": "spring.cloud.nacos.discovery.server-addr", + "type": "java.lang.String", + "description": "nacos discovery server address.", + "sourceType": "com.tencent.cloud.polaris.extend.nacos.NacosContextProperties" + }, + { + "name": "spring.cloud.nacos.discovery.username", + "type": "java.lang.String", + "description": "the nacos authentication username.", + "sourceType": "com.tencent.cloud.polaris.extend.nacos.NacosContextProperties" + }, + { + "name": "spring.cloud.nacos.discovery.cluster-name", + "type": "java.lang.String", + "description": "the nacos authentication cluster-name.", + "sourceType": "com.tencent.cloud.polaris.extend.nacos.NacosContextProperties" } ] } 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 new file mode 100644 index 000000000..c1bae3abc --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/extend/nacos/NacosContextPropertiesTest.java @@ -0,0 +1,90 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * 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 com.tencent.cloud.polaris.extend.nacos; + +import java.util.List; +import java.util.Map; +import java.util.Optional; + +import com.tencent.polaris.api.config.plugin.DefaultPlugins; +import com.tencent.polaris.client.api.SDKContext; +import com.tencent.polaris.factory.config.global.ServerConnectorConfigImpl; +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.util.CollectionUtils; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Test for {@link NacosContextProperties}. + * + * @author lingxiao.wlx + */ +@RunWith(SpringRunner.class) +@SpringBootTest(classes = NacosContextPropertiesTest.TestApplication.class) +@ActiveProfiles("test") +public class NacosContextPropertiesTest { + + @Autowired + private NacosContextProperties nacosContextProperties; + + @Autowired + private SDKContext sdkContext; + + @Test + public void testDefaultInitialization() { + assertThat(nacosContextProperties).isNotNull(); + assertThat(nacosContextProperties.isEnabled()).isTrue(); + assertThat(nacosContextProperties.getServerAddr()).isEqualTo("127.0.0.1:8848"); + assertThat(nacosContextProperties.isRegisterEnabled()).isTrue(); + assertThat(nacosContextProperties.isDiscoveryEnabled()).isTrue(); + assertThat(nacosContextProperties.getGroup()).isNotBlank(); + assertThat(nacosContextProperties.getClusterName()).isNotBlank(); + } + + @Test + public void testModify() { + assertThat(sdkContext).isNotNull(); + com.tencent.polaris.api.config.Configuration configuration = sdkContext.getConfig(); + List serverConnectorConfigs = configuration.getGlobal().getServerConnectors(); + Optional optionalServerConnectorConfig = serverConnectorConfigs.stream().filter( + item -> "nacos".equals(item.getId()) + ).findAny(); + assertThat(optionalServerConnectorConfig.isPresent()).isTrue(); + ServerConnectorConfigImpl serverConnectorConfig = optionalServerConnectorConfig.get(); + if (!CollectionUtils.isEmpty(serverConnectorConfig.getAddresses())) { + assertThat(nacosContextProperties.getServerAddr().equals(serverConnectorConfig.getAddresses().get(0))).isTrue(); + } + assertThat(DefaultPlugins.SERVER_CONNECTOR_NACOS.equals(serverConnectorConfig.getProtocol())).isTrue(); + + Map metadata = serverConnectorConfig.getMetadata(); + 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()); + } + + @SpringBootApplication + protected static class TestApplication { + } +} diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/registry/PolarisRegistrationTest.java b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/registry/PolarisRegistrationTest.java index 420f61569..1b21783b0 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/registry/PolarisRegistrationTest.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/registry/PolarisRegistrationTest.java @@ -23,6 +23,7 @@ import java.util.Map; import com.tencent.cloud.common.metadata.StaticMetadataManager; import com.tencent.cloud.polaris.PolarisDiscoveryProperties; import com.tencent.cloud.polaris.extend.consul.ConsulContextProperties; +import com.tencent.cloud.polaris.extend.nacos.NacosContextProperties; import com.tencent.polaris.api.config.Configuration; import com.tencent.polaris.api.config.global.APIConfig; import com.tencent.polaris.api.config.global.GlobalConfig; @@ -38,6 +39,7 @@ import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; /** * Test for {@link PolarisRegistration}. @@ -49,6 +51,8 @@ public class PolarisRegistrationTest { private PolarisRegistration polarisRegistration; + NacosContextProperties nacosContextProperties = mock(NacosContextProperties.class); + @Before public void setUp() { // mock PolarisDiscoveryProperties @@ -76,7 +80,7 @@ public class PolarisRegistrationTest { doReturn(Collections.singletonMap("key1", "value1")).when(staticMetadataManager).getMergedStaticMetadata(); polarisRegistration = new PolarisRegistration(polarisDiscoveryProperties, consulContextProperties, - polarisContext, staticMetadataManager); + nacosContextProperties, polarisContext, staticMetadataManager); } @Test @@ -127,4 +131,24 @@ public class PolarisRegistrationTest { public void testToString() { System.out.println(polarisRegistration); } + + @Test + public void testGetNacosServiceId() { + String groupName = "group"; + String format = "%s__%s"; + when(nacosContextProperties.getGroup()).thenReturn(groupName); + String serviceId = polarisRegistration.getServiceId(); + assertThat(String.format(format, groupName, SERVICE_PROVIDER).equals(serviceId)); + } + + @Test + public void testGetNacosMetadata() { + String clusterName = "cluster"; + when(nacosContextProperties.getClusterName()).thenReturn(clusterName); + Map metadata = polarisRegistration.getMetadata(); + assertThat(metadata).isNotNull(); + assertThat(metadata).isNotEmpty(); + assertThat(metadata.size()).isEqualTo(4); + assertThat(metadata.get("nacos.cluster")).isEqualTo(clusterName); + } } diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/test/resources/application-test.yml b/spring-cloud-starter-tencent-polaris-discovery/src/test/resources/application-test.yml index 8fbdbcc48..52752ef59 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/test/resources/application-test.yml +++ b/spring-cloud-starter-tencent-polaris-discovery/src/test/resources/application-test.yml @@ -22,3 +22,14 @@ spring: service-name: ${spring.application.name} ip-address: 127.0.0.1 prefer-ip-address: true + nacos: + enabled: true + context-path: /nacos + discovery: + enabled: true + register-enabled: true + group: polaris + server-addr: 127.0.0.1:8848 + username: nacos + password: nacos + cluster-name: polaris diff --git a/spring-cloud-tencent-examples/polaris-discovery-example/discovery-caller-service/src/main/resources/bootstrap.yml b/spring-cloud-tencent-examples/polaris-discovery-example/discovery-caller-service/src/main/resources/bootstrap.yml index c1fed673f..ff7d0e346 100644 --- a/spring-cloud-tencent-examples/polaris-discovery-example/discovery-caller-service/src/main/resources/bootstrap.yml +++ b/spring-cloud-tencent-examples/polaris-discovery-example/discovery-caller-service/src/main/resources/bootstrap.yml @@ -38,6 +38,17 @@ spring: # service-name: ${spring.application.name} # ip-address: localhost # prefer-ip-address: true +# nacos: +# enabled: true +# context-path: /nacos +# discovery: +# enabled: true +# register-enabled: true +# group: polaris +# server-addr: 127.0.0.1:8848 +# username: nacos +# password: nacos +# cluster-name: polaris #eureka: # client: # serviceUrl: diff --git a/spring-cloud-tencent-polaris-context/pom.xml b/spring-cloud-tencent-polaris-context/pom.xml index a1be890e5..4670df8f5 100644 --- a/spring-cloud-tencent-polaris-context/pom.xml +++ b/spring-cloud-tencent-polaris-context/pom.xml @@ -77,6 +77,11 @@ connector-consul + + com.tencent.polaris + connector-nacos + + com.tencent.polaris connector-composite