提交 72b722a5 编写于 作者: K kezhenxu94 提交者: wu-sheng

Support namespace in Nacos cluster/configuration (#3578)

* Support namespace in Nacos cluster/configuration

* Give Nacos default namespace as per config/registry
上级 002a161f
...@@ -77,6 +77,7 @@ generateClusterNacos() { ...@@ -77,6 +77,7 @@ generateClusterNacos() {
cluster: cluster:
nacos: nacos:
serviceName: \${SW_SERVICE_NAME:"SkyWalking_OAP_Cluster"} serviceName: \${SW_SERVICE_NAME:"SkyWalking_OAP_Cluster"}
namespace: \${SW_CLUSTER_NACOS_NAMESPACE:""}
hostPort: \${SW_CLUSTER_NACOS_HOST_PORT:nacos:8848} hostPort: \${SW_CLUSTER_NACOS_HOST_PORT:nacos:8848}
EOT EOT
} }
...@@ -163,15 +164,17 @@ generateConfigurationNacos() { ...@@ -163,15 +164,17 @@ generateConfigurationNacos() {
configuration: configuration:
nacos: nacos:
# Nacos Server Host # Nacos Server Host
serverAddr: \${SW_CONFIGURATION_NACO_SERVER_ADDR:naco} serverAddr: \${SW_CONFIGURATION_NACOS_SERVER_ADDR:nacos}
# Nacos Server Port # Nacos Server Port
port: \${SW_CONFIGURATION_NACO_PORT:8848} port: \${SW_CONFIGURATION_NACOS_PORT:8848}
# Nacos Configuration Group # Nacos Configuration Group
group: \${SW_CONFIGURATION_NACO_GROUP:skywalking} group: \${SW_CONFIGURATION_NACOS_GROUP:skywalking}
# Nacos Configuration namespace
namespace: \${SW_CONFIGURATION_NACOS_NAMESPACE:""}
# Unit seconds, sync period. Default fetch every 60 seconds. # Unit seconds, sync period. Default fetch every 60 seconds.
period : \${SW_CONFIGURATION_NACO_PERIOD:5} period : \${SW_CONFIGURATION_NACOS_PERIOD:5}
# the name of current cluster, set the name if you want to upstream system known. # the name of current cluster, set the name if you want to upstream system known.
clusterName: \${SW_CONFIGURATION_NACO_CLUSTER_NAME:default} clusterName: \${SW_CONFIGURATION_NACOS_CLUSTER_NAME:default}
EOT EOT
} }
......
...@@ -106,6 +106,8 @@ cluster: ...@@ -106,6 +106,8 @@ cluster:
serviceName: ${SW_SERVICE_NAME:"SkyWalking_OAP_Cluster"} serviceName: ${SW_SERVICE_NAME:"SkyWalking_OAP_Cluster"}
# Nacos cluster nodes, example: 10.0.0.1:8848,10.0.0.2:8848,10.0.0.3:8848 # Nacos cluster nodes, example: 10.0.0.1:8848,10.0.0.2:8848,10.0.0.3:8848
hostPort: ${SW_CLUSTER_NACOS_HOST_PORT:localhost:8848} hostPort: ${SW_CLUSTER_NACOS_HOST_PORT:localhost:8848}
# Nacos Configuration namespace
namespace: ${SW_CLUSTER_NACOS_NAMESPACE:"public"}
``` ```
## Etcd ## Etcd
......
...@@ -60,6 +60,8 @@ configuration: ...@@ -60,6 +60,8 @@ configuration:
port: 8848 port: 8848
# Nacos Configuration Group # Nacos Configuration Group
group: 'skywalking' group: 'skywalking'
# Nacos Configuration namespace
namespace: ''
# Unit seconds, sync period. Default fetch every 60 seconds. # Unit seconds, sync period. Default fetch every 60 seconds.
period : 60 period : 60
# the name of current cluster, set the name if you want to upstream system known. # the name of current cluster, set the name if you want to upstream system known.
......
...@@ -28,4 +28,5 @@ import org.apache.skywalking.oap.server.library.module.ModuleConfig; ...@@ -28,4 +28,5 @@ import org.apache.skywalking.oap.server.library.module.ModuleConfig;
public class ClusterModuleNacosConfig extends ModuleConfig { public class ClusterModuleNacosConfig extends ModuleConfig {
@Setter @Getter private String serviceName; @Setter @Getter private String serviceName;
@Setter @Getter private String hostPort; @Setter @Getter private String hostPort;
@Setter @Getter private String namespace = "public";
} }
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
package org.apache.skywalking.oap.server.cluster.plugin.nacos; package org.apache.skywalking.oap.server.cluster.plugin.nacos;
import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.naming.NamingFactory; import com.alibaba.nacos.api.naming.NamingFactory;
import com.alibaba.nacos.api.naming.NamingService; import com.alibaba.nacos.api.naming.NamingService;
import org.apache.skywalking.oap.server.core.CoreModule; import org.apache.skywalking.oap.server.core.CoreModule;
...@@ -27,6 +27,8 @@ import org.apache.skywalking.oap.server.core.cluster.ClusterNodesQuery; ...@@ -27,6 +27,8 @@ import org.apache.skywalking.oap.server.core.cluster.ClusterNodesQuery;
import org.apache.skywalking.oap.server.core.cluster.ClusterRegister; import org.apache.skywalking.oap.server.core.cluster.ClusterRegister;
import org.apache.skywalking.oap.server.library.module.*; import org.apache.skywalking.oap.server.library.module.*;
import java.util.Properties;
/** /**
* @author caoyixiong * @author caoyixiong
*/ */
...@@ -58,8 +60,11 @@ public class ClusterModuleNacosProvider extends ModuleProvider { ...@@ -58,8 +60,11 @@ public class ClusterModuleNacosProvider extends ModuleProvider {
@Override @Override
public void prepare() throws ServiceNotProvidedException, ModuleStartException { public void prepare() throws ServiceNotProvidedException, ModuleStartException {
try { try {
namingService = NamingFactory.createNamingService(config.getHostPort()); Properties properties = new Properties();
} catch (NacosException e) { properties.put(PropertyKeyConst.SERVER_ADDR, config.getHostPort());
properties.put(PropertyKeyConst.NAMESPACE, config.getNamespace());
namingService = NamingFactory.createNamingService(properties);
} catch (Exception e) {
throw new ModuleStartException(e.getMessage(), e); throw new ModuleStartException(e.getMessage(), e);
} }
NacosCoordinator coordinator = new NacosCoordinator(namingService, config); NacosCoordinator coordinator = new NacosCoordinator(namingService, config);
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
package org.apache.skywalking.oap.server.cluster.plugin.nacos; package org.apache.skywalking.oap.server.cluster.plugin.nacos;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.naming.NamingFactory; import com.alibaba.nacos.api.naming.NamingFactory;
import com.alibaba.nacos.api.naming.NamingService; import com.alibaba.nacos.api.naming.NamingService;
import org.apache.skywalking.oap.server.core.CoreModule; import org.apache.skywalking.oap.server.core.CoreModule;
...@@ -32,6 +33,9 @@ import org.powermock.core.classloader.annotations.PowerMockIgnore; ...@@ -32,6 +33,9 @@ import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox; import org.powermock.reflect.Whitebox;
import java.util.Properties;
import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
...@@ -79,13 +83,17 @@ public class ClusterModuleNacosProviderTest { ...@@ -79,13 +83,17 @@ public class ClusterModuleNacosProviderTest {
nacosConfig.setServiceName(SERVICE_NAME); nacosConfig.setServiceName(SERVICE_NAME);
Whitebox.setInternalState(provider, "config", nacosConfig); Whitebox.setInternalState(provider, "config", nacosConfig);
NamingService namingService = mock(NamingService.class); NamingService namingService = mock(NamingService.class);
PowerMockito.when(NamingFactory.createNamingService("10.0.0.1:1000,10.0.0.2:1001")).thenReturn(namingService);
Properties properties = new Properties();
properties.put(PropertyKeyConst.SERVER_ADDR, "10.0.0.1:1000,10.0.0.2:1001");
PowerMockito.when(NamingFactory.createNamingService(properties)).thenReturn(namingService);
provider.prepare(); provider.prepare();
ArgumentCaptor<String> addressCaptor = ArgumentCaptor.forClass(String.class); ArgumentCaptor<Properties> addressCaptor = ArgumentCaptor.forClass(Properties.class);
PowerMockito.verifyStatic(); PowerMockito.verifyStatic();
NamingFactory.createNamingService(addressCaptor.capture()); NamingFactory.createNamingService(addressCaptor.capture());
String data = addressCaptor.getValue(); Properties data = addressCaptor.getValue();
assertEquals("10.0.0.1:1000,10.0.0.2:1001", data); assertEquals("10.0.0.1:1000,10.0.0.2:1001", data.getProperty(PropertyKeyConst.SERVER_ADDR));
} }
@Test @Test
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
package org.apache.skywalking.oap.server.configuration.nacos; package org.apache.skywalking.oap.server.configuration.nacos;
import com.alibaba.nacos.api.NacosFactory; import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.config.ConfigService; import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener; import com.alibaba.nacos.api.config.listener.Listener;
import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.api.exception.NacosException;
...@@ -53,7 +54,8 @@ public class NacosConfigWatcherRegister extends ConfigWatcherRegister { ...@@ -53,7 +54,8 @@ public class NacosConfigWatcherRegister extends ConfigWatcherRegister {
final String serverAddr = this.settings.getServerAddr(); final String serverAddr = this.settings.getServerAddr();
final Properties properties = new Properties(); final Properties properties = new Properties();
properties.put("serverAddr", serverAddr + ":" + port); properties.put(PropertyKeyConst.SERVER_ADDR, serverAddr + ":" + port);
properties.put(PropertyKeyConst.NAMESPACE, settings.getNamespace());
this.configService = NacosFactory.createConfigService(properties); this.configService = NacosFactory.createConfigService(properties);
} }
......
...@@ -18,62 +18,22 @@ ...@@ -18,62 +18,22 @@
package org.apache.skywalking.oap.server.configuration.nacos; package org.apache.skywalking.oap.server.configuration.nacos;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.apache.skywalking.oap.server.library.module.ModuleConfig; import org.apache.skywalking.oap.server.library.module.ModuleConfig;
/** /**
* @author kezhenxu94 * @author kezhenxu94
*/ */
@Getter
@Setter
@ToString
public class NacosServerSettings extends ModuleConfig { public class NacosServerSettings extends ModuleConfig {
private String clusterName = "default"; private String clusterName = "default";
private String namespace = "";
private String serverAddr; private String serverAddr;
private int port = 8848; private int port = 8848;
private String group; private String group;
private int period = 60; private int period = 60;
public String getClusterName() {
return clusterName;
}
public void setClusterName(String clusterName) {
this.clusterName = clusterName;
}
public String getServerAddr() {
return serverAddr;
}
public void setServerAddr(String serverAddr) {
this.serverAddr = serverAddr;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public String getGroup() {
return group;
}
public void setGroup(String group) {
this.group = group;
}
public int getPeriod() {
return period;
}
public void setPeriod(int period) {
this.period = period;
}
public String toString() {
return "NacosServerSettings(clusterName=" + this.getClusterName()
+ ", serverAddr=" + this.getServerAddr()
+ ", group=" + this.getGroup()
+ ", period=" + this.getPeriod() + ")";
}
} }
...@@ -45,6 +45,7 @@ public class NacosConfigWatcherRegisterTest { ...@@ -45,6 +45,7 @@ public class NacosConfigWatcherRegisterTest {
final NacosServerSettings mockSettings = mock(NacosServerSettings.class); final NacosServerSettings mockSettings = mock(NacosServerSettings.class);
when(mockSettings.getGroup()).thenReturn(group); when(mockSettings.getGroup()).thenReturn(group);
when(mockSettings.getNamespace()).thenReturn("");
final NacosConfigWatcherRegister mockRegister = spy(new NacosConfigWatcherRegister(mockSettings)); final NacosConfigWatcherRegister mockRegister = spy(new NacosConfigWatcherRegister(mockSettings));
final ConfigService mockConfigService = mock(ConfigService.class); final ConfigService mockConfigService = mock(ConfigService.class);
......
...@@ -27,6 +27,8 @@ configuration: ...@@ -27,6 +27,8 @@ configuration:
port: ${nacos.port} port: ${nacos.port}
# Nacos Configuration Group # Nacos Configuration Group
group: 'skywalking' group: 'skywalking'
# Nacos Configuration namespace
namespace: ''
# Unit seconds, sync period. Default fetch every 60 seconds. # Unit seconds, sync period. Default fetch every 60 seconds.
period: 1 period: 1
# the name of current cluster, set the name if you want to upstream system known. # the name of current cluster, set the name if you want to upstream system known.
......
...@@ -40,6 +40,8 @@ cluster: ...@@ -40,6 +40,8 @@ cluster:
# nacos: # nacos:
# serviceName: ${SW_SERVICE_NAME:"SkyWalking_OAP_Cluster"} # serviceName: ${SW_SERVICE_NAME:"SkyWalking_OAP_Cluster"}
# hostPort: ${SW_CLUSTER_NACOS_HOST_PORT:localhost:8848} # hostPort: ${SW_CLUSTER_NACOS_HOST_PORT:localhost:8848}
# # Nacos Configuration namespace
# namespace: 'public'
# etcd: # etcd:
# serviceName: ${SW_SERVICE_NAME:"SkyWalking_OAP_Cluster"} # serviceName: ${SW_SERVICE_NAME:"SkyWalking_OAP_Cluster"}
# etcd cluster nodes, example: 10.0.0.1:2379,10.0.0.2:2379,10.0.0.3:2379 # etcd cluster nodes, example: 10.0.0.1:2379,10.0.0.2:2379,10.0.0.3:2379
...@@ -152,6 +154,8 @@ configuration: ...@@ -152,6 +154,8 @@ configuration:
# port: 8848 # port: 8848
# # Nacos Configuration Group # # Nacos Configuration Group
# group: 'skywalking' # group: 'skywalking'
# # Nacos Configuration namespace
# namespace: ''
# # Unit seconds, sync period. Default fetch every 60 seconds. # # Unit seconds, sync period. Default fetch every 60 seconds.
# period : 60 # period : 60
# # the name of current cluster, set the name if you want to upstream system known. # # the name of current cluster, set the name if you want to upstream system known.
......
...@@ -38,6 +38,8 @@ cluster: ...@@ -38,6 +38,8 @@ cluster:
# hostPort: ${SW_CLUSTER_CONSUL_HOST_PORT:localhost:8500} # hostPort: ${SW_CLUSTER_CONSUL_HOST_PORT:localhost:8500}
# nacos: # nacos:
# serviceName: ${SW_SERVICE_NAME:"SkyWalking_OAP_Cluster"} # serviceName: ${SW_SERVICE_NAME:"SkyWalking_OAP_Cluster"}
# # Nacos Configuration namespace
# namespace: ${SW_CLUSTER_NACOS_NAMESPACE:"public"}
# hostPort: ${SW_CLUSTER_NACOS_HOST_PORT:localhost:8848} # hostPort: ${SW_CLUSTER_NACOS_HOST_PORT:localhost:8848}
# etcd: # etcd:
# serviceName: ${SW_SERVICE_NAME:"SkyWalking_OAP_Cluster"} # serviceName: ${SW_SERVICE_NAME:"SkyWalking_OAP_Cluster"}
...@@ -164,6 +166,8 @@ configuration: ...@@ -164,6 +166,8 @@ configuration:
# port: 8848 # port: 8848
# # Nacos Configuration Group # # Nacos Configuration Group
# group: 'skywalking' # group: 'skywalking'
# # Nacos Configuration namespace
# namespace: ''
# # Unit seconds, sync period. Default fetch every 60 seconds. # # Unit seconds, sync period. Default fetch every 60 seconds.
# period : 5 # period : 5
# # the name of current cluster, set the name if you want to upstream system known. # # the name of current cluster, set the name if you want to upstream system known.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册