Feature:support multi register and discovery both to nacos and polaris (#693)

* feature:multi register support nacos.

* feature:multi register support nacos.

* feature:multi register support nacos.

* feature:multi register support nacos.

* feature:multi register support nacos.

* feature:multi register support nacos.

Co-authored-by: wulingxiao <1251605638@qqcom>
Co-authored-by: Haotian Zhang <928016560@qq.com>
pull/718/head
lingxiao,wu 2 years ago committed by GitHub
parent ad44516d30
commit e56bb13f0d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -5,6 +5,7 @@
- [Optimize/optimize annotation.](https://github.com/Tencent/spring-cloud-tencent/pull/672) - [Optimize/optimize annotation.](https://github.com/Tencent/spring-cloud-tencent/pull/672)
- [Optimize: Register the service with the ProviderAPI#registerInstance method.](https://github.com/Tencent/spring-cloud-tencent/pull/686) - [Optimize: Register the service with the ProviderAPI#registerInstance method.](https://github.com/Tencent/spring-cloud-tencent/pull/686)
- [docs:update PR template.](https://github.com/Tencent/spring-cloud-tencent/pull/690) - [docs:update PR template.](https://github.com/Tencent/spring-cloud-tencent/pull/690)
- [Feature:support multi register and discovery both to nacos and polaris.](https://github.com/Tencent/spring-cloud-tencent/pull/693)
- [Code optimize & add junit tests.](https://github.com/Tencent/spring-cloud-tencent/pull/701) - [Code optimize & add junit tests.](https://github.com/Tencent/spring-cloud-tencent/pull/701)
- [Optimize: remote deprecated method](https://github.com/Tencent/spring-cloud-tencent/pull/697) - [Optimize: remote deprecated method](https://github.com/Tencent/spring-cloud-tencent/pull/697)
- [Test:support environment variable metadata test](https://github.com/Tencent/spring-cloud-tencent/pull/698) - [Test:support environment variable metadata test](https://github.com/Tencent/spring-cloud-tencent/pull/698)

@ -21,6 +21,8 @@ import com.tencent.cloud.polaris.context.ConditionalOnPolarisEnabled;
import com.tencent.cloud.polaris.discovery.PolarisDiscoveryHandler; import com.tencent.cloud.polaris.discovery.PolarisDiscoveryHandler;
import com.tencent.cloud.polaris.extend.consul.ConsulConfigModifier; import com.tencent.cloud.polaris.extend.consul.ConsulConfigModifier;
import com.tencent.cloud.polaris.extend.consul.ConsulContextProperties; 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.ConsumerAPI;
import com.tencent.polaris.api.core.ProviderAPI; import com.tencent.polaris.api.core.ProviderAPI;
import com.tencent.polaris.client.api.SDKContext; import com.tencent.polaris.client.api.SDKContext;
@ -38,7 +40,7 @@ import org.springframework.context.annotation.Import;
*/ */
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)
@ConditionalOnPolarisEnabled @ConditionalOnPolarisEnabled
@Import({PolarisDiscoveryProperties.class, ConsulContextProperties.class}) @Import({PolarisDiscoveryProperties.class, ConsulContextProperties.class, NacosContextProperties.class})
public class DiscoveryPropertiesAutoConfiguration { public class DiscoveryPropertiesAutoConfiguration {
@Bean @Bean
@ -66,4 +68,10 @@ public class DiscoveryPropertiesAutoConfiguration {
public PolarisDiscoveryConfigModifier polarisDiscoveryConfigModifier(PolarisDiscoveryProperties polarisDiscoveryProperties) { public PolarisDiscoveryConfigModifier polarisDiscoveryConfigModifier(PolarisDiscoveryProperties polarisDiscoveryProperties) {
return new PolarisDiscoveryConfigModifier(polarisDiscoveryProperties); return new PolarisDiscoveryConfigModifier(polarisDiscoveryProperties);
} }
@Bean
@ConditionalOnMissingBean
public NacosConfigModifier nacosConfigModifier(@Autowired(required = false) NacosContextProperties nacosContextProperties) {
return new NacosConfigModifier(nacosContextProperties);
}
} }

@ -0,0 +1,114 @@
/*
* 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<String, String> 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;
}
}

@ -0,0 +1,172 @@
/*
* 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 + '\'' +
'}';
}
}

@ -21,10 +21,12 @@ package com.tencent.cloud.polaris.registry;
import java.net.URI; import java.net.URI;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import com.tencent.cloud.common.metadata.StaticMetadataManager; import com.tencent.cloud.common.metadata.StaticMetadataManager;
import com.tencent.cloud.polaris.PolarisDiscoveryProperties; import com.tencent.cloud.polaris.PolarisDiscoveryProperties;
import com.tencent.cloud.polaris.extend.consul.ConsulContextProperties; import com.tencent.cloud.polaris.extend.consul.ConsulContextProperties;
import com.tencent.cloud.polaris.extend.nacos.NacosContextProperties;
import com.tencent.polaris.client.api.SDKContext; import com.tencent.polaris.client.api.SDKContext;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
@ -33,6 +35,9 @@ import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import org.springframework.util.CollectionUtils; 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. * Registration object of Polaris.
* *
@ -42,6 +47,8 @@ public class PolarisRegistration implements Registration {
private static final String METADATA_KEY_IP = "internal-ip"; private static final String METADATA_KEY_IP = "internal-ip";
private static final String METADATA_KEY_ADDRESS = "internal-address"; 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 PolarisDiscoveryProperties polarisDiscoveryProperties;
@ -51,6 +58,8 @@ public class PolarisRegistration implements Registration {
private final StaticMetadataManager staticMetadataManager; private final StaticMetadataManager staticMetadataManager;
private final NacosContextProperties nacosContextProperties;
private Map<String, String> metadata; private Map<String, String> metadata;
private String host; private String host;
@ -60,19 +69,31 @@ public class PolarisRegistration implements Registration {
public PolarisRegistration( public PolarisRegistration(
PolarisDiscoveryProperties polarisDiscoveryProperties, PolarisDiscoveryProperties polarisDiscoveryProperties,
@Nullable ConsulContextProperties consulContextProperties, @Nullable ConsulContextProperties consulContextProperties,
SDKContext context, StaticMetadataManager staticMetadataManager) { SDKContext context, StaticMetadataManager staticMetadataManager,
@Nullable NacosContextProperties nacosContextProperties) {
this.polarisDiscoveryProperties = polarisDiscoveryProperties; this.polarisDiscoveryProperties = polarisDiscoveryProperties;
this.consulContextProperties = consulContextProperties; this.consulContextProperties = consulContextProperties;
this.polarisContext = context; this.polarisContext = context;
this.staticMetadataManager = staticMetadataManager; this.staticMetadataManager = staticMetadataManager;
this.nacosContextProperties = nacosContextProperties;
host = polarisContext.getConfig().getGlobal().getAPI().getBindIP(); host = polarisContext.getConfig().getGlobal().getAPI().getBindIP();
} }
@Override @Override
public String getServiceId() { public String getServiceId() {
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(); return polarisDiscoveryProperties.getService();
} }
}
}
@Override @Override
public String getHost() { public String getHost() {
@ -111,6 +132,12 @@ public class PolarisRegistration implements Registration {
instanceMetadata.put(METADATA_KEY_IP, host); instanceMetadata.put(METADATA_KEY_IP, host);
instanceMetadata.put(METADATA_KEY_ADDRESS, host + ":" + polarisDiscoveryProperties.getPort()); 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()); instanceMetadata.putAll(staticMetadataManager.getMergedStaticMetadata());
this.metadata = instanceMetadata; this.metadata = instanceMetadata;
@ -136,12 +163,14 @@ public class PolarisRegistration implements Registration {
boolean registerEnabled = false; boolean registerEnabled = false;
if (null != polarisDiscoveryProperties) { if (null != polarisDiscoveryProperties) {
registerEnabled |= polarisDiscoveryProperties.isRegisterEnabled(); registerEnabled = polarisDiscoveryProperties.isRegisterEnabled();
} }
if (null != consulContextProperties && consulContextProperties.isEnabled()) { if (null != consulContextProperties && consulContextProperties.isEnabled()) {
registerEnabled |= consulContextProperties.isRegister(); registerEnabled |= consulContextProperties.isRegister();
} }
if (null != nacosContextProperties && nacosContextProperties.isEnabled()) {
registerEnabled |= nacosContextProperties.isRegisterEnabled();
}
return registerEnabled; return registerEnabled;
} }

@ -23,6 +23,7 @@ import com.tencent.cloud.polaris.PolarisDiscoveryProperties;
import com.tencent.cloud.polaris.discovery.PolarisDiscoveryAutoConfiguration; import com.tencent.cloud.polaris.discovery.PolarisDiscoveryAutoConfiguration;
import com.tencent.cloud.polaris.discovery.PolarisDiscoveryHandler; import com.tencent.cloud.polaris.discovery.PolarisDiscoveryHandler;
import com.tencent.cloud.polaris.extend.consul.ConsulContextProperties; import com.tencent.cloud.polaris.extend.consul.ConsulContextProperties;
import com.tencent.cloud.polaris.extend.nacos.NacosContextProperties;
import com.tencent.polaris.client.api.SDKContext; import com.tencent.polaris.client.api.SDKContext;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -61,8 +62,8 @@ public class PolarisServiceRegistryAutoConfiguration {
public PolarisRegistration polarisRegistration( public PolarisRegistration polarisRegistration(
PolarisDiscoveryProperties polarisDiscoveryProperties, PolarisDiscoveryProperties polarisDiscoveryProperties,
@Autowired(required = false) ConsulContextProperties consulContextProperties, @Autowired(required = false) ConsulContextProperties consulContextProperties,
SDKContext context, StaticMetadataManager staticMetadataManager) { SDKContext context, StaticMetadataManager staticMetadataManager, NacosContextProperties nacosContextProperties) {
return new PolarisRegistration(polarisDiscoveryProperties, consulContextProperties, context, staticMetadataManager); return new PolarisRegistration(polarisDiscoveryProperties, consulContextProperties, context, staticMetadataManager, nacosContextProperties);
} }
@Bean @Bean

@ -65,6 +65,50 @@
"type": "java.lang.Long", "type": "java.lang.Long",
"defaultValue": 60000, "defaultValue": 60000,
"description": "Millis interval of refresh of service info list. Default: 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"
} }
] ]
} }

@ -0,0 +1,91 @@
/*
* 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<ServerConnectorConfigImpl> serverConnectorConfigs = configuration.getGlobal().getServerConnectors();
Optional<ServerConnectorConfigImpl> 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<String, String> 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 {
}
}

@ -23,6 +23,7 @@ import java.util.Map;
import com.tencent.cloud.common.metadata.StaticMetadataManager; import com.tencent.cloud.common.metadata.StaticMetadataManager;
import com.tencent.cloud.polaris.PolarisDiscoveryProperties; import com.tencent.cloud.polaris.PolarisDiscoveryProperties;
import com.tencent.cloud.polaris.extend.consul.ConsulContextProperties; 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.Configuration;
import com.tencent.polaris.api.config.global.APIConfig; import com.tencent.polaris.api.config.global.APIConfig;
import com.tencent.polaris.api.config.global.GlobalConfig; 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.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/** /**
* Test for {@link PolarisRegistration}. * Test for {@link PolarisRegistration}.
@ -49,6 +51,8 @@ public class PolarisRegistrationTest {
private PolarisRegistration polarisRegistration; private PolarisRegistration polarisRegistration;
NacosContextProperties nacosContextProperties = mock(NacosContextProperties.class);
@Before @Before
public void setUp() { public void setUp() {
// mock PolarisDiscoveryProperties // mock PolarisDiscoveryProperties
@ -76,7 +80,7 @@ public class PolarisRegistrationTest {
doReturn(Collections.singletonMap("key1", "value1")).when(staticMetadataManager).getMergedStaticMetadata(); doReturn(Collections.singletonMap("key1", "value1")).when(staticMetadataManager).getMergedStaticMetadata();
polarisRegistration = new PolarisRegistration(polarisDiscoveryProperties, consulContextProperties, polarisRegistration = new PolarisRegistration(polarisDiscoveryProperties, consulContextProperties,
polarisContext, staticMetadataManager); polarisContext, staticMetadataManager, nacosContextProperties);
} }
@Test @Test
@ -127,4 +131,24 @@ public class PolarisRegistrationTest {
public void testToString() { public void testToString() {
System.out.println(polarisRegistration); 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<String, String> metadata = polarisRegistration.getMetadata();
assertThat(metadata).isNotNull();
assertThat(metadata).isNotEmpty();
assertThat(metadata.size()).isEqualTo(4);
assertThat(metadata.get("nacos.cluster")).isEqualTo(clusterName);
}
} }

@ -22,3 +22,14 @@ spring:
service-name: ${spring.application.name} service-name: ${spring.application.name}
ip-address: 127.0.0.1 ip-address: 127.0.0.1
prefer-ip-address: true 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

@ -41,6 +41,17 @@ spring:
# service-name: ${spring.application.name} # service-name: ${spring.application.name}
# ip-address: localhost # ip-address: localhost
# prefer-ip-address: true # 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: #eureka:
# client: # client:
# serviceUrl: # serviceUrl:

@ -69,6 +69,11 @@
<artifactId>connector-consul</artifactId> <artifactId>connector-consul</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.tencent.polaris</groupId>
<artifactId>connector-nacos</artifactId>
</dependency>
<dependency> <dependency>
<groupId>com.tencent.polaris</groupId> <groupId>com.tencent.polaris</groupId>
<artifactId>connector-composite</artifactId> <artifactId>connector-composite</artifactId>

Loading…
Cancel
Save