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
parent
ad44516d30
commit
e56bb13f0d
@ -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 + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
@ -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 {
|
||||
}
|
||||
}
|
Loading…
Reference in new issue