feature:multi register support nacos.

pull/693/head
wulingxiao 3 years ago
parent e1b577e30e
commit 42331ac626

@ -71,7 +71,7 @@ public class DiscoveryPropertiesAutoConfiguration {
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public NacosConfigModifier nacosConfigModifier(@Autowired(required = false) NacosContextProperties nacosContextProperties){ public NacosConfigModifier nacosConfigModifier(@Autowired(required = false) NacosContextProperties nacosContextProperties) {
return new NacosConfigModifier(nacosContextProperties); return new NacosConfigModifier(nacosContextProperties);
} }
} }

@ -18,6 +18,11 @@
package com.tencent.cloud.polaris.extend.nacos; 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.common.constant.ContextConstant;
import com.tencent.cloud.polaris.context.PolarisConfigModifier; import com.tencent.cloud.polaris.context.PolarisConfigModifier;
import com.tencent.polaris.api.config.plugin.DefaultPlugins; import com.tencent.polaris.api.config.plugin.DefaultPlugins;
@ -29,12 +34,9 @@ import org.apache.commons.lang.StringUtils;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Map;
import java.util.Objects;
/** /**
* {@link PolarisConfigModifier} impl of Nacos.
*
* @author lingxiao.wlx * @author lingxiao.wlx
*/ */
public class NacosConfigModifier implements PolarisConfigModifier { public class NacosConfigModifier implements PolarisConfigModifier {

@ -22,11 +22,16 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
/** /**
* Discovery configuration of Nacos.
*
* @author lingxiao.wlx * @author lingxiao.wlx
*/ */
@ConfigurationProperties("spring.cloud.nacos") @ConfigurationProperties("spring.cloud.nacos")
public class NacosContextProperties { public class NacosContextProperties {
/**
* Nacos default group name.
*/
public static final String DEFAULT_GROUP = "DEFAULT_GROUP"; public static final String DEFAULT_GROUP = "DEFAULT_GROUP";
private boolean enabled = false; private boolean enabled = false;
@ -38,13 +43,13 @@ public class NacosContextProperties {
* if you just want to subscribe on nacos , but don't want to register your service, set it to * if you just want to subscribe on nacos , but don't want to register your service, set it to
* false. * false.
*/ */
@Value("${spring.cloud.nacos.discovery.registerEnabled:#{'true'}}") @Value("${spring.cloud.nacos.discovery.register-enabled:#{'true'}}")
private boolean registerEnabled; private boolean registerEnabled;
/** /**
* nacos discovery server address. * nacos discovery server address.
*/ */
@Value("${spring.cloud.nacos.discovery.serverAddr:}") @Value("${spring.cloud.nacos.discovery.server-addr:}")
private String serverAddr; private String serverAddr;
/** /**

@ -46,6 +46,7 @@ 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 final PolarisDiscoveryProperties polarisDiscoveryProperties; private final PolarisDiscoveryProperties polarisDiscoveryProperties;
@ -84,7 +85,7 @@ public class PolarisRegistration implements Registration {
else { else {
String group = nacosContextProperties.getGroup(); String group = nacosContextProperties.getGroup();
if (StringUtils.isNotBlank(group) && !DEFAULT_GROUP.equals(group)) { if (StringUtils.isNotBlank(group) && !DEFAULT_GROUP.equals(group)) {
return group + "_" + polarisDiscoveryProperties.getService(); return String.format(GROUP_SERVER_ID_FORMAT, group, polarisDiscoveryProperties.getService());
} }
else { else {
return polarisDiscoveryProperties.getService(); return polarisDiscoveryProperties.getService();

@ -63,7 +63,7 @@ public class PolarisServiceRegistryAutoConfiguration {
PolarisDiscoveryProperties polarisDiscoveryProperties, PolarisDiscoveryProperties polarisDiscoveryProperties,
@Autowired(required = false) ConsulContextProperties consulContextProperties, @Autowired(required = false) ConsulContextProperties consulContextProperties,
SDKContext context, StaticMetadataManager staticMetadataManager, NacosContextProperties nacosContextProperties) { SDKContext context, StaticMetadataManager staticMetadataManager, NacosContextProperties nacosContextProperties) {
return new PolarisRegistration(polarisDiscoveryProperties, consulContextProperties, context, staticMetadataManager,nacosContextProperties); 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,83 @@
/*
* 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 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 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();
Map<String, String> metadata = null;
for (ServerConnectorConfigImpl serverConnectorConfig : serverConnectorConfigs) {
if (serverConnectorConfig.getId().equals("nacos")) {
metadata = serverConnectorConfig.getMetadata();
}
}
assertThat(metadata).isNotNull();
assertThat(metadata.get("internal-nacos-cluster")).isEqualTo("polaris");
}
@SpringBootApplication
protected static class TestApplication {
}
}

@ -39,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}.
@ -50,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
@ -61,7 +64,6 @@ public class PolarisRegistrationTest {
// mock // mock
ConsulContextProperties consulContextProperties = mock(ConsulContextProperties.class); ConsulContextProperties consulContextProperties = mock(ConsulContextProperties.class);
NacosContextProperties nacosContextProperties = mock(NacosContextProperties.class);
// mock SDKContext // mock SDKContext
APIConfig apiConfig = mock(APIConfig.class); APIConfig apiConfig = mock(APIConfig.class);
@ -129,4 +131,13 @@ 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("group");
String serviceId = polarisRegistration.getServiceId();
assertThat(String.format(format, groupName, SERVICE_PROVIDER).equals(serviceId));
}
} }

@ -22,3 +22,13 @@ 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
discovery:
enabled: true
register-enabled: true
group: polaris
server-addr: 127.0.0.1:8848
username: nacos
password: nacos
cluster-name: polaris

@ -45,9 +45,9 @@ spring:
# enabled: true # enabled: true
# discovery: # discovery:
# enabled: true # enabled: true
# registerEnabled: true # register-enabled: true
# group: polaris # group: polaris
# serverAddr: 127.0.0.1:8848 # server-addr: 127.0.0.1:8848
# username: nacos # username: nacos
# password: nacos # password: nacos
#eureka: #eureka:

Loading…
Cancel
Save