parent
f36195109d
commit
e1b577e30e
@ -0,0 +1,93 @@
|
||||
/*
|
||||
* 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 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;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* @author lingxiao.wlx
|
||||
*/
|
||||
public class NacosConfigModifier implements PolarisConfigModifier {
|
||||
private static final String ID = "nacos";
|
||||
private static final String ADDRESS_FORMAT = "%s:%s@%s";
|
||||
private static final String INTERNAL_NACOS_CLUSTER = "internal-nacos-cluster";
|
||||
|
||||
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);
|
||||
// Nacos Address URI: nacos:nacos@127.0.0.1:8848
|
||||
String address = String.format(ADDRESS_FORMAT, nacosContextProperties.getUsername(), nacosContextProperties.getPassword(), nacosContextProperties.getServerAddr());
|
||||
serverConnectorConfig.setAddresses(
|
||||
Collections.singletonList(address));
|
||||
serverConnectorConfig.setProtocol(DefaultPlugins.SERVER_CONNECTOR_NACOS);
|
||||
Map<String, String> metadata = serverConnectorConfig.getMetadata();
|
||||
// set internal-nacos-cluster if necessary
|
||||
String clusterName = nacosContextProperties.getClusterName();
|
||||
if (StringUtils.isNotBlank(clusterName)) {
|
||||
metadata.put(INTERNAL_NACOS_CLUSTER, clusterName);
|
||||
}
|
||||
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,151 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* @author lingxiao.wlx
|
||||
*/
|
||||
@ConfigurationProperties("spring.cloud.nacos")
|
||||
public class NacosContextProperties {
|
||||
|
||||
public static final String DEFAULT_GROUP = "DEFAULT_GROUP";
|
||||
|
||||
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.registerEnabled:#{'true'}}")
|
||||
private boolean registerEnabled;
|
||||
|
||||
/**
|
||||
* nacos discovery server address.
|
||||
*/
|
||||
@Value("${spring.cloud.nacos.discovery.serverAddr:}")
|
||||
private String serverAddr;
|
||||
|
||||
/**
|
||||
* the nacos authentication username.
|
||||
*/
|
||||
@Value("${spring.cloud.nacos.discovery.username:nacos}")
|
||||
private String username;
|
||||
|
||||
/**
|
||||
* the nacos authentication password.
|
||||
*/
|
||||
@Value("${spring.cloud.nacos.discovery.username:nacos}")
|
||||
private String password;
|
||||
|
||||
/**
|
||||
* cluster name for nacos .
|
||||
*/
|
||||
@Value("${spring.cloud.nacos.discovery.cluster-name:}")
|
||||
private String clusterName;
|
||||
|
||||
/**
|
||||
* group name for nacos.
|
||||
*/
|
||||
@Value("${spring.cloud.nacos.discovery.group:DEFAULT_GROUP}")
|
||||
private String group = DEFAULT_GROUP;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "NacosContextProperties{" +
|
||||
"enabled=" + enabled +
|
||||
", discoveryEnabled=" + discoveryEnabled +
|
||||
", registerEnabled=" + registerEnabled +
|
||||
", serverAddr='" + serverAddr + '\'' +
|
||||
", username='" + username + '\'' +
|
||||
", password='" + password + '\'' +
|
||||
", clusterName='" + clusterName + '\'' +
|
||||
", group='" + group + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
Loading…
Reference in new issue