You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by mi...@apache.org on 2019/08/20 06:31:32 UTC

[dubbo-admin] branch develop updated: Support nacos metadata center (#465)

This is an automated email from the ASF dual-hosted git repository.

min pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/dubbo-admin.git


The following commit(s) were added to refs/heads/develop by this push:
     new cabfdbf  Support nacos metadata center (#465)
cabfdbf is described below

commit cabfdbf532bcff50dfb7d64e145b338362885eef
Author: kexianjun <ke...@hotmail.com>
AuthorDate: Tue Aug 20 14:31:26 2019 +0800

    Support nacos metadata center (#465)
    
    * nacos metaData support
    
    * nacos metadata support
    
    * nacos metadata support
---
 dubbo-admin-server/pom.xml                         |  22 +++++
 .../metadata/impl/NacosMetaDataCollector.java      | 104 +++++++++++++++++++++
 ...dubbo.admin.registry.metadata.MetaDataCollector |   3 +-
 pom.xml                                            |  18 +++-
 4 files changed, 144 insertions(+), 3 deletions(-)

diff --git a/dubbo-admin-server/pom.xml b/dubbo-admin-server/pom.xml
index d968143..44c6828 100644
--- a/dubbo-admin-server/pom.xml
+++ b/dubbo-admin-server/pom.xml
@@ -65,6 +65,28 @@
         <dependency>
             <groupId>com.ctrip.framework.apollo</groupId>
             <artifactId>apollo-openapi</artifactId>
+            <exclusions>
+                <exclusion>
+                    <artifactId>guava</artifactId>
+                    <groupId>com.google.guava</groupId>
+                </exclusion>
+            </exclusions>
+        </dependency>
+
+        <dependency>
+            <groupId>com.alibaba.nacos</groupId>
+            <artifactId>nacos-client</artifactId>
+            <exclusions>
+                <exclusion>
+                    <artifactId>guava</artifactId>
+                    <groupId>com.google.guava</groupId>
+                </exclusion>
+            </exclusions>
+        </dependency>
+
+        <dependency>
+            <groupId>com.google.guava</groupId>
+            <artifactId>guava</artifactId>
         </dependency>
 
         <dependency>
diff --git a/dubbo-admin-server/src/main/java/org/apache/dubbo/admin/registry/metadata/impl/NacosMetaDataCollector.java b/dubbo-admin-server/src/main/java/org/apache/dubbo/admin/registry/metadata/impl/NacosMetaDataCollector.java
new file mode 100644
index 0000000..d658024
--- /dev/null
+++ b/dubbo-admin-server/src/main/java/org/apache/dubbo/admin/registry/metadata/impl/NacosMetaDataCollector.java
@@ -0,0 +1,104 @@
+/*
+ * 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.dubbo.admin.registry.metadata.impl;
+
+import org.apache.dubbo.admin.common.util.Constants;
+import org.apache.dubbo.admin.registry.metadata.MetaDataCollector;
+import org.apache.dubbo.common.URL;
+import org.apache.dubbo.common.logger.Logger;
+import org.apache.dubbo.common.logger.LoggerFactory;
+import org.apache.dubbo.metadata.identifier.MetadataIdentifier;
+
+import com.alibaba.nacos.api.NacosFactory;
+import com.alibaba.nacos.api.config.ConfigService;
+import com.alibaba.nacos.api.exception.NacosException;
+
+import java.util.Properties;
+
+import static com.alibaba.nacos.api.PropertyKeyConst.SERVER_ADDR;
+
+public class NacosMetaDataCollector implements MetaDataCollector {
+    private static final Logger logger = LoggerFactory.getLogger(NacosMetaDataCollector.class);
+    private ConfigService configService;
+    private String group;
+    private URL url;
+    @Override
+    public void setUrl(URL url) {
+        this.url = url;
+    }
+
+    @Override
+    public URL getUrl() {
+        return url;
+    }
+
+    @Override
+    public void init() {
+        group = url.getParameter(Constants.GROUP_KEY, "DEFAULT_GROUP");
+
+        configService = buildConfigService(url);
+    }
+
+    private ConfigService buildConfigService(URL url) {
+        Properties nacosProperties = buildNacosProperties(url);
+        try {
+            configService = NacosFactory.createConfigService(nacosProperties);
+        } catch (NacosException e) {
+            if (logger.isErrorEnabled()) {
+                logger.error(e.getErrMsg(), e);
+            }
+            throw new IllegalStateException(e);
+        }
+        return configService;
+    }
+
+    private Properties buildNacosProperties(URL url) {
+        Properties properties = new Properties();
+        setServerAddr(url, properties);
+        return properties;
+    }
+
+    private void setServerAddr(URL url, Properties properties) {
+
+        String serverAddr = url.getHost() + // Host
+                ":" +
+                url.getPort() // Port
+                ;
+        properties.put(SERVER_ADDR, serverAddr);
+    }
+
+    @Override
+    public String getProviderMetaData(MetadataIdentifier key) {
+        return getMetaData(key);
+    }
+
+    @Override
+    public String getConsumerMetaData(MetadataIdentifier key) {
+        return getMetaData(key);
+    }
+
+    private String getMetaData(MetadataIdentifier identifier) {
+        try {
+            return configService.getConfig(identifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.UNIQUE_KEY),
+                    group, 1000 * 10);
+        } catch (NacosException e) {
+            logger.warn("Failed to get " + identifier + " from nacos, cause: " + e.getMessage(), e);
+        }
+        return null;
+    }
+}
diff --git a/dubbo-admin-server/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.admin.registry.metadata.MetaDataCollector b/dubbo-admin-server/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.admin.registry.metadata.MetaDataCollector
index cd1a521..d9e532d 100644
--- a/dubbo-admin-server/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.admin.registry.metadata.MetaDataCollector
+++ b/dubbo-admin-server/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.admin.registry.metadata.MetaDataCollector
@@ -1,2 +1,3 @@
 zookeeper=org.apache.dubbo.admin.registry.metadata.impl.ZookeeperMetaDataCollector
-redis=org.apache.dubbo.admin.registry.metadata.impl.RedisMetaDataCollector
\ No newline at end of file
+redis=org.apache.dubbo.admin.registry.metadata.impl.RedisMetaDataCollector
+nacos=org.apache.dubbo.admin.registry.metadata.impl.NacosMetaDataCollector
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
index 13550f4..869873f 100644
--- a/pom.xml
+++ b/pom.xml
@@ -55,7 +55,7 @@
 
 	<properties>
 		<main.basedir>${project.basedir}</main.basedir>
-                <commons-lang3-version>3.7</commons-lang3-version>
+		<commons-lang3-version>3.7</commons-lang3-version>
 		<dubbo-version>2.7.2</dubbo-version>
 		<curator-version>2.12.0</curator-version>
 		<curator-test-version>4.1.0</curator-test-version>
@@ -64,7 +64,9 @@
 		<netty-version>4.1.30.Final</netty-version>
 		<jacoco-version>0.8.2</jacoco-version>
 		<jedis-version>2.9.0</jedis-version>
-                <apollo-version>1.2.0</apollo-version>
+		<apollo-version>1.2.0</apollo-version>
+		<nacos-version>1.0.0</nacos-version>
+		<guava-version>20.0</guava-version>
 		<snakeyaml-version>1.24</snakeyaml-version>
 		<maven-checkstyle-plugin-version>3.0.0</maven-checkstyle-plugin-version>
 		<spring-boot-version>2.1.4.RELEASE</spring-boot-version>
@@ -108,6 +110,12 @@
 			</dependency>
 
 			<dependency>
+				<groupId>com.alibaba.nacos</groupId>
+				<artifactId>nacos-client</artifactId>
+				<version>${nacos-version}</version>
+			</dependency>
+
+			<dependency>
 				<groupId>org.apache.dubbo</groupId>
 				<artifactId>dubbo</artifactId>
 				<version>${dubbo-version}</version>
@@ -164,6 +172,12 @@
 				<version>${netty-version}</version>
 			</dependency>
 
+			<dependency>
+				<groupId>com.google.guava</groupId>
+				<artifactId>guava</artifactId>
+				<version>${guava-version}</version>
+			</dependency>
+
 		</dependencies>
 	</dependencyManagement>