Skip to content

Commit

Permalink
Support namespace in Nacos cluster/configuration (#3578)
Browse files Browse the repository at this point in the history
* Support namespace in Nacos cluster/configuration

* Give Nacos default namespace as per config/registry
  • Loading branch information
kezhenxu94 authored and wu-sheng committed Oct 9, 2019
1 parent 002a161 commit 72b722a
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 60 deletions.
13 changes: 8 additions & 5 deletions docker/oap/docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ generateClusterNacos() {
cluster:
nacos:
serviceName: \${SW_SERVICE_NAME:"SkyWalking_OAP_Cluster"}
namespace: \${SW_CLUSTER_NACOS_NAMESPACE:""}
hostPort: \${SW_CLUSTER_NACOS_HOST_PORT:nacos:8848}
EOT
}
Expand Down Expand Up @@ -163,15 +164,17 @@ generateConfigurationNacos() {
configuration:
nacos:
# Nacos Server Host
serverAddr: \${SW_CONFIGURATION_NACO_SERVER_ADDR:naco}
serverAddr: \${SW_CONFIGURATION_NACOS_SERVER_ADDR:nacos}
# Nacos Server Port
port: \${SW_CONFIGURATION_NACO_PORT:8848}
port: \${SW_CONFIGURATION_NACOS_PORT:8848}
# 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.
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.
clusterName: \${SW_CONFIGURATION_NACO_CLUSTER_NAME:default}
clusterName: \${SW_CONFIGURATION_NACOS_CLUSTER_NAME:default}
EOT
}

Expand Down
2 changes: 2 additions & 0 deletions docs/en/setup/backend/backend-cluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ 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
hostPort: ${SW_CLUSTER_NACOS_HOST_PORT:localhost:8848}
# Nacos Configuration namespace
namespace: ${SW_CLUSTER_NACOS_NAMESPACE:"public"}
```

## Etcd
Expand Down
2 changes: 2 additions & 0 deletions docs/en/setup/backend/dynamic-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ configuration:
port: 8848
# Nacos Configuration Group
group: 'skywalking'
# Nacos Configuration namespace
namespace: ''
# Unit seconds, sync period. Default fetch every 60 seconds.
period : 60
# the name of current cluster, set the name if you want to upstream system known.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@
public class ClusterModuleNacosConfig extends ModuleConfig {
@Setter @Getter private String serviceName;
@Setter @Getter private String hostPort;
@Setter @Getter private String namespace = "public";
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

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.NamingService;
import org.apache.skywalking.oap.server.core.CoreModule;
Expand All @@ -27,6 +27,8 @@
import org.apache.skywalking.oap.server.core.cluster.ClusterRegister;
import org.apache.skywalking.oap.server.library.module.*;

import java.util.Properties;

/**
* @author caoyixiong
*/
Expand Down Expand Up @@ -58,8 +60,11 @@ public ModuleConfig createConfigBeanIfAbsent() {
@Override
public void prepare() throws ServiceNotProvidedException, ModuleStartException {
try {
namingService = NamingFactory.createNamingService(config.getHostPort());
} catch (NacosException e) {
Properties properties = new Properties();
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);
}
NacosCoordinator coordinator = new NacosCoordinator(namingService, config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

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.NamingService;
import org.apache.skywalking.oap.server.core.CoreModule;
Expand All @@ -32,6 +33,9 @@
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;

import java.util.Properties;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
Expand Down Expand Up @@ -79,13 +83,17 @@ public void prepare() throws Exception {
nacosConfig.setServiceName(SERVICE_NAME);
Whitebox.setInternalState(provider, "config", nacosConfig);
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();
ArgumentCaptor<String> addressCaptor = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<Properties> addressCaptor = ArgumentCaptor.forClass(Properties.class);
PowerMockito.verifyStatic();
NamingFactory.createNamingService(addressCaptor.capture());
String data = addressCaptor.getValue();
assertEquals("10.0.0.1:1000,10.0.0.2:1001", data);
Properties data = addressCaptor.getValue();
assertEquals("10.0.0.1:1000,10.0.0.2:1001", data.getProperty(PropertyKeyConst.SERVER_ADDR));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.apache.skywalking.oap.server.configuration.nacos;

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.listener.Listener;
import com.alibaba.nacos.api.exception.NacosException;
Expand Down Expand Up @@ -53,7 +54,8 @@ public NacosConfigWatcherRegister(NacosServerSettings settings) throws NacosExce
final String serverAddr = this.settings.getServerAddr();

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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,62 +18,22 @@

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;

/**
* @author kezhenxu94
*/
@Getter
@Setter
@ToString
public class NacosServerSettings extends ModuleConfig {
private String clusterName = "default";
private String namespace = "";
private String serverAddr;
private int port = 8848;
private String group;
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() + ")";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public void shouldReadConfigs() throws NacosException {

final NacosServerSettings mockSettings = mock(NacosServerSettings.class);
when(mockSettings.getGroup()).thenReturn(group);
when(mockSettings.getNamespace()).thenReturn("");

final NacosConfigWatcherRegister mockRegister = spy(new NacosConfigWatcherRegister(mockSettings));
final ConfigService mockConfigService = mock(ConfigService.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ configuration:
port: ${nacos.port}
# Nacos Configuration Group
group: 'skywalking'
# Nacos Configuration namespace
namespace: ''
# Unit seconds, sync period. Default fetch every 60 seconds.
period: 1
# the name of current cluster, set the name if you want to upstream system known.
Expand Down
4 changes: 4 additions & 0 deletions oap-server/server-starter/src/main/assembly/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ cluster:
# nacos:
# serviceName: ${SW_SERVICE_NAME:"SkyWalking_OAP_Cluster"}
# hostPort: ${SW_CLUSTER_NACOS_HOST_PORT:localhost:8848}
# # Nacos Configuration namespace
# namespace: 'public'
# etcd:
# 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
Expand Down Expand Up @@ -152,6 +154,8 @@ configuration:
# port: 8848
# # Nacos Configuration Group
# group: 'skywalking'
# # Nacos Configuration namespace
# namespace: ''
# # Unit seconds, sync period. Default fetch every 60 seconds.
# period : 60
# # the name of current cluster, set the name if you want to upstream system known.
Expand Down
4 changes: 4 additions & 0 deletions oap-server/server-starter/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ cluster:
# hostPort: ${SW_CLUSTER_CONSUL_HOST_PORT:localhost:8500}
# nacos:
# serviceName: ${SW_SERVICE_NAME:"SkyWalking_OAP_Cluster"}
# # Nacos Configuration namespace
# namespace: ${SW_CLUSTER_NACOS_NAMESPACE:"public"}
# hostPort: ${SW_CLUSTER_NACOS_HOST_PORT:localhost:8848}
# etcd:
# serviceName: ${SW_SERVICE_NAME:"SkyWalking_OAP_Cluster"}
Expand Down Expand Up @@ -164,6 +166,8 @@ configuration:
# port: 8848
# # Nacos Configuration Group
# group: 'skywalking'
# # Nacos Configuration namespace
# namespace: ''
# # Unit seconds, sync period. Default fetch every 60 seconds.
# period : 5
# # the name of current cluster, set the name if you want to upstream system known.
Expand Down

0 comments on commit 72b722a

Please sign in to comment.