Skip to content

Commit

Permalink
add cluster nacos support
Browse files Browse the repository at this point in the history
  • Loading branch information
litian33 committed Mar 29, 2019
1 parent 16aca37 commit 29c6fae
Show file tree
Hide file tree
Showing 6 changed files with 282 additions and 0 deletions.
46 changes: 46 additions & 0 deletions oap-server/server-cluster-plugin/cluster-nacos-plugin/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Licensed to the Apache Software Foundation (ASF) under one or more
~ contributor license agreements. See the NOTICE file distributed with
~ this work for additional information regarding copyright ownership.
~ The ASF licenses this file to You under the Apache License, Version 2.0
~ (the "License"); you may not use this file except in compliance with
~ the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ 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.
~
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>server-cluster-plugin</artifactId>
<groupId>org.apache.skywalking</groupId>
<version>6.1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>cluster-nacos-plugin</artifactId>

<packaging>jar</packaging>

<dependencies>
<dependency>
<groupId>org.apache.skywalking</groupId>
<artifactId>server-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>0.9.1</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 org.apache.skywalking.oap.server.cluster.plugin.nacos;

import lombok.Getter;
import lombok.Setter;
import org.apache.skywalking.oap.server.library.module.ModuleConfig;

/**
* @author litian33@gmail.com
*/
public class ClusterModuleNacosConfig extends ModuleConfig {
@Setter
@Getter
private String serviceName;
@Setter
@Getter
private String hostPort;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 org.apache.skywalking.oap.server.cluster.plugin.nacos;

import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingFactory;
import com.alibaba.nacos.api.naming.NamingService;
import org.apache.skywalking.oap.server.core.CoreModule;
import org.apache.skywalking.oap.server.core.cluster.ClusterModule;
import org.apache.skywalking.oap.server.core.cluster.ClusterNodesQuery;
import org.apache.skywalking.oap.server.core.cluster.ClusterRegister;
import org.apache.skywalking.oap.server.library.module.ModuleConfig;
import org.apache.skywalking.oap.server.library.module.ModuleProvider;
import org.apache.skywalking.oap.server.library.module.ModuleStartException;
import org.apache.skywalking.oap.server.library.module.ServiceNotProvidedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* @author litian33@gmail.com
*/
public class ClusterModuleNacosProvider extends ModuleProvider {
private static final Logger logger = LoggerFactory.getLogger(ClusterModuleNacosProvider.class);

private final ClusterModuleNacosConfig config;
private NamingService client;

public ClusterModuleNacosProvider() {
super();
this.config = new ClusterModuleNacosConfig();
}

@Override
public String name() {
return "nacos";
}

@Override
public Class module() {
return ClusterModule.class;
}

@Override
public ModuleConfig createConfigBeanIfAbsent() {
return config;
}

@Override
public void prepare() throws ServiceNotProvidedException, ModuleStartException {
try {
client = NamingFactory.createNamingService(config.getHostPort());
} catch (NacosException e) {
logger.error("create naming service error", e);
throw new ModuleStartException(e.getMessage(), e);
}


NacosCoordinator coordinator = new NacosCoordinator(config, client);
this.registerServiceImplementation(ClusterRegister.class, coordinator);
this.registerServiceImplementation(ClusterNodesQuery.class, coordinator);
}

@Override
public void start() {
}

@Override
public void notifyAfterCompleted() {
}

@Override
public String[] requiredModules() {
return new String[]{CoreModule.NAME};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 org.apache.skywalking.oap.server.cluster.plugin.nacos;

import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.google.common.base.Strings;
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.RemoteInstance;
import org.apache.skywalking.oap.server.core.cluster.ServiceRegisterException;
import org.apache.skywalking.oap.server.core.remote.client.Address;
import org.apache.skywalking.oap.server.library.util.CollectionUtils;
import org.apache.skywalking.oap.server.telemetry.api.TelemetryRelatedContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
* @author litian33@gmail.com
*/
public class NacosCoordinator implements ClusterRegister, ClusterNodesQuery {
private static final Logger logger = LoggerFactory.getLogger(NacosCoordinator.class);

private final NamingService client;
private final String serviceName;
private volatile Address selfAddress;

public NacosCoordinator(ClusterModuleNacosConfig config, NamingService client) {
this.client = client;
this.serviceName = config.getServiceName();
}

@Override
public List<RemoteInstance> queryRemoteNodes() {
List<RemoteInstance> remoteInstances = new ArrayList<>();
List<Instance> nodes;
try {
nodes = client.selectInstances(serviceName, true);
} catch (NacosException e) {
logger.error("query service instance error", e);
return remoteInstances;
}

if (CollectionUtils.isNotEmpty(nodes)) {
nodes.forEach(node -> {
if (!Strings.isNullOrEmpty(node.getIp())) {
if (Objects.nonNull(selfAddress)) {
if (selfAddress.getHost().equals(node.getIp()) && selfAddress.getPort() == node.getPort()) {
remoteInstances.add(new RemoteInstance(new Address(node.getIp(), node.getPort(), true)));
} else {
remoteInstances.add(new RemoteInstance(new Address(node.getIp(), node.getPort(), false)));
}
}
}
});
}
return remoteInstances;
}

@Override
public void registerRemote(RemoteInstance remoteInstance) throws ServiceRegisterException {
this.selfAddress = remoteInstance.getAddress();
TelemetryRelatedContext.INSTANCE.setId(selfAddress.toString());

try {
client.registerInstance(serviceName, remoteInstance.getAddress().getHost(), remoteInstance.getAddress().getPort());
} catch (NacosException e) {
logger.error("register local instance as service error", e);
throw new ServiceRegisterException(e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# 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.
#
#

org.apache.skywalking.oap.server.cluster.plugin.nacos.ClusterModuleNacosProvider
1 change: 1 addition & 0 deletions oap-server/server-cluster-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<module>cluster-standalone-plugin</module>
<module>cluster-kubernetes-plugin</module>
<module>cluster-consul-plugin</module>
<module>cluster-nacos-plugin</module>
</modules>

<dependencies>
Expand Down

0 comments on commit 29c6fae

Please sign in to comment.